0
votes

I am new to jfreechart and i am having trouble plotting the values in sequence. i have series of numbers with each one having more than 6 decimal values example:(5.981412023,2636.51824) and varying from +6.00000000 to -6.00000000. The values are taken from excel sheet and it has a graph(xy scatter type), which doesn't look similar to the one created by jfreechart. I found out that the values are not plotted in the sequence which i gave but its arranged and plotted. example: if i gave the sequence like (8,6,2,9,4,7,5), the plot is arranged and then the line looks like its plotted to this arranged sequence like (2,4,5,6,7,8,9).

Is there any way i can plot it in a sequence of the plots inserted ? sorry about my English.

Example:

package test;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class PlotTest {
    private XYSeriesCollection dataset;

    public static void main (String[] args) {
        new PlotTest();
    }

    public PlotTest () {
        dataset = new XYSeriesCollection();
        XYSeries data = new XYSeries("data");
        data.add(3, 2); //Point 1
        data.add(1, 1); //Point 2
        data.add(4, 1); //Point 3
        data.add(2, 2); //Point 4
        dataset.addSeries(data);
        showGraph();
    }

    private void showGraph() {
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        final ApplicationFrame frame = new ApplicationFrame("Title");
        frame.setContentPane(chartPanel);
        frame.pack();
        frame.setVisible(true);
    }

    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart chart = ChartFactory.createScatterPlot(
            "Title",                  // chart title
            "X",                      // x axis label
            "Y",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,                     // tooltips
            false                     // urls
        );
        XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);
        plot.setRenderer(renderer);
        return chart;
    }

}

Based on the above example, I want the program to connect the dots in order 1-2-3-4, which is the order I added them to my dataset. But I do get them connected in order 2-4-1-3, sorted by x-value.

1
A scatter plot usually uses the first value in each pair as the x coordinate, which does not seem to be what you want. Have you considered using a different chart type?Patricia Shanahan
You may get some guidance from this possible duplicate.trashgod
@trashgod is your suggestion to use NumberAxis to map to the next plot in the series ? I do have lots of variations in data and its dynamically rendered - means the data varies btwn excel. I felt i could figure out a way to map one plot to another. Regarding the data : series.add(5.981412023 , 2636.51824); series.add( 5.858928315 , 2576.280117); the X starts from +6 to -8 and Y starts from -4000 to +4500Harish Aravind
@HarishAravind: Please edit your question to include a complete example that shows your approach; when constructing your dataset, use the XYSeries constructor with autoSort set to true.trashgod
@trashgod: i have updated with example.Harish Aravind

1 Answers

0
votes

@trashgod: Thanks for giving insights on autoSort feature.

As he suggested I came to know that we can disable autosort in XYSersies. Like

final XYSeries data = new XYSeries("data",false);

Since i mentioned it as 'false' line is drawn in the order i added the plots in dataset.