在jfreechart中动态重绘ScatterPlot [英] Dynamically repaint ScatterPlot in jfreechart

查看:86
本文介绍了在jfreechart中动态重绘ScatterPlot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个散点图,用于绘制代理的位置.这些职位改变了.我想知道如何用新位置重新绘制/重绘散点图

I have a scatterplot which plots positions of agents. These positions change. I was wondering how can I repaint/redraw the scatterplot with the new positions

我的绘画方法.我需要在updatePositions函数中重画.有什么方法可以实现ScatterPlot的任何侦听器?

my drawing method. I need to redraw in the updatePositions function. Is there any way to implement any listener for ScatterPlot?

private ChartPanel createPanel() {
    JFreeChart jfreechart = ChartFactory.createScatterPlot(
            title, "", "", initPositions(),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);
    adjustAxis((NumberAxis) xyPlot.getDomainAxis(), true);
    adjustAxis((NumberAxis) xyPlot.getRangeAxis(), false);
    xyPlot.setBackgroundPaint(Color.white);
    return new ChartPanel(jfreechart);
}

private void adjustAxis(NumberAxis axis, boolean vertical) {
    axis.setRange(-1, lattice+1);
    axis.setTickUnit(new NumberTickUnit(1));
    axis.setVerticalTickLabels(vertical);
}

private XYDataset initPositions() {
    XYSeriesCollection xySeriesCollection = new XYSeriesCollection();

    for (int i = 0; i < populationSize; i++) {
        if(population.get(i).status==1){
            healthy.add(population.get(i).position[0], population.get(i).position[1]);
        }else if(population.get(i).status==2){
            infected.add(population.get(i).position[0], population.get(i).position[1]);
        }else if(population.get(i).status==3){
            recovered.add(population.get(i).position[0], population.get(i).position[1]);
        }
    }

    xySeriesCollection.addSeries(healthy);
    xySeriesCollection.addSeries(infected);
    xySeriesCollection.addSeries(recovered);
    return xySeriesCollection;
}

public void clear(){
    healthy.clear();
    infected.clear();
    recovered.clear();
}
public void updatePositions(ArrayList<Person> pop ){
    population = pop;

    for (int i = 0; i < populationSize; i++) {
        if(population.get(i).status==1){
            healthy.addOrUpdate(population.get(i).position[0], population.get(i).position[1]);
        }else if(population.get(i).status==2){
            infected.addOrUpdate(population.get(i).position[0], population.get(i).position[1]);
        }else if(population.get(i).status==3){
            recovered.addOrUpdate(population.get(i).position[0], population.get(i).position[1]);
        }
    }
}

这是主类中的方法.位置更新是通过移动"功能完成的

this is the method in the main class. The update of the positions is done at the "move" function

    public static void main(String [] args){
    createPopulation(populationSize);
    initInfection(infectRatio);

    EventQueue.invokeLater(new Runnable() {             
        @Override           
        public void run() {         
            DrawArea demo = new DrawArea("Demo", lattice, populationSize,population);               
            demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                
            demo.pack();                
            demo.setLocationRelativeTo(null);               
            demo.setVisible(true);
            for(int i =0;i<1000;i++){

                for(int j=0; j<populationSize; j++){
                    population.get(j).move(0.8);
                }
                demo.clear();
                demo.updatePositions(population);
            }
        }          
    });
}

推荐答案

如下所示,图表(视图)侦听其数据集(模型)并相应地进行更新.按下 Move 按钮时,该系列中的每个XYDataItemupdate()中进行了修改,以反映其新位置.

As shown below, the chart (view) listens to its dataset (model) and updates itself accordingly. When the Move button is pressed, each XYDataItem in the series in modified in update() to reflect its new position.

import java.awt.BorderLayout;
import java.awt.Dimension;
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.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see http://stackoverflow.com/a/19749344/230513
 * @see http://stackoverflow.com/a/7208723/230513
 */
public class ScatterMove extends JFrame {

    private static final int N = 16;
    private static final String title = "Scatter Move Demo";
    private static final Random rand = new Random();
    private XYSeries moved = new XYSeries("Population");

    public ScatterMove(String s) {
        super(s);
        update();
        final ChartPanel chartPanel = createDemoPanel();
        this.add(chartPanel, BorderLayout.CENTER);
        JPanel control = new JPanel();
        control.add(new JButton(new AbstractAction("Move") {
            @Override
            public void actionPerformed(ActionEvent e) {
                moved.clear();
                update();
            }
        }));
        this.add(control, BorderLayout.SOUTH);
    }

    private void update() {
        for (int i = 0; i < N; i++) {
            moved.add(new XYDataItem(rand.nextGaussian(), rand.nextGaussian()));
        }
    }

    private ChartPanel createDemoPanel() {
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            title, "X", "Y", createSampleData(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        XYItemRenderer renderer = xyPlot.getRenderer();
        NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
        domain.setRange(-3.0, 3.0);
        domain.setTickUnit(new NumberTickUnit(1));
        NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
        range.setRange(-3.0, 3.0);
        range.setTickUnit(new NumberTickUnit(1));
        return new ChartPanel(jfreechart){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
    }

    private XYDataset createSampleData() {
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        xySeriesCollection.addSeries(moved);
        return xySeriesCollection;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                ScatterMove demo = new ScatterMove(title);
                demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                demo.pack();
                demo.setLocationRelativeTo(null);
                demo.setVisible(true);
            }
        });
    }
}

这篇关于在jfreechart中动态重绘ScatterPlot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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