Nasty bug with labels

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

Nasty bug with labels

Post by Andrzej Porebski » Fri Sep 22, 2000 4:50 pm

I have discovered a very nasty bug.
It involves tick labels in Numerical Axis and occurs when the minimum
value in the series is 0.0
As a result of this bug, label 1 is ALWAYS drawn instead of 0 and
if it happens that the tick unit is 1 then you have two labels 1
on the axis. (try series 5.0,7.0,0.0,9.0).
I tried it with VerticalBarChart.

I have been trying to debug this sucker but so far to no avail.
It seems like the probrlem occurs in refreshTicks method in VerticalNumberAxis. The currentTickValue is passed to the tickFormatter and out somes the label. My debugger shows that this
method is called several times (to be precise - 4) before the window
pops up. The first time it is called, a correct label 0 is generated but then on all subsequent calls an incorrect label 1 is generated.
The value passed into the format call of tickLabelFormatter in all
invocations is 0.0 but the result varies - it is either 0 or 1.
I will be debugging this more and will keep you updated.....


Andrew

Andrzej Porebski

RE: Nasty bug with labels

Post by Andrzej Porebski » Fri Sep 22, 2000 5:04 pm

I have found the problem. It is the NumberFormat and DecimalFormat bug
in java 1.2.2
Here is a test program I wrote.
Run it. It is supposed to print the same exact sequence twice but it doesn't. The second time around, it prints a 1 instead of 0.
That is exactly what I discovered in JFreeChart. 0 label is generated
only the first time format method is called on the DecimalFormat.


I will submit this bug to sun and look for a workaround in the meantime.

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.text.*;

public class test
{
public static void main(String [] args)
{
DecimalFormat nf = new DecimalFormat("#,###");
for(int j = 0; j < 2; j++)
for (int i = 0 ; i < 750000; i+=50000)
{
System.out.println(nf.format(new Double((double)i)));
}
}
}

Andrew Porebski

RE: Nasty bug with labels

Post by Andrew Porebski » Fri Sep 22, 2000 5:22 pm

Here is the fix:

The bug has to do with reusing the same instance of DecimalFormat for multiple format() method invocations.

Modify refreshTicks() in VerticalNumberAxis and HorizontalNumberAxis
to create a new instance of DecimalFormat before the label generating loop is started.

public void refreshTicks(Graphics2D g2, Rectangle2D drawArea, ..)

.....
tickLabelFormatter = new DecimalFormat(tickLabelFormatter.toPattern());

for (int i=0; i<count; i++)
{
.....
}
}

I hope that this can be incorporated into the codebase soon.

Thanks

Andrzej

David Gilbert

RE: Nasty bug with labels

Post by David Gilbert » Sat Sep 23, 2000 9:47 pm

Hi Andrzej,

Thanks for the bug report and the fix! I'll incorporate it into the next version when I get back from holiday - two more weeks now.

Regards,

DG.

Daniel Serodio

RE: Nasty bug with labels

Post by Daniel Serodio » Wed May 23, 2001 6:46 pm

I'm using the IBM Java SDK, and I can't reproduce this bug with this test code you sent (it does print the exact same sequence twice). However, JFreeChart still ignores the NumberFormat I pass it. Any other ideas?
BTW, in my experience IBM's JDK is a lot faster than Sun's (on Linux, never tried it on Windows). Haven't done any benchmarks, thou.

[]'s
Daniel Serodio

Locked