使用JfreeChart动态地向XYSeries添加点 [英] Adding points to XYSeries dynamically with JfreeChart

查看:192
本文介绍了使用JfreeChart动态地向XYSeries添加点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am facing problems in adding points to XYSeries. I have two classes. One is Sample (it has a main method) and the other class is JfreeChart (it has JfreeChart Code). In my Sample class I have a 2D array sample[row][2] which has initially 10 rows, and then I need to call the JfreeChart class and add them to XYSeries and display a scatter plot. I managed to do this, but the next time I call the Jfreechart class my Array has 25 rows.

I需要将值添加到XYSeries并在散点图上绘制它们,散点图应显示前面10行的不同颜色值,现在25行值不同颜色......这样就可以了。任何人都可以提供一些建议或示例吗?

I need to add the values to XYSeries and plot them on a scatter plot which should display the earlier 10 rows' values with different colors and now 25 rows values with different colors… and this goes on. Can anyone give some suggestions or examples?

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)当我打电话给First TimeJfreeChart Class时我将在我的样本数组中使用这些对

1)When I call "First Time" JfreeChart Class I will be having these Pairs in my Sample Array

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

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

2)当我第二次调用JfreeChart类时,我的样本数组中会有不同的值
(0.20,0.19)
(0.8,0.79) )
(0.41,0.45)
(0.77,0.79)
(0.54,0.65)

2)When I call JfreeChart Class "Second time" I will having Diffrent values in my Sample Array (0.20,0.19) (0.8,0.79) (0.41,0.45) (0.77,0.79) (0.54,0.65)

这种情况持续了几次( 10次​​)所以我需要将它添加到XYSeries和XYSeriesCollection并显示第一次值和第二次值,当我调用第二次JFreeChart类

And this goes for few times(10 times)So I need add this to "XYSeries" and "XYSeriesCollection" and display the "First time" Values and "Second time" Values when I call Second time JFreeChart Class

推荐答案

您可以向 XYSeries 使用一个可用的 add()方法,如下所示示例的。如果您正在获取不定的行,则需要发布 sscce

You can add new values to the XYSeries using one of the available add() methods, as shown in this example. If you're getting adventitious rows, you'll need to post an sscce.

附录:仔细观察(最近更新的)

Addendum: Looking more closely at the (recently updated) genesis of your example, some confusion is understandable: no array is needed at all. The example below includes a button that adds new samples to a second series.


点击添加后,我可以更改点的颜色吗按钮?

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

每个新系列都是新颜色,如示例。要更改单个颜色,建议的方法是覆盖渲染器的 getItemPaint()方法,如图所示这里

Each new series is a new color, as shown in this example. To change individual colors, the recommended way is to override the renderer's getItemPaint() method, as shown here.

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

这篇关于使用JfreeChart动态地向XYSeries添加点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