Bug in LogarithmicAxis (urgent)

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

Bug in LogarithmicAxis (urgent)

Post by Clemens » Tue Jul 30, 2002 5:40 pm

Hi,

I think there is a bug in the logarithmic axis classes, or at least some unusual behavior.

I expect a log-log plot to draw a straight line when I draw points of the function 1/x^z at least for every positive z.

However, the JF.C. classes draw a curve.
I'd be happy if you helped me because I urgently need this feature.


I've set up an example:

Here's the output. Excel does it right:
http://www.cmarschner.net/jfreechart-bug/

These are the numbers that are the basis for this plot. These are columns for 3 series x1,y1,x2,y2,x3,y3:
1.0;1000.0;1.0;1000.0;1.0;1000.0
2.0;250.0;2.0;125.0;2.0;62.5
3.0;111.1111111111111;3.0;37.03703703703704;3.0;12.345679012345679
4.0;62.5;4.0;15.625;4.0;3.90625
5.0;40.0;5.0;8.0;5.0;1.6
6.0;27.777777777777775;6.0;4.62962962962963;6.0;0.7716049382716049
7.0;20.408163265306126;7.0;2.9154518950437316;7.0;0.41649312786339027
8.0;15.625;8.0;1.953125;8.0;0.244140625
9.0;12.345679012345679;9.0;1.3717421124828533;9.0;0.1524157902758726
10.0;10.0;10.0;1.0;10.0;0.1
11.0;8.264462809917356;11.0;0.7513148009015778;11.0;0.06830134553650706
12.0;6.944444444444444;12.0;0.5787037037037037;12.0;0.04822530864197531
13.0;5.9171597633136095;13.0;0.4551661356395084;13.0;0.035012779664577574
14.0;5.102040816326531;14.0;0.36443148688046645;14.0;0.02603082049146189
15.0;4.444444444444445;15.0;0.2962962962962963;15.0;0.019753086419753086
16.0;3.90625;16.0;0.244140625;16.0;0.0152587890625
17.0;3.4602076124567476;17.0;0.2035416242621616;17.0;0.011973036721303624
18.0;3.0864197530864197;18.0;0.17146776406035666;18.0;0.009525986892242037
19.0;2.770083102493075;19.0;0.1457938474996355;19.0;0.007673360394717659
20.0;2.5;20.0;0.125;20.0;0.00625
21.0;2.2675736961451247;21.0;0.10797969981643452;21.0;0.005141890467449262
22.0;2.066115702479339;22.0;0.09391435011269722;22.0;0.004268834096031691
23.0;1.890359168241966;23.0;0.08218952905399853;23.0;0.0035734577849564574
24.0;1.736111111111111;24.0;0.07233796296296297;24.0;0.0030140817901234567
25.0;1.6;25.0;0.064;25.0;0.00256
26.0;1.4792899408284024;26.0;0.05689576695493855;26.0;0.0021882987290360984
27.0;1.3717421124828533;27.0;0.050805263425290854;27.0;0.0018816764231589208
28.0;1.2755102040816328;28.0;0.045553935860058306;28.0;0.0016269262807163682
29.0;1.1890606420927465;29.0;0.04100209110664644;29.0;0.001413865210574015
30.0;1.1111111111111112;30.0;0.037037037037037035;30.0;0.0012345679012345679
31.0;1.040582726326743;31.0;0.03356718472021752;31.0;0.0010828124103295972
32.0;0.9765625;32.0;0.030517578125;32.0;9.5367431640625E-4
33.0;0.9182736455463728;33.0;0.027826474107465842;33.0;8.432264881050255E-4
34.0;0.8650519031141869;34.0;0.0254427030327702;34.0;7.483147950814765E-4
35.0;0.8163265306122449;35.0;0.023323615160349854;35.0;6.663890045814244E-4
36.0;0.7716049382716049;36.0;0.021433470507544582;36.0;5.953741807651273E-4
37.0;0.7304601899196493;37.0;0.019742167295125658;37.0;5.335720890574503E-4
38.0;0.6925207756232687;38.0;0.018224230937454438;38.0;4.795850246698537E-4
39.0;0.6574621959237344;39.0;0.016858005023685497;39.0;4.322565390688589E-4
40.0;0.625;40.0;0.015625;40.0;3.90625E-4
41.0;0.5948839976204641;41.0;0.014509365795621074;41.0;3.5388697062490427E-4
42.0;0.5668934240362812;42.0;0.013497462477054314;42.0;3.213681542155789E-4
43.0;0.5408328826392645;43.0;0.012577508898587546;43.0;2.9250020694389643E-4
44.0;0.5165289256198348;44.0;0.011739293764087153;44.0;2.668021310019807E-4
45.0;0.4938271604938272;45.0;0.010973936899862827;45.0;2.4386526444139612E-4
46.0;0.4725897920604915;46.0;0.010273691131749816;46.0;2.233411115597786E-4
47.0;0.4526935264825713;47.0;0.009631777159203644;47.0;2.049314289192265E-4
48.0;0.43402777777777773;48.0;0.00904224537037037;48.0;1.8838011188271605E-4
49.0;0.41649312786339027;49.0;0.008499859752314086;49.0;1.7346652555743033E-4
50.0;0.4;50.0;0.0080;50.0;1.6E-4

