ValueAxis FixedDimension bug

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jylaxx
Posts: 4
Joined: Tue Aug 07, 2012 10:16 pm
antibot: No, of course not.

ValueAxis FixedDimension bug

Post by jylaxx » Tue Aug 07, 2012 10:42 pm

I tried to use FixedDimension option of a ValueAxis but the behaviour was not correct.
After investigation I found that probably one piece of code was not at the right place :

class ValueAxis

Code: Select all

public AxisSpace reserveSpace(Graphics2D g2, Plot plot,
                                  Rectangle2D plotArea,
                                  RectangleEdge edge, AxisSpace space) {

        // create a new space object if one wasn't supplied...
        if (space == null) {
            space = new AxisSpace();
        }

        // if the axis is not visible, no additional space is required...
        if (!isVisible()) {
            return space;
        }

        // if the axis has a fixed dimension, return it...
		// <-- fix part #1 move this code to the end
//        double dimension = getFixedDimension();
//        if (dimension > 0.0) {
//            space.ensureAtLeast(dimension, edge);
//        }
		// -->

        // calculate the max size of the tick labels (if visible)...
        double tickLabelHeight = 0.0;
        double tickLabelWidth = 0.0;
        if (isTickLabelsVisible()) {
            g2.setFont(getTickLabelFont());
            List ticks = refreshTicks(g2, new AxisState(), plotArea, edge);
            if (RectangleEdge.isTopOrBottom(edge)) {
                tickLabelHeight = findMaximumTickLabelHeight(ticks, g2,
                        plotArea, isVerticalTickLabels());
            }
            else if (RectangleEdge.isLeftOrRight(edge)) {
                tickLabelWidth = findMaximumTickLabelWidth(ticks, g2, plotArea,
                        isVerticalTickLabels());
            }
        }

        // get the axis label size and update the space object...
        Rectangle2D labelEnclosure = getLabelEnclosure(g2, edge);
        double labelHeight = 0.0;
        double labelWidth = 0.0;
        if (RectangleEdge.isTopOrBottom(edge)) {
            labelHeight = labelEnclosure.getHeight();
            space.add(labelHeight + tickLabelHeight, edge);
        }
        else if (RectangleEdge.isLeftOrRight(edge)) {
            labelWidth = labelEnclosure.getWidth();
            space.add(labelWidth + tickLabelWidth, edge);
        }

		// <-- fix part #2
        double dimension = getFixedDimension();
        if (dimension > 0.0) {
            space.ensureAtLeast(dimension, edge);
        }
		// -->

        return space;

    }
This fix has to be include also in PeriodAxis

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: ValueAxis FixedDimension bug

Post by matinh » Wed Aug 08, 2012 8:23 am

Hi!

I've created an entry in the bug-tracker: #3555275.
Could you be so kind and give some more details about the actual problem. What is the expected and the actual behaviour?
A demo program or a unit-test would be very helpful!

Thanks for reporting,
- martin

jylaxx
Posts: 4
Joined: Tue Aug 07, 2012 10:16 pm
antibot: No, of course not.

Re: ValueAxis FixedDimension bug

Post by jylaxx » Wed Aug 08, 2012 10:21 am

Well, it is not easy for me to set a demo or unit test now but the bug seems obvious.
On the screen capture you can see in first image that the fixed dimension (set to 50) is first added and then the labels axis. So the axis width change with labels width.
In second image (with fix) the axis width is correct and identical in both charts.

Image

Image

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

Re: ValueAxis FixedDimension bug

Post by david.gilbert » Mon Sep 03, 2012 8:23 pm

Thanks for reporting this. There is definitely a bug here, but I think the correct behaviour would be for the code to *add* the fixedDimension to the incoming 'space' argument (which might already be configured to allow space for other axes that belong to the plot) then return early (there's no point calculating the size of the axis label and tick labels if the developer has already specified a fixed dimension). This would be my JUnit test:

Code: Select all

    /**
     * A test for bug 3555275 (where the fixed axis space is calculated 
     * incorrectly).
     */
    public void test3555275() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        JFreeChart chart = ChartFactory.createLineChart("Title", "X", "Y",
                dataset, PlotOrientation.VERTICAL, true, false, false);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setInsets(RectangleInsets.ZERO_INSETS);
        plot.setAxisOffset(RectangleInsets.ZERO_INSETS);
        ValueAxis yAxis = plot.getRangeAxis();
        yAxis.setFixedDimension(100.0);
        BufferedImage image = new BufferedImage(500, 300, 
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        ChartRenderingInfo info = new ChartRenderingInfo();
        chart.draw(g2, new Rectangle2D.Double(0, 0, 500, 300), info);
        g2.dispose();
        Rectangle2D rect = info.getPlotInfo().getDataArea();
        double x = rect.getMinX();
        assertEquals(100.0, x, 1.0);
    }
...and this is the fix (but this might require some more testing to be sure it does in fact have the desired outcome in all cases):

Code: Select all

# This patch file was generated by NetBeans IDE
# Following Index: paths are relative to: /Users/dgilbert/NetBeansProjects/jfreechart-1.0.x-branch/source/org/jfree/chart/axis
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: ValueAxis.java
--- ValueAxis.java Remotely Modified (Based On HEAD)
+++ ValueAxis.java Locally Modified (Based On LOCAL)
@@ -2,7 +2,7 @@
  * JFreeChart : a free chart library for the Java(tm) platform
  * ===========================================================
  *
- * (C) Copyright 2000-2011, by Object Refinery Limited and Contributors.
+ * (C) Copyright 2000-2012, by Object Refinery Limited and Contributors.
  *
  * Project Info:  http://www.jfree.org/jfreechart/index.html
  *
@@ -27,7 +27,7 @@
  * --------------
  * ValueAxis.java
  * --------------
- * (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
+ * (C) Copyright 2000-2012, by Object Refinery Limited and Contributors.
  *
  * Original Author:  David Gilbert (for Object Refinery Limited);
  * Contributor(s):   Jonathan Nash;
@@ -105,6 +105,7 @@
  * 26-Mar-2009 : In equals(), only check current range if autoRange is
  *               false (DG);
  * 30-Mar-2009 : Added pan(double) method (DG);
+ * 03-Sep-2012 : Fix reserveSpace() method, bug 3555275 (DG);
  *
  */
 
@@ -798,7 +799,8 @@
         // if the axis has a fixed dimension, return it...
         double dimension = getFixedDimension();
         if (dimension > 0.0) {
-            space.ensureAtLeast(dimension, edge);
+            space.add(dimension, edge);
+            return space;
         }
 
         // calculate the max size of the tick labels (if visible)...
If anyone has a chance to test this out, it would be great to hear your feedback.
David Gilbert
JFreeChart Project Leader

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

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

Re: ValueAxis FixedDimension bug

Post by david.gilbert » Tue Sep 04, 2012 6:31 am

I've committed this fix for inclusion in the 1.0.15 release.
David Gilbert
JFreeChart Project Leader

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

Locked