How to plot log scale?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
gkk
Posts: 2
Joined: Mon Jan 23, 2017 8:17 pm
antibot: No, of course not.

How to plot log scale?

Post by gkk » Mon Jan 23, 2017 9:20 pm

Hello,

Using 1.0.19 JFreeChart, I'm not able to figure out how to plot my desired log-scale chart.

If I use LogarithmicAxis, how to get it to disable showing labels for the minor-axis tick marks? JFreeChart appears to set a few minor tick mark labels whenever it thinks it has room. Here's the relevant code I'm using:

Code: Select all

XYPlot plot = jchart.getXYPlot();
LogarithmicAxis xAxis = new LogarithmicAxis("Frequency (Hz)");
xAxis.setAutoTickUnitSelection(false);
xAxis.setLog10TickLabelsFlag(true);  // this never works; labels always come out as the full number
//xAxis.setExpTickLabelsFlag(true);    // this works 
xAxis.setMinorTickMarksVisible(false);  // does not work; minor tick mark labels still appear
xAxis.setTickMarksVisible(false);
xAxis.setAxisLineVisible(false);
xAxis.setRange(xLower, xUpper);
...
plot.setDomainGridlinePaint(new Color(0xb0,0xb0,0xb0)); // domain is x-axis
plot.setDomainGridlineStroke(new BasicStroke(0.5f));
plot.setDomainMinorGridlinePaint(new Color(0xb0,0xb0,0xb0));
plot.setDomainMinorGridlineStroke(new BasicStroke(0.5f));
plot.setDomainMinorGridlinesVisible(true);
plot.setDomainAxis(xAxis);
Alternatively, if I use LogAxis, I can't figure out how to control the number of minor tick marks. A log base 10 graph should have exactly 9 minor tick marks in each decade (e.g. 2,3,4,5,6,7,8,9; 1 and 10 are major tick marks). JFreeChart always creates 10 minor tick marks. Here's my code:

Code: Select all

XYPlot plot = jchart.getXYPlot();
LogAxis xAxis = new LogAxis("Frequency (Hz)");
xAxis.setAutoTickUnitSelection(false);
xAxis.setMinorTickCount(9);  // changing the integer argument has no effect on chart
xAxis.setBase(10);
xAxis.setTickMarksVisible(false);
xAxis.setAxisLineVisible(false);
xAxis.setRange(xLower, xUpper);
...
plot.setDomainGridlinePaint(new Color(0xb0,0xb0,0xb0)); // domain is x-axis
plot.setDomainGridlineStroke(new BasicStroke(0.5f));
plot.setDomainMinorGridlinePaint(new Color(0xb0,0xb0,0xb0));
plot.setDomainMinorGridlineStroke(new BasicStroke(0.5f));
plot.setDomainMinorGridlinesVisible(true);
plot.setDomainAxis(xAxis);
Been struggling with this for a long time. Any advice much appreciated.

UPDATE

I saw this post from 2008: http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=23833
which is exactly the behavior I'm seeing for the number of minor tick marks created by LogAxis default. That is, LogAxis creates 10 minor tick marks when it should be 9 (LogarithmicAxis does not have this bug). David Gilbert reports in that post that the fix was implemented in subversion for 1.0.10 release, but I'm still seeing the bug in 1.0.19.

Somehow I can't get xAxis.setMinorTickCount(9) to work for me. I change the argument's integer, but it has no effect.

gkk
Posts: 2
Joined: Mon Jan 23, 2017 8:17 pm
antibot: No, of course not.

Re: How to plot log scale?

Post by gkk » Tue Jan 24, 2017 1:19 am

What I did to solve this was take the latest version of LogAxis.java code available here,

http://www.jfree.org/jfreechart/api/jav ... l#line.100

and modify line 144 from this

Code: Select all

this.tickUnit = new NumberTickUnit(1.0, new DecimalFormat("0.#"), 10);
to this

Code: Select all

this.tickUnit = new NumberTickUnit(1.0, new DecimalFormat("0.#"), 9);
Then compile it into my java application code base.

Would be great if someone could actually correct this bug in the source code.

Also, it appears that

Code: Select all

xAxis.setMinorTickCount(9)
does not work either, so I didn't see any alternative.

Locked