Search code examples
javaplotjfreechart

creating XY Chart between different points with jfreechart


I am trying to create a interconnect between multiple points in a XY chart with jfreechart. This chart.add( 1.0 , 4.0 );chart.add( 2.0 , 5.0 ); chart.add( 2.5 , 7.0 ); sort of connects them in a line. Like this - wrong image. But I want to return back to the 1st point and create a chart like this -correct image. And I want to repeat it for multiple base nodes. Something like this -

for(int i=0;i<=1000;i++){
  for(int j=0;j<=30;j++){
    chart.add(arr1[i], arr2[j]);
  }
}

How can I go about this ?


Solution

  • enter image description hereTry using multiple XYSeries:

    In the XYSeries constructor set autosort to false to allow the lines to go backwards if needed, and set allowDuplicates to true if you may need to navigate a series through a point that has already been plotted.

        final XYSeries series1 = new XYSeries("Data 1", false, true);
        series1.add( 1.0 , 4.0 );
        series1.add( 2.0 , 5.0 );
    
        final XYSeries series2 = new XYSeries("Data 2", false, true);
        series2.add( 1.0 , 4.0 );
        series2.add( 2.5 , 7.0 );
    
        final XYSeriesCollection data = new XYSeriesCollection();
        data.addSeries(series1);
        data.addSeries(series2);
    
        final JFreeChart chart = ChartFactory.createXYLineChart(
                "XY Chart",
                "X",
                "Y",
                data,
                PlotOrientation.VERTICAL,
                true,
                true,
                false
        );
    

    To add further data series, use more invocations of XYSeriesCollection.addSeries(series).