This is the code snippet that generated the graph:


Object[][][] data = new Object[3][50][2];
for(int i=1; i<=50; i++)
{
data[0][i-1][0] = new Double(i);
data[0][i-1][1] = new Double(1000 * Math.pow(i, -2)); // 1/x^2
data[1][i-1][0] = new Double(i);
data[1][i-1][1] = new Double(1000 * Math.pow(i, -3)); // 1/x^3
data[2][i-1][0] = new Double(i);
data[2][i-1][1] = new Double(1000 * Math.pow(i, -4)); // 1/x^4
System.out.println(data[0][i-1][0] + ";" + data[0][i-1][1] + ";" + data[1][i-1][0] + ";" + data[1][i-1][1] + ";" + data[2][i-1][0] + ";" + data[2][i-1][1]);
}

XYDataset dataset = new DefaultXYDataset(data);

JFreeChart chart = ChartFactory.createXYChart(
"logtest", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
true // include legend
);

VerticalLogarithmicAxis v = new VerticalLogarithmicAxis("testy");
chart.getXYPlot().setRangeAxis(v);

HorizontalLogarithmicAxis h = new HorizontalLogarithmicAxis(
"testx",
Axis.DEFAULT_AXIS_LABEL_FONT,
Axis.DEFAULT_AXIS_LABEL_PAINT,
Axis.DEFAULT_AXIS_LABEL_INSETS,
true, // tick labels visible
Axis.DEFAULT_TICK_LABEL_FONT,
Axis.DEFAULT_TICK_LABEL_PAINT,
Axis.DEFAULT_TICK_LABEL_INSETS,
false, // tick labels drawn vertically
true, // tick marks visible
Axis.DEFAULT_TICK_STROKE,
true, // auto range selection
NumberAxis.DEFAULT_AUTO_RANGE_INCLUDES_ZERO,
NumberAxis.DEFAULT_AUTO_RANGE_STICKY_ZERO,
NumberAxis.DEFAULT_MINIMUM_AUTO_RANGE,
0.0,
1000.0,
false, // inverted
true, // auto tick unit selection
NumberAxis.DEFAULT_TICK_UNIT,
true, // grid lines visible
ValueAxis.DEFAULT_GRID_LINE_STROKE,
ValueAxis.DEFAULT_GRID_LINE_PAINT,
ValueAxis.DEFAULT_CROSSHAIR_VISIBLE,
0.0,
ValueAxis.DEFAULT_CROSSHAIR_STROKE,
ValueAxis.DEFAULT_CROSSHAIR_PAINT,
true //'allowNegativesFlag' set false for no values < 0
);


chart.getXYPlot().setDomainAxis(h);
chart.setBackgroundPaint(Color.white);
plot.setSeriesPaint(new Paint[] { Color.green, Color.orange, Color.red });
plot.setOutlinePaint(Color.blue); // change the category labels to vertical...
File file1 = new File("chartTest.jpg");
ChartUtilities.writeChartAsJPEG(new FileOutputStream(file1), chart, 600, 400);

