Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

java - JFreeChart Scatter Plot Lines

I'm trying to create a graph with JFreeChart, however it doesn't get the lines right. Instead of connecting the points in the order I put them, it connects the points from in order of their x-values. I'm using ChartFactory.createScatterPlot to create the plot and a XYLineAndShapeRenderer to set the lines visible.

/edit: sscce:

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;
    }

}

Now 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

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

Using this constructor for XYSeries disables autosort, as defined in the XYSeries API.

Before:

Before

After:

After


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...