CombinedDataset and auto scaling

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Zappo
Posts: 9
Joined: Thu Jan 31, 2008 8:53 pm

CombinedDataset and auto scaling

Post by Zappo » Thu Jan 31, 2008 9:05 pm

Hi all! :)

I'm developing an application using JFreeChart. So far, I've used an XYSeriesCollection which worked fine.

Now, I also have to display error bars, min/max values and a bunch of other stuff, not all of which is clear at this point, so I figured that moving to a CombinedDataset would be the best bet.

So, what I'm doing now is, I take my XYSeriesCollection and I put it inside a CombinedDataset. This, however, creates a problem. The plot is no longer automatically adjusted to display all the data. Rather, it stays with the default axis ranges (0.0-1.0 on both).

After tinkering a bit, I've found that I need to add the XYSeriesCollection to the CombinedDataset after I've added all the data points. When I used the XYSeriesCollection directly, I could add the data points last and the chart would automatically adjust.

I could redesign the code so that I add the XYSeriesCollection to the CombinedDataset only after adding the data points, but for several reasons this isn't as easy as it sounds. For example, I need to add points over time.

So: is there a way to get a CombinedDataset to automatically adjust the axis ranges when data points are added to a series of one of the datasets it contains?

Or, at least, to explicitly tell the dataset that it has to recalculate the axis ranges?

Any idea?

nimowy
Posts: 19
Joined: Fri Jan 25, 2008 10:16 pm

Post by nimowy » Mon Feb 04, 2008 5:40 pm

Are you using a NumberAxis? If so, you could try:

Code: Select all

autoAdjustRange()
          Rescales the axis to ensure that all data is visible.
Here's the javadocs for it:
http://www.jfree.org/jfreechart/api/jav ... rAxis.html

You can get a reference to the axis from the plot object, which you get from the JFreechart object.

Code: Select all

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

Zappo
Posts: 9
Joined: Thu Jan 31, 2008 8:53 pm

Post by Zappo » Mon Feb 04, 2008 5:45 pm

nimowy wrote:Are you using a NumberAxis? If so, you could try:

Code: Select all

autoAdjustRange()
          Rescales the axis to ensure that all data is visible.
Here's the javadocs for it:
(url removed 'cause I'm a new user)

You can get a reference to the axis from the plot object, which you get from the JFreechart object.

Code: Select all

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
autoAdjustRange is protected, I can't call it from outside.

BTW, I've managed to make the chart update correctly by doing this:

Code: Select all

XYPlot plot = MainForm.mainForm.getChart().getChart().getXYPlot();
plot.setDataset( plot.getDataset() );
but I don't like it much.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Tue Feb 05, 2008 11:05 pm

Hi,
the CombinedDataset does not implement DatasetChangeListener, so it can´t be registered as a listener to any of the enclosed datasets. I assume that when one of datasets inside a CombinedDataset is changed the change event that is fired is simply lost.
Solution: make CombinedDataset implement the DatasetChangeListener and ensure that any change event is propagated towards the plot.

Zappo
Posts: 9
Joined: Thu Jan 31, 2008 8:53 pm

Post by Zappo » Tue Feb 05, 2008 11:11 pm

paradoxoff wrote:Hi,
the CombinedDataset does not implement DatasetChangeListener, so it can´t be registered as a listener to any of the enclosed datasets. I assume that when one of datasets inside a CombinedDataset is changed the change event that is fired is simply lost.
Solution: make CombinedDataset implement the DatasetChangeListener and ensure that any change event is propagated towards the plot.
That would require modifying JFreeChart's source, wouldn't it? It wouldn't be a problem for me technically, but I am working under some constraints; I might not be able to do it.

Here's another halfway-decent fix I found: call fireSeriesChanged() for each Series after you add the new values.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Wed Feb 06, 2008 12:05 pm

Zappo wrote: Here's another halfway-decent fix I found: call fireSeriesChanged() for each Series after you add the new values.
Interesting. I had assumed that fireSeriesChanged() is invoked automatically after you add items to a series. How make to sure that the event is reaching the CombinedDataset ? Do you add the CombinedDataset explicitly as a SeriesChangeListener to the XYSeries?

Zappo
Posts: 9
Joined: Thu Jan 31, 2008 8:53 pm

Post by Zappo » Wed Feb 06, 2008 12:11 pm

paradoxoff wrote:
Zappo wrote: Here's another halfway-decent fix I found: call fireSeriesChanged() for each Series after you add the new values.
Interesting. I had assumed that fireSeriesChanged() is invoked automatically after you add items to a series. How make to sure that the event is reaching the CombinedDataset ? Do you add the CombinedDataset explicitly as a SeriesChangeListener to the XYSeries?
No, I don't. Anyway, I'm using the bool parameter to tell .add not to fire the event when I add items, because I'm adding a very large number of items. This way it's faster.

However, before doing it this way, I was adding items normally and the CombinedDataset chart scale wouldn't update (the lines were drawn, only out of scale).

Locked