如何使用JFreeChart绘制数组中的数据?

时间:2018-05-17 11:07:17

标签: java jfreechart

我需要绘制一个图形,以便在x轴上有" i"数据,在y轴上将有" massAngle [i]"数据。我试着这样做:

for (int i = 0; i < massAngle.length; i++) {
    XYSeries series = new XYSeries("1");
    series.add(i, massAngle[i]);

    XYSeriesCollection seriesCollection = new XYSeriesCollection(series);
    final JFreeChart chart = ChartFactory.createXYLineChart("Долгота и Широта",
            "Долгота", "Широта", seriesCollection, PlotOrientation.VERTICAL, true, true, false);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(1150, 470));
    panel.add(chartPanel);
}

1 个答案:

答案 0 :(得分:1)

您正在为阵列的每个元素创建新的XYSeriesCollection,新的JFChart新的ChartPanel,您必须执行一次:创建XYSeries添加所有数据并构建一个图表:

XYSeries series = new XYSeries("1");
for (int i = 0; i < massAngle.length; i++) {
    series.add(i, massAngle[i]);
}

XYSeriesCollection seriesCollection = new XYSeriesCollection(series);
final JFreeChart chart = ChartFactory.createXYLineChart("Долгота и Широта", "Долгота", 
                 "Широта", seriesCollection, PlotOrientation.VERTICAL, true, true, false);

final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(1150, 470));
panel.add(chartPanel);