Forcing axis update in dynamic plots?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Irv Thomae

Forcing axis update in dynamic plots?

Post by Irv Thomae » Mon Dec 16, 2002 9:19 pm

Hello David,
You may recall that I've submitted some alternative classes for faster performance - many inspired by your suggestion to use "FastScatterPlot" as a jumping-off point.

For dynamic plots of time-dependent data, I've been experimenting with an alternative to the TimeSeriesCollection class, implementing the "XYDataset" interface with simple arrays for faster updates. The data now marches forward very nicely, but the time axis is not being updated (even though I call "fireDatasetChanged()" each time new data is added.

Can you point me toward the series of classes and method calls in "official" jFreeChart code, which normally cause the x- (time-)axis to be redrawn when another TimePeriod's data are added?

Thanks,
Irv Thomae

David Gilbert

Re: Forcing axis update in dynamic plots?

Post by David Gilbert » Tue Dec 17, 2002 12:15 am

Hi Irv,

All datasets have the methods addDatasetChangeListener(...) and removeDatasetChangeListener(...), because those methods are defined in the Dataset interface.

Classes that implement the dataset interfaces should include a mechanism for recording the registered listeners, and send them an event notification whenever anything in the dataset changes. The AbstractDataset class provides a default implementation of this (including the fireDatasetChanged() method), or you can write your own. Sending the event to all listeners is all that you are required to do from the dataset...after that, if the chart doesn't update then there is a bug elsewhere.

If you look at the Plot class, you will see that it implements the DatasetChangeListener interface and, further, that it automatically registers itself with the dataset passed into the setDataset(...) method. So the plot should always hear about it when the dataset changes.

Once the plot receives notification that the dataset has changed, it just tells the JFreeChart class, which in turn tells the ChartPanel class, which reacts by redrawing the entire chart.

In the FastScatterPlot class, I think this chain of events is not fully implemented since I was just trying out the speed of drawing a static chart...it shouldn't be hard to fix.

Regards,

DG

Irv Thomae

Re: Forcing axis update in dynamic plots?

Post by Irv Thomae » Wed Dec 18, 2002 8:21 pm

David,
Thanks for the explanation - I spent much of yesterday reviewing my code to see where I'd gone wrong. Found and fixed quite a few second-order problems, but this (rather significant) problem still remains.

You wrote:
Once the plot receives notification that the dataset has changed, it just tells the JFreeChart class, which in turn tells the ChartPanel class, which reacts by redrawing the entire chart.

But in my case, although fireDatasetChanged() causes the _data_ to be redrawn appropriately (with new data showing up at right, and old data getting pushed off the left edge), the time axis never gets updated.
When new data (for a new instant of time) is added to my TimeSeriesCollection, do I need to supplement fireDatasetChanged() with an AxisChangeEvent? - [Which could be done by calling HorizontalDateAxis's setRange() method, but I hadn't expected that to be necessary.] And if an AxisChangeEvent is indeed needed, what class is the appropriate Listener, the axis itself, the Chart, or "who"???
Meanwhile I'll try some experiments along these lines, but even if they "work", I might not be the only user who'd like to understand it better.

Thanks,
Irv Thomae

Priti Ranka

Re: Forcing axis update in dynamic plots?

Post by Priti Ranka » Mon Dec 23, 2002 2:12 pm

Hi,

I think I am facing a similar problem.
I need a scatter plot with time on my x axis .
Just like timeseries my x axis should be updated.
But When I try to cast x axis of scatter plot to HorizontalDateAxis it gives me class cast exception.

So I have to use HorizontalNumberAxis but then I cannot format the labels to date format ...

Any help on this is truly appreciated.

Regards,
Priti

David Gilbert

Re: Forcing axis update in dynamic plots?

Post by David Gilbert » Mon Dec 23, 2002 2:23 pm

Hi Priti,

If you used the createScatterPlot(...) method in the ChartFactory class, your XYPlot will be set up with a HorizontalNumberAxis for the domain (horizontal) axis. You are free to replace that with a HorizontalDateAxis, using the setDomainAxis(...) method in the XYPlot class:

XYPlot plot = myChart.getXYPlot();
plot.setDomainAxis(new HorizontalDateAxis("Date/Time"));

Now the axis will interpret the x-values as milliseconds since 1-Jan-1970 (same as java.util.Date) and label the axis accordingly.

Or you could write a new method createTimeSeriesScatterPlot(...) that does almost the same thing as the createScatterPlot(...) method in the ChartFactory class, but uses a HorizontalDateAxis from the start.

Regards,

Dave Gilbert

Priti Ranka

Re: Forcing axis update in dynamic plots?

Post by Priti Ranka » Tue Dec 24, 2002 7:49 am

Hi Dave,

Thanks a lot.

Regards,
Priti

Locked