java 使用二维数组和 JfreeChart 制作散点图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6594748/
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 16:32:33  来源:igfitidea点击:

Making a Scatter Plot Using 2d array and JfreeChart

javajfreechart

提问by Zeeshan

This is my First Month with Java, so I apologize for my stupid question in advance. I'm trying to make a simple program using Jfreechart. I want to display my 2D array on the scatter plot. here is the code:

这是我使用 Java 的第一个月,所以我提前为我的愚蠢问题道歉。我正在尝试使用 Jfreechart 制作一个简单的程序。我想在散点图上显示我的二维数组。这是代码:



package myappthatusesjfreechart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.DefaultPieDataset;

public class MyAppThatUsesJFreeChart {

    public static void main(String[] args) {
        // create a dataset...
        int[][] a2 = new int[10][5];

        // print array in rectangular form
        for (int r = 0; r < a2.length; r++) {
            for (int c = 0; c < a2[r].length; c++) {
                System.out.print(" " + a2[r][c]);
            }
            System.out.println("");
        }

        // create a chart...
        JFreeChart chart = ChartFactory.createScatterPlot(
            "Scatter Plot", // chart title
            "X", // x axis label
            "Y", // y axis label
            a2, // data  ***-----PROBLEM------***
            PlotOrientation.VERTICAL,
            true, // include legend
            true, // tooltips
            false // urls
            );

        // create and display a frame...
        ChartFrame frame = new ChartFrame("First", chart);
        frame.pack();
        frame.setVisible(true);
    }
}


The ;ChartFactory.createScatterPlot; is not allowing me to pass the 2d array, I want to ask is there any way that i can do it.

;ChartFactory.createScatterPlot; 不允许我传递二维数组,我想问有什么办法可以做到。

回答by trashgod

The createScatterPlot()method expects an XYDataset, such as XYSeriesCollection. There are examples using XYSeriesCollectionhereand here.

createScatterPlot()方法需要一个XYDataset,例如XYSeriesCollection。有使用XYSeriesCollectionherehere的例子。

Addendum: Here's an example more suited to a scatter plot; just replace a2with createDataset()in the factory call.

附录:这是一个更适合散点图的示例;只需在工厂调用中替换a2createDataset()

private static final Random r = new Random();

private static XYDataset createDataset() {
    XYSeriesCollection result = new XYSeriesCollection();
    XYSeries series = new XYSeries("Random");
    for (int i = 0; i <= 100; i++) {
        double x = r.nextDouble();
        double y = r.nextDouble();
        series.add(x, y);
    }
    result.addSeries(series);
    return result;
}