JFreeChart Scatter Plot Lines

时间:2011-03-13 16:46:09

标签: java jfreechart lines scatter-plot

我正在尝试使用JFreeChart创建一个图形,但它没有正确的线条。它不是按我按顺序连接点,而是按照x值的顺序连接点。我正在使用ChartFactory.createScatterPlot来创建绘图,使用XYLineAndShapeRenderer来设置可见的行。

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

}

现在我希望程序以1-2-3-4的顺序连接点,这是我将它们添加到我的数据集的顺序。但是我按顺序2-4-1-3连接它们,按x值排序。

2 个答案:

答案 0 :(得分:11)

试试这个:

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

使用XYSeries的此构造函数禁用自动排序,如XYSeries API中所定义。

在:

Before

后:

After

答案 1 :(得分:2)

缺少sscce很难说,但您可以尝试从DomainOrder.NONE的实施中返回XYDataset。此外,了解散点图中连接点的线应该有什么意义。