我需要绘制一个图形,以便在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);
}

最佳答案

您正在为数组的每个元素创建一个新的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);

09-26 05:22