Jfreechart - Refresh a chart according to changing data

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 03:59:14
trashgod

The add() method of TaskSeries automatically sends a SeriesChangeEvent to all registered listeners, e.g. CategoryPlot. In this example, DynamicTimeSeriesCollection implements SeriesChangeEvent. In this case, the chart's XYPlot is a registered listener.

I had this issue too with an XYPlot. I found a workaround by resetting the dataset:

chart.getXYPlot().setDataset(chart.getXYPlot().getDataset());

that's crazy but it works...

Note: chart.setNotify(true) seems to do nothing.

Danni V. Hansen

I had this issue; I did it using this:

private void refreshChart() {
    jPanel_GraphicsTop.removeAll();
    jPanel_GraphicsTop.revalidate(); // This removes the old chart 
    aChart = createChart(); 
    aChart.removeLegend(); 
    ChartPanel chartPanel = new ChartPanel(aChart); 
    jPanel_GraphicsTop.setLayout(new BorderLayout()); 
    jPanel_GraphicsTop.add(chartPanel); 
    jPanel_GraphicsTop.repaint(); // This method makes the new chart appear
}

I haven't found an easy way to update a JFreeChart "live", since the data-structure of jfreechart is very incompatible to my data-structure. So I build a redraw()-Method of my own, that collects the data from my dataModel, build up a JFreeChart dataModel and set the chart new.

This gives the feeling of a "live" update, also it is very ugly.

// create a chart
ChartFrame mychartframe = new ChartFrame("my charts", chart);

// some other stuff ...

// somewhere else in a code far far away
mychartframe.getChartPanel().getChart().fireChartChanged();

What worked with me was the following:

//reset with new dataset
chart().setDataset(dataset);
repaint the ChartPanel that contains the JFreeChart
chartPanel.repaint();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!