Logarithmic Axis Labels/Ticks Customization

Solution 1:

Even though you're using a LogAxis, you can specify integer tick units, as shown in the variation of @amaidment's example below.

LogAxis

/** @see http://stackoverflow.com/a/10353270/230513 */
private static void createFrame() {
    XYSeries series = new XYSeries("Series");
    for (int i = 0; i <= N; i++) {
        series.add(i, Math.pow(2, i));
    }
    NumberAxis xAxis = new NumberAxis("X");
    xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    LogAxis yAxis = new LogAxis("Y");
    yAxis.setBase(2);
    yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    XYPlot plot = new XYPlot(new XYSeriesCollection(series),
        xAxis, yAxis, new XYLineAndShapeRenderer(true, false));
    JFreeChart chart = new JFreeChart(
        "Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    JFrame frame = new JFrame("LogAxis Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ChartPanel(chart));
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            createFrame();
        }
    });
}

Solution 2:

I think what you need is logAxis.setNumberFormatOverride(NumberFormat format);

EDIT: Since further help is needed... try this:

logAxis.setBase(10);
LogFormat format = new LogFormat(logAxis.getBase(), "", "", true);
logAxis.setNumberFormatOverride(format);

Here's a whole method that can be used to play with...:

  public static void main(String[] args) {
    XYSeries series = new XYSeries("");
    series.add(1, 10);
    series.add(2, 100);
    series.add(3, 1000);
    series.add(4, 10000);
    series.add(5, 100000);
    series.add(6, 1000000);

//    NumberAxis yAxis = new NumberAxis("");
    LogAxis yAxis = new LogAxis("");
    yAxis.setBase(10);
    LogFormat format = new LogFormat(yAxis.getBase(), "", "", true);
    yAxis.setNumberFormatOverride(format);
    XYPlot plot = new XYPlot(
      new XYSeriesCollection(series),
      new NumberAxis(""),
      yAxis,
      new XYLineAndShapeRenderer(true, false));
    JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);

    JFrame frame = new JFrame("LogAxis Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ChartPanel(chart));
    frame.pack();
    frame.setVisible(true);
  }