How to set absolute axis margin?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

How to set absolute axis margin?

Post by mhilpert » Fri Dec 08, 2006 11:15 am

The range of an axis can be extended by setting lower/upper margins or just decrease/increase the min/max range values. However this is always a percentage of the range values. For charts that can have a wide variety of possible values, a percentage setting is unusable as for some charts, 5% is just right for one charts and wrong for other charts. So we need the possibility to set an absolute margin.

I thought of getting the width/height of a plot and calculate the absolute length of an axis and then calculate the percentage margin of this length. But JFreeChart doesn't have these values. Only when you save a chart or display it in a frame, you can set width/height of the chart.
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

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 » Fri Dec 08, 2006 11:29 am

Do you mean a margin specified as a fixed number of Java2D units? That could be done, but I think it would not be a very general solution in the face of changing the chart drawing dimensions (which is why the percentage margins are used). But maybe I misunderstand what you mean...
David Gilbert
JFreeChart Project Leader

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

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Post by mhilpert » Fri Dec 08, 2006 3:16 pm

Well, I thought I found a workaround:

I have a (custom) logarithmic axis. So far, I adjusted the range to subtract/add a 5% of the range to the minRange/maxRange. This works good for linear axes, but for logarithmic axis, the bottom range can get very big, if the upper range is very big. So I have to decouple the margins from the range "distance" (maxRange - minRange). Instead I search for the minimum and maximum value and compute the minimumMargin only dependend on the minimumValue and the maximumMargin only dependend on the maximumValue. This works also for non-linear axes as it doesn't matter what the value between min/max value/range is (as this is wrong either way).

Code: Select all

                    final ValueAxis va = (ValueAxis) axis;
                    double minValue = 0.0;
                    double maxValue = 0.0;
                    Number n = getMinMaxValue(chart, axisType, indexAxis, false);
                    if (n != null) minValue = n.doubleValue();
                    n = getMinMaxValue(chart, axisType, indexAxis, true);
                    if (n != null) maxValue = n.doubleValue();
                    
                    final double deltaBottom = minValue * marginBottom;
                    final double deltaTop = maxValue * marginTop;
                    
                    double minRange = minValue - deltaBottom;
                    double maxRange = maxValue + deltaTop;
                    
                    va.setRange(minRange, maxRange);
However, this is not a real solution as the margin is still a relative percentage factor. This solved the problem of one of my charts, but suddenly my other charts got broken up as the percentage solely rely on the min/max values is awfully wrong if you have a small range but high values (e.g. 99,74 - 101,34). For such cases, the range should be taken into account - which breaks my new case ...

Either way, the only solution would be a margin that doesn't depend on the dataset values / range. It should just be a percentage of the axis length or a fixed length in pixels/Java2D dimension.
Java 11, JFreeChart 1.0.15, JFreeSVG 4.0

Jojor
Posts: 1
Joined: Tue Jan 10, 2017 3:26 pm
antibot: No, of course not.

Re: How to set absolute axis margin?

Post by Jojor » Tue Jan 10, 2017 3:57 pm

I know this topic is very old but I faced the exact same issue and I thought it could be interesting to share my solution.
If there is a better way to do it now, please let me know, I'm new to JFreeChart.

So my issue is exactly what mhilpert described, I want to generically add the same margin every time to my Y axis but I can't use percentage because I can have linear or logarithmic axis, depending on my chart instance.
I didn't want to manipulate Java2D objects or do complex math to add a margin so I did this in two steps:
- first, I set my new range to my Y axis without margins
- then, I use JFreeChart#zoomRangeAxes() to zoom out a little on the Y axis from the center of the chart, it will add top and bottom margins to my Y range.

Thus, I don't have to know what are my values on Y and JFreeChart will do all the math for me.

Locked