如何使用jfreechart动态更新散点图

时间:2013-11-11 22:24:15

标签: java jfreechart

我应该模拟球的运动。情节中的斑点是我的球。现在,我已经生成了一个随机数据集,但后来我将读取一个连续给出球位置的输入文件。

所以我的问题是,如何连续更新我的图表,以便它看起来像是在移动?

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
//import java.security.PublicKey;
import java.util.Random;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class visualize {

    private static XYSeriesCollection createDataset() {
            Random rgenX = new Random(); 
            Random rgenY = new Random();
            XYSeriesCollection dataset = new XYSeriesCollection();
            for (int i = 999; i >= 0; i-=10) {
            int x = rgenX.nextInt(i+1);
            int y = rgenY.nextInt(i+1);
            XYSeries series1 = new XYSeries(i);
            series1.add(x, y);
            dataset.addSeries(series1);
            }
            final XYSeries series3 = new XYSeries("line1");
            series3.add(300, 400);
            series3.add(500, 300);
            dataset.addSeries(series3);
            final XYSeries series4 = new XYSeries("line2");
            series4.add(500, 200);
            series4.add(700, 300);
            dataset.addSeries(series4);

            return dataset;
    }

private static JFreeChart createChart(XYSeriesCollection dataset) {
      JFreeChart chart = ChartFactory.createXYLineChart(
                "", // chart title
                "", // x axis label
                "", // y axis label
                dataset, // data  ***-----PROBLEM------***
                PlotOrientation.VERTICAL,
                false, // include legend
                true, // tooltips
                false // urls
                );
            XYPlot xyPlot = (XYPlot) chart.getPlot();
            final XYLineAndShapeRenderer renderer0 = new XYLineAndShapeRenderer();
            renderer0.setSeriesShapesVisible(100, false);
            renderer0.setSeriesShapesVisible(101, false);
            renderer0.setSeriesStroke(100, new BasicStroke(5.0f));
            renderer0.setSeriesStroke(101, new BasicStroke(5.0f));

            xyPlot.setRenderer(renderer0);

            Shape shape  = new Ellipse2D.Double(0,0,20,20);

            XYItemRenderer renderer = xyPlot.getRenderer();
            renderer.setBaseShape(shape);
            renderer.setBasePaint(Color.red);
            renderer.setSeriesPaint(100, Color.black);
            renderer.setSeriesPaint(101, Color.black);
            for (int i = 0; i < 99; i++) {
                renderer.setSeriesShape(i, shape);

            }
            NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
            domain.setRange(0, 1000);
            domain.setTickUnit(new NumberTickUnit(50));
            domain.setVerticalTickLabels(true);
            NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
            range.setRange(0, 1000);
            range.setTickUnit(new NumberTickUnit(50));
            xyPlot.setBackgroundPaint(Color.white);

            return chart;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ChartFrame frame = new ChartFrame("DropBalls", createChart(createDataset()));
    frame.pack();
    frame.setVisible(true); 
}
}

1 个答案:

答案 0 :(得分:1)

ScatterMove显示了一种更新按钮动作侦听器中散点图的方法。要进行持续更新,请使用javax.swing.Timer,图示为here