2012-02-03 105 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

顺便说一下,目前还不清楚什么对你很重要,我无法理解顶部的无序轴。在我看来,更好的问题不是如何制作此图表?而是如何最好地显示此数据?