java JFreeChart 散点图线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5290812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:27:08  来源:igfitidea点击:

JFreeChart Scatter Plot Lines

javajfreechartlinesscatter-plot

提问by hvitedragonfire

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.

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

/edit: sscce:

/编辑: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.

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

采纳答案by andersoj

Try this:

试试这个:

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

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

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

Before:

前:

Before

前

After:

后:

After

后

回答by trashgod

Absent an sscceit's hard to say, but you might try returning DomainOrder.NONEfrom your implementation of XYDataset. Also, it may help to know what meaning should be given to lines connecting dots in a scatter plot.

没有sscce很难说,但您可以尝试DomainOrder.NONEXYDataset. 此外,了解散点图中连接点的线的含义可能会有所帮助。