0

I need to draw a graph so that on the x axis there will be "i" data, and on the y axis there will be "massAngle [i]" data. I tried to do it this way:

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);
}
2
  • but ? what's the problem ? Commented May 17, 2018 at 11:10
  • 2
    I don't know JFreeChart very well, but in your code you are creating a new chart for every angle. I don't think that's what you want. I'd try to put everything outside the loop except for series.add(...) Commented May 17, 2018 at 11:10

1 Answer 1

1

You are creating a new XYSeriesCollection, a new JFChart a new ChartPanel for each element of your array, you have to do it once : create a XYSeries add all your data and build one chart :

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@VIRICH ;) don't hesite to accept/vote up the answer, it'll guide the new people which look for accepted post at first ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.