Dynamic Chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Juan M Aguera

Dynamic Chart

Post by Juan M Aguera » Fri Jul 06, 2001 2:15 am

Hi,

I'm trying to use the JFreeChart library to build charts dinamically and I'm having some issues trying to refresh the chart after changing the datasource defined,the new data is not refreshing the chart.

Does anybody knows what is the easiest way to refresh the datasource for the charts and repaint the changes. Currently, I'm trying to refresh dinamically a LineChart object.

Also, will be helpful if you have any example about this.

Thanks in advance.

Juan.

David Gilbert

RE: Dynamic Chart

Post by David Gilbert » Sun Jul 15, 2001 7:22 pm

Hi Juan,

JFreeChart (the class) implements the DataSourceListener interface, so receives dataSourceChanged(...) events from the DataSource whenever it changes. For now, if I recall correctly, the chart responds to this by completely redrawing itself.

At least that is the theory...to be honest, the update messaging hasn't had an extensive workout yet (not by me anyway). Once I get around to implementing some dynamic data source classes (the current data sources are set up once and then can't be changed) I'll no doubt find out whether my "design" actually works...

Regards,

DG.

Michael Berggren

RE: Dynamic Chart

Post by Michael Berggren » Thu Jul 19, 2001 9:33 am

Hi,

I tried to use JFreeChart with dynamic data (time series) and my final (desperate) solution was to use two "GraphDataSource" objects which each held a reference to the same data (anything to avoid rewriting JFreeChart to support dynamic data myself:-).

public class GraphDataSource extends AbstractDataSource implements XYDataSource, DomainInfo, RangeInfo

I also had a listener for changes in my data. When a change occured I just switched the source (setDataSource) for the chart. This makes the chart reinitialize itself so you don't want to make this to often, depending of the number of points per serie. I have a max length of my time series to control the amount of data used.

/Michael

David Tuma

RE: Dynamic Chart

Post by David Tuma » Fri Oct 05, 2001 6:49 pm

Michael, thank you for this workaround! I too was having difficulty figuring out how to get the chart to update (when it received my DataSourceChangeEvent, the chart *would* redraw, but the axes would not resize correctly).

One added tip - the logic in JFreeChart.setDataSource() doesn't actually check to see whether the new data source object and the old data source object are the same. Because of this, you don't have to create two different DataSource objects, swapping them out each time the data changes. You can have a single DataSource object, and simply call setDataSource with that object whenever you want the chart to refresh. You may find the code below useful:

class DataChangeWrapper implements DataSourceChangeListener
{
JFreeChart chart;
public DataChangeWrapper(JFreeChart c) { chart = c; }
public void dataSourceChanged(DataSourceChangeEvent event) {
chart.setDataSource(chart.getDataSource());
}
}

Then, when you create your chart, simply say:

dataSource.addChangeListener(new DataChangeWrapper(chart));

instead of

dataSource.addChangeListener(chart);

-David

Locked