10

I would like to know how to refresh a chart if we want to change "in live" some piece of data. I mean for instance, I have a chart with a TaskSeries which appears on 3 years and I would like to change 3 years by 5 years. I want the chart to change immediately.

Is there some kind of update chart or something like that ?

I know that you could say "get your TaskSeries, do your changes and it will be refreshed automatically" but my TaskSeries are generated and I cannot easily change these ones. That's why I would like to find a way to recalculate and to rebuild the whole chart.

frogatto
  • 27,475
  • 10
  • 76
  • 119
Johann
  • 437
  • 2
  • 7
  • 23

6 Answers6

7

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.

Community
  • 1
  • 1
trashgod
  • 200,320
  • 28
  • 229
  • 974
  • Is `XYPlot` registered as the listener which listens to `SeriesChangeEvent` by default when the `JFreeChart` is created by `ChartFactory` (that is to say, I don't need to register one by myself)? Your answer helped me twice in two days. Thanks! – Dante WWWW Dec 29 '11 at 08:11
  • Indirectly: [`AbstractSeriesDataset`](http://www.jfree.org/jfreechart/api/javadoc/org/jfree/data/general/AbstractSeriesDataset.html) _forwards_ series changes to the plot, which (automatically) listens for `Dataset` changes. – trashgod Dec 29 '11 at 09:12
5

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.

Djak
  • 51
  • 1
  • 1
4

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
}
yegor256
  • 97,508
  • 114
  • 426
  • 573
2
// 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();
mnmp
  • 360
  • 2
  • 14
0

What worked with me was the following:

//reset with new dataset
chart().setDataset(dataset);
repaint the ChartPanel that contains the JFreeChart
chartPanel.repaint();
user3245747
  • 825
  • 2
  • 16
  • 27
0

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.

Erik
  • 3,767
  • 19
  • 27
  • 1
    I've found JFreeChart's approach very compatible with Swing's [separable model architecture](http://java.sun.com/products/jfc/tsc/articles/architecture/). – trashgod Jun 01 '11 at 19:34