NumberFormatOverride does not work well

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
RémiONERA
Posts: 5
Joined: Wed Jul 24, 2013 7:07 am
antibot: No, of course not.

NumberFormatOverride does not work well

Post by RémiONERA » Tue Oct 03, 2017 2:09 pm

Hello

I have a XY plot chart with Y values between 1e-20 and 1e-12. I use a logarithmic scale and I want my ticks label to be shown in a scientific format (ie 1e-14)
So I did this

Code: Select all

NumberAxis Axis = (NumberAxis) l_Plot.getRangeAxis();
DecimalFormat formatter=new DecimalFormat("##0.##E0",new DecimalFormatSymbols(new Locale("us")));
Axis.setNumberFormatOverride(formatter);
Unfortunatly the result is a scale with numbers not formatted. I get 0.0000000000001 instead of 1e-12

The fun is that it works for values over 1

Any ideas
Rémi

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

Re: NumberFormatOverride does not work well

Post by david.gilbert » Tue Oct 03, 2017 4:55 pm

Did you look at replacing the NumberAxis in your plot with an instance of LogAxis?
David Gilbert
JFreeChart Project Leader

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

RémiONERA
Posts: 5
Joined: Wed Jul 24, 2013 7:07 am
antibot: No, of course not.

Re: NumberFormatOverride does not work well

Post by RémiONERA » Wed Oct 04, 2017 7:14 am

My axis is already a LogAxis. I have replace

Code: Select all

NumberAxis Axis = (NumberAxis) l_Plot.getRangeAxis();
by

Code: Select all

LogarithmicAxis Axis = (LogarithmicAxis) l_Plot.getRangeAxis();
It still does not work
Rémi

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: NumberFormatOverride does not work well

Post by John Matthews » Thu Oct 05, 2017 1:31 am

I can't reproduce your result using LogAxis, suggested by David Gilbert above. As shown in the examples examined here, the LogTest below will let you experiment with various formats.
  • Your DecimalFormat seems to work using setNumberFormatOverride().
  • The LogFormat shown lets the axis display a scaled exponent, rather than a scaled mantissa.
  • The default format, sans override, uses an AttributedString for superscripting.

Code: Select all

import java.awt.EventQueue;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.LogAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.util.LogFormat;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/q/10347674/230513
 */
public class LogTest {

    private void display() {
        XYSeries series = new XYSeries("");
        for (int i = 1; i < 10; i++) {
            series.add(i, Math.pow(10, i));
        }
        LogAxis yAxis = new LogAxis("Range");
        //yAxis.setNumberFormatOverride(new DecimalFormat("##0.##E0"));
        //yAxis.setNumberFormatOverride(new LogFormat(10, "10", "E", true));
        XYPlot plot = new XYPlot(new XYSeriesCollection(series),
            new NumberAxis("Domain"), yAxis, new XYLineAndShapeRenderer(true, false));
        JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(chart));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new LogTest()::display);
    }
}

RémiONERA
Posts: 5
Joined: Wed Jul 24, 2013 7:07 am
antibot: No, of course not.

NumberFormatOverride does not work well

Post by RémiONERA » Thu Oct 05, 2017 8:37 am

Your code is working but it does not respond to my problem. If I use my data instead of your formula I have label with decimal exponent (10-20.5) witch is not acceptable.
For exemple

Code: Select all

DecimalFormat df=new DecimalFormat("##0.##E0",new DecimalFormatSymbols(new Locale("us")));
LogFormat lf=new LogFormat(10, "10", "E", true);
System.out.println(d.format(53e-15));
System.out.println(lf.format(53e-15));

The result is
53E-15
10E-13,28
The first one is ok but the second one is not understandable for the people who will look at the values even if 53e-15=10e-13.28

The thing I don't understand is why when my exponent is negative the label is shown like that 0.00000000001 is there a problem with AttributedString?
Rémi

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: NumberFormatOverride does not work well

Post by John Matthews » Thu Oct 05, 2017 10:53 am

RémiONERA wrote:Is there a problem with AttributedString?
Not to my knowledge. As shown in the createTickLabel() source code, TextAttribute.SUPERSCRIPT is applied when you don't specify a format override. If you do specify a format override, LogFormat scales the exponent, while DecimalFormat scales the mantissa, as your result shows.

RémiONERA
Posts: 5
Joined: Wed Jul 24, 2013 7:07 am
antibot: No, of course not.

Re: NumberFormatOverride does not work well

Post by RémiONERA » Tue Oct 10, 2017 12:59 pm

In the LogarithmicAxis class I have found the setExpTickLabelsFlag(boolean flag) which solves my problem.
I have also tried setLog10TickLabelsFlag(boolean flag) doesn't work.
Rémi

Locked