Search code examples
javajfreechartexponentialyaxis

Exponential tick on linear yAxis - JFreeChart


I have a chart with a logarithmic yAxis like this one: logarithmic chart

As we can see, the yAxis have exponential ticks (I setted it that way with yAxis.setExpTickLabelsFlag(true);). Here is the same chart with a linear axis (the one by default when creating a xylineChart):

linear chart

Unfortunately, my values on the yAxis can't be formatted with exponential ticks because it is not a LogarithmicAxis. How can I force it to?


Solution

  • As shown here, you can invoke setNumberFormatOverride() to set the desired DecimalFormat. You can invoke the setExponentSeparator() method of DecimalFormatSymbols to set the desired exponent separator.

    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setExponentSeparator("e");
    DecimalFormat format = new DecimalFormat("0.######E0", symbols);
    yAxis.setNumberFormatOverride(format);
    

    Alternatively, you should be able to modify the numberFormatterObj of LogarithmicAxis in a custom implementation of setupNumberFmtObj(), seen here, but I haven't tried it.