关于Java:使用JfreeChart动态向XYSeries添加点

Adding points to XYSeries dynamically with JfreeChart

我在向XYSeries添加点时遇到问题。我有两节课。一个是Sample(它具有main方法),另一个类是JfreeChart(它具有JfreeChart代码)。在我的Sample类中,我有一个二维数组sample[row][2],该数组最初有10行,然后我需要调用JfreeChart类并将它们添加到XYSeries中并显示散点图。我设法做到了,但是下次我调用JfreeChart类时,我的数组有25行。

我需要将值添加到XYSeries并将其绘制在散点图上,该散点图应显示之前10行的颜色不同的值,现在显示25行的颜色不同的颜色……然后继续。任何人都可以提出一些建议或示例吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Sample {

    public static void main(String args[]) {
        System.out.print("(X,Y) Paired Values");
        double[][] sample = new double[row][2];

        for (int g = 0; g < sampe.length; g++) {
            for (int h = 0; h < 2; h++) {
                System.out.print("" + sample[g][h] +",");
            }
        }
        JfreeChart sample = new JfreeChart("Demo", sample);
    }

    static XYDataset samplexydataset2(double[][] sample) {
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("DataSet");
        for (int x = 0; x < sample.length; x++) {
            series.add(sample[x][0], sample[x][1]);
        }
        xySeriesCollection.addSeries(series);
        return xySeriesCollection;
    }
}

1)当我调用"首次" JfreeChart类时,我将在我的示例数组中包含这些对。

(0.78,0.80)
(0.21,0.19)
(0.181,0.187)

2)当我将JfreeChart类称为"第二时间"时,我的样本数组中将具有Diffrent值
(0.20,0.19)
(0.8,0.79)
(0.41,0.45)
(0.77,0.79)
(0.54,0.65)

这将运行几次(10次),所以我需要将其添加到" XYSeries"和" XYSeriesCollection"中,并在我调用"第二次JFreeChart类"时显示"第一次"值和"第二次"值。


您可以使用一种可用的add()方法向XYSeries添加新值,如本示例所示。如果您遇到不定的行,则需要发布一个sscce。

附录:更仔细地查看示例的起源(最近更新),可以理解有些混乱:根本不需要数组。下面的示例包含一个按钮,该按钮可将新样本添加到第二个系列。

Can I change the Color of Points when I click the"Add" Button?

每个新系列都是一种新颜色,如本示例所示。若要更改各种颜色,建议的方法是重写渲染器的getItemPaint()方法,如下所示。

ScatterAdd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/questions/7205742
 * @see https://stackoverflow.com/questions/7208657
 * @see https://stackoverflow.com/questions/7071057
 */

public class ScatterAdd extends JFrame {

    private static final int N = 8;
    private static final String title ="Scatter Add Demo";
    private static final Random rand = new Random();
    private XYSeries added = new XYSeries("Added");

    public ScatterAdd(String s) {
        super(s);
        final ChartPanel chartPanel = createDemoPanel();
        this.add(chartPanel, BorderLayout.CENTER);
        JPanel control = new JPanel();
        control.add(new JButton(new AbstractAction("Add") {

            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < N; i++) {
                    added.add(rand.nextGaussian(), rand.nextGaussian());
                }
            }
        }));
        this.add(control, BorderLayout.SOUTH);
    }

    private ChartPanel createDemoPanel() {
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            title,"X","Y", createSampleData(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);
        NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
        domain.setVerticalTickLabels(true);
        return new ChartPanel(jfreechart);
    }

    private XYDataset createSampleData() {
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("Random");
        for (int i = 0; i < N * N; i++) {
            double x = rand.nextGaussian();
            double y = rand.nextGaussian();
            series.add(x, y);
        }
        xySeriesCollection.addSeries(series);
        xySeriesCollection.addSeries(added);
        return xySeriesCollection;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ScatterAdd demo = new ScatterAdd(title);
                demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                demo.pack();
                demo.setLocationRelativeTo(null);
                demo.setVisible(true);
            }
        });
    }
}