Automatic Bounds Calculation

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Freecharter
Posts: 4
Joined: Thu Mar 13, 2008 3:12 pm

Automatic Bounds Calculation

Post by Freecharter » Thu Mar 13, 2008 3:28 pm

I am using jfreechart 1.0.9

The developer guide states
By default, JFreeChart is configured to automatically calculate axis ranges so that all of the data
in your dataset is visible. It does this by determining the highest and lowest values in your dataset,
adding a small margin (to prevent the data being plotted right up to the edge of a chart), and
setting the axis range.
The values of my range axis are 2755.63 - 2883.65. Yet when my XYLineChart displays, the rangeaxis starts from 0.

See below url for example image (new forum users can't include urls in their posts!)

Code: Select all

img144.imageshack.us/img144/8251/chartcaj1l77ujk5.png
When I debug, I can see that

autoRange is set to true and that the Range is set to Range[0.0,3043.1099999999997]
autoRangeIncludesZero=true
autoRangeStickyZero=true

Can anyone suggest why this is?

Freecharter
Posts: 4
Joined: Thu Mar 13, 2008 3:12 pm

Post by Freecharter » Thu Mar 13, 2008 4:09 pm

I have done the following

Code: Select all

NumberAxis range = new NumberAxis();
range.setAutoRangeIncludesZero(false);
plot.setRangeAxis(range);
Whilst I think what I am seeing is now correct, is this the right way to do what I want?

Thanks

zeke56
Posts: 2
Joined: Sat May 03, 2008 8:25 pm

Post by zeke56 » Sat May 03, 2008 8:51 pm

I'm just beginning to use this package and I notice the same thing. Did you ever figure out a better solution? It seems quite bizarre that autoranging to one's data by default always includes 0. Setting it to NOT include 0 using the method you site seems wrong too, altho I've seen a couple of references to doing what you suggest. For example what happens if new data you're plotting now goes thru 0?? Do you have to reset this "Auto" range each time your data changes?! Moreover, this setAutoRangeIncludesZero method doesn't even seem to work if you generate your chart by the ChartFactory (e.g. chart = ChartFactory.createXYLineChart...) since it seems to create a ValueAxis that doesn't contain this method, and doesn't seem castable to a NumberAxis. I hope I'm just not getting something simple here or I'm in for a wild ride trying to use this package if the rest of it contains similar twisted logic.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon May 05, 2008 9:12 am

zeke56 wrote:It seems quite bizarre that autoranging to one's data by default always includes 0.
If you don't want that behaviour, just call setAutoRangeIncludesZero(false).
zeke56 wrote:Setting it to NOT include 0 using the method you site seems wrong too, altho I've seen a couple of references to doing what you suggest.
The cited method is fine, although it isn't necessary to completely replace the existing axis (you could just change the flag on the existing axis).
zeke56 wrote:For example what happens if new data you're plotting now goes thru 0??
Then the axis range will include zero regardless of the value of the 'autoRangeIncludesZero' flag (the flag is used to force zero to be included when it wouldn't otherwise be included).
zeke56 wrote:Do you have to reset this "Auto" range each time your data changes?!
No.
zeke56 wrote:Moreover, this setAutoRangeIncludesZero method doesn't even seem to work if you generate your chart by the ChartFactory (e.g. chart = ChartFactory.createXYLineChart...) since it seems to create a ValueAxis that doesn't contain this method, and doesn't seem castable to a NumberAxis.
You used the word 'seem' three times here, and each time it suggests that you are making guesses about how things work rather than actually trying them out to see how they do work. In fact, createXYLineChart() creates a chart with x and y axes that are instances of NumberAxis (which is a subclass of ValueAxis), which you could see just by looking at the source code for that method.
zeke56 wrote:I hope I'm just not getting something simple here or I'm in for a wild ride trying to use this package if the rest of it contains similar twisted logic.
I think you are not getting something simple and you are in for a wild ride.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

zeke56
Posts: 2
Joined: Sat May 03, 2008 8:25 pm

Post by zeke56 » Tue May 06, 2008 7:18 am

Thank you for taking the time to respond. I certainly was a bit quick in getting frustrated at first when I found out the terminology was different from what I've seen before and the documentation didn't accurately describe the behavior of autoRange (see Freecharter's quotation). I did in fact get things working after some more trial and error.
RE NumberAxis vs. ValueAxis:
Yes, the XY chart does seem to get created with NumberAxis, but getRangeAxis() e.g. seems to return a ValueAxis:

public ValueAxis getRangeAxis() {
return getRangeAxis(0);
}

So

xyChart = ChartFactory.createXYLineChart("test", "x", "y", chartXYDataset, PlotOrientation.VERTICAL, true, false,false);
NumberAxis range = xyChart.getXYPlot().getRangeAxis();

does not work for me. I have to cast the return to NumberAxis. I don't know why I got an error when I first tried the cast as I implied before. I probably made some compound mistake when plugging things in and trying to get them to work while still feeling my way around.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue May 06, 2008 1:22 pm

zeke56 wrote:...the documentation didn't accurately describe the behavior of autoRange (see Freecharter's quotation).
I'll try to improve the description to make this clearer.

zeke56 wrote:RE NumberAxis vs. ValueAxis:
Yes, the XY chart does seem to get created with NumberAxis, but getRangeAxis() e.g. seems to return a ValueAxis:

public ValueAxis getRangeAxis() {
return getRangeAxis(0);
}

So

xyChart = ChartFactory.createXYLineChart("test", "x", "y", chartXYDataset, PlotOrientation.VERTICAL, true, false,false);
NumberAxis range = xyChart.getXYPlot().getRangeAxis();

does not work for me. I have to cast the return to NumberAxis.
This type of casting is required in a number of places in JFreeChart. There are advantages and disadvantages. For example, internally JFreeChart only works with a ValueAxis - the actual subclass being used might be a NumberAxis, DateAxis, LogAxis or some other axis type. Advantage - flexibility in the types of axes supported. Disadvantage - you (the developer) need to know what kind of axis your chart has, in order to make use of some of the axis-type-specific attributes (like autoRangeIncludesZero which is only available in NumberAxis).

None of this is elegant, of course, but it achieves a good level of flexibility.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

donj@deltatee.com
Posts: 2
Joined: Mon Jan 13, 2020 9:41 pm
antibot: No, of course not.

Re: Automatic Bounds Calculation

Post by donj@deltatee.com » Tue Jan 14, 2020 11:06 pm

Here is a work around for this auto range without zero problem with createXYLineChart.

Create an XYPLot

XYItemRenderer renderer1 = new StandardXYItemRenderer();
this.YAxis = new NumberAxis(Graph_Ylabel);
XYPlot subplot = new XYPlot((XYDataset) this.data, this.XAxis, this.YAxis, renderer1);
this.YAxis.setAutoRangeIncludesZero(false);

Instead of using

JFreeChart chart = ChartFactory.createXYLineChart(title, Graph_Xlabel, Graph_Ylabel, data);

Use
CombinedDomainXYPlot plot = new CombinedDomainXYPlot(
new NumberAxis(Graph_Xlabel));
plot.add(subplot);
plot.setOrientation(PlotOrientation.VERTICAL);

JFreeChart chart = new JFreeChart(this.graph_title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);

ChartPanel panel = new ChartPanel(chart);
this.XAxis = (NumberAxis)chart.getXYPlot().getDomainAxis();
this.XAxis.setRange(start, end);

See the dynamic charts, chapter 14 in the developer guide

Locked