У меня есть диаграмма рассеивания, которая отображает позиции агентов. Эти позиции меняются. Мне было интересно, как я могу перекрасить/перерисовать диаграмму рассеяния с новыми позициямиДинамически перекрашивать ScatterPlot в jfreechart
мой метод рисования. Мне нужно перерисовать в функции updatePositions. Есть ли способ реализовать любой слушатель для 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]);
}
}
}
Это метод в основном классе. Обновление позиций выполняется в функции «Переместить»
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);
}
}
});
}
хорошо там идет мой XD сна. Спасибо, я попробую. Кажется, он будет работать с тяжелой модификацией (точки меняются в другом классе, поэтому я должен каждый раз обновлять/передавать в сюжет-класс) – MayTheSchwartzBeWithYou
Вместо очистки и добавления вам может понадобиться просмотр и расширение абстрактного класс, который реализует 'XYDataset', как показано [здесь] (http://stackoverflow.com/a/12481509/230513). – trashgod