2012-02-03 103 views
0

如何使用Swing繪製這樣的圖形?我已經使用了一個JFreeChart庫,但我不知道如何使用該庫來繪製這樣的線圖?如何在Swing中繪製此圖表?

graph

import org.jfree.chart.*; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.data.xy.*; 

public class DrawGraph{ 

public void drawGraph(int[][] drawPoints) { 
    XYSeries series = new XYSeries("Average Weight"); 
    for(int i=0;i<drawPoints.length;i++){ 
    for(int j=0;j<=1;j+=2){ 
     if(drawPoints[i][j]!=0){ 
      series.add(bla...bla...bla...); 
     } 
    } 
    } 

    XYDataset xyDataset = new XYSeriesCollection(series); 
    JFreeChart chart = ChartFactory.createXYLineChart 
    ("XYLine Chart using JFreeChart", "Age", "Weight", 
xyDataset, PlotOrientation.VERTICAL, true, true, false); 
    ChartFrame frame1=new ChartFrame("XYLine Chart",chart); 
    frame1.setVisible(true); 
    frame1.setSize(300,300); 
    } 

} 

我畫圖表利用這一點,但不工作...

+0

一張圖片描繪了千言萬語。 ***你的***圖是什麼樣的? – 2012-02-03 07:18:47

+0

@AndrewThompson:我已經給出了一個圖表的鏈接...在頂部或點擊[這裏](http://i.imgur.com/ahyiQ.jpg) – Parth 2012-02-03 07:27:48

+0

因此,你想畫一個像在頂部還是您繪製的頂部? – Kris 2012-02-03 07:28:59

回答

1

看起來你在構建數據集時遇到了麻煩。您可以使用ChartFactory.createXYAreaChart()ChartFactory.createXYLineChart()以下所示的方法。

private static XYDataset createDataset() { 
    XYSeriesCollection result = new XYSeriesCollection(); 
    XYSeries series = new XYSeries("Test"); 
    series.add(0, 2); 
    // more points here 
    series.add(10, 10); 
    result.addSeries(series); 
    return result; 
} 

另請參閱這些examples

順便說一下,目前還不清楚什麼對你很重要,我無法理解頂部的無序軸。在我看來,更好的問題不是如何製作此圖表?而是如何最好地顯示此數據?