David Gilbert

Re: Bug in LogarithmicAxis (urgent)

Post by David Gilbert » Tue Jul 30, 2002 9:46 pm

Hi Clemens,

Another developer has submitted some changes to the logarithmic axes. I committed these changes to CVS today...try them out if you can. I will have a closer look at your sample tomorrow.

Regards,

DG.

David Gilbert

Re: Bug in LogarithmicAxis (urgent)

Post by David Gilbert » Wed Jul 31, 2002 12:23 pm

If I run your code against the CVS version of JFreeChart (a couple of minor changes are required to get it to compile) then I get this result:

http://www.object-refinery.com/jfreechart/logdemo.png

Here's the code I used, I will commit it to CVS later. This will only work against the CVS version, not the release 0.9.2:

/* ===============
* JFreeChart Demo
* ===============
*
* Project Info: http://www.object-refinery.com/jfreechart/index.html
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2002, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* ------------------
* XYLogAxesDemo.java
* ------------------
* (C) Copyright 2002, by Simba Management Limited and Contributors.
*
* Original Author: David Gilbert (for Simba Management Limited);
* Contributor(s): Clemens;
*
* $Id$
*
* Changes
* -------
* 31-Jul-2002 : Version 1 (DG);
*
*/
package com.jrefinery.chart.demo;

import java.awt.Paint;
import java.awt.Color;
import com.jrefinery.data.XYDataset;
import com.jrefinery.data.DefaultXYDataset;
import com.jrefinery.ui.ApplicationFrame;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.ChartPanel;
import com.jrefinery.chart.XYPlot;
import com.jrefinery.chart.HorizontalLogarithmicAxis;
import com.jrefinery.chart.VerticalLogarithmicAxis;
import com.jrefinery.chart.Axis;
import com.jrefinery.chart.ValueAxis;
import com.jrefinery.chart.NumberAxis;

public class XYLogAxesDemo extends ApplicationFrame {

public XYLogAxesDemo(String title) {

super(title);

Object[][][] data = new Object[3][50][2];
for(int i=1; i<=50; i++) {
data[0][i-1][0] = new Double(i);
data[0][i-1][1] = new Double(1000 * Math.pow(i, -2)); // 1/x^2
data[1][i-1][0] = new Double(i);
data[1][i-1][1] = new Double(1000 * Math.pow(i, -3)); // 1/x^3
data[2][i-1][0] = new Double(i);
data[2][i-1][1] = new Double(1000 * Math.pow(i, -4)); // 1/x^4
System.out.println(data[0][i-1][0] + ";" + data[0][i-1][1] + ";" + data[1][i-1][0] + ";" + data[1][i-1][1] + ";" + data[2][i-1][0] + ";" + data[2][i-1][1]);
}

XYDataset dataset = new DefaultXYDataset(data);

JFreeChart chart = ChartFactory.createXYChart("Log Axis Demo", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
true // include legend
);


XYPlot plot = chart.getXYPlot();
VerticalLogarithmicAxis v = new VerticalLogarithmicAxis("Log(y)");
HorizontalLogarithmicAxis h = new HorizontalLogarithmicAxis("Log(x)");
plot.setRangeAxis(v);
plot.setDomainAxis(h);
chart.setBackgroundPaint(Color.white);
plot.setSeriesPaint(new Paint[] { Color.green, Color.orange, Color.red });
plot.setOutlinePaint(Color.blue);
ChartPanel chartPanel = new ChartPanel(chart);
this.setContentPane(chartPanel);

}

/**
* Starting point for the demonstration application.
*/
public static void main(String[] args) {

XYLogAxesDemo demo = new XYLogAxesDemo("XY Log Axes Demo");
demo.pack();
demo.setVisible(true);

}

}

Clemens

Re: Bug in LogarithmicAxis (urgent)

Post by Clemens » Wed Jul 31, 2002 3:31 pm

very good, thank you for the fast response.
Unfortunately I spent an hour yesterday to solve the problem on my own. Now my chart looks exactly the way yours is looking.
Pity because of the double effort. I would have contributed my changes, but i think this is obsolete now...

--Clemens

Locked