Skip to content
Advertisement

Change the font size of the y-axis and x-axis values

In the following code, I want to reduce the font size of the y-axis and x-axis values.

enter image description here

I searched and found these code:

suppose you want to reduce the font size of number axis use the following code:

Font nwfont=new Font("Arial",0,7);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setTickLabelFont(nwfont);

suppose you want to reduce the font size of CategoryAxis use the following code:

CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setTickLabelFont(nwfont);

but unfortunately, the size of the axes did not decrease. Did I do something wrong?

this sample code:

 public class NegativeExpPlot {

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

        private void display() {
            int nData = 100;
            Random r = new Random(nData);
            XYSeries ds1 = new XYSeries("rand");
            for (int i = 0; i < nData; i++) {
                ds1.add(r.nextDouble(), r.nextDouble() / 1000);
            }
            for (int i = 0; i < nData; i++) {
                ds1.add(r.nextDouble(), r.nextDouble() * 1000);
            }
            LogAxis logAxis = new LogAxis("log");
            XYPlot p = new XYPlot(new XYSeriesCollection(ds1), new NumberAxis(),
                logAxis, new XYLineAndShapeRenderer());
            logAxis.setNumberFormatOverride(new DecimalFormat("0.0E0"));
            Font nwfont=new Font("Arial",0,1);
            logAxis.setTickLabelFont(nwfont);
            JFreeChart chart = new JFreeChart(p);
            JFrame f = new JFrame("Log Axis");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new ChartPanel(chart));
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
            NumberAxis rangeAxis = (NumberAxis) p.getRangeAxis();
            rangeAxis.setTickLabelFont(nwfont);
        }

    }

Advertisement

Answer

This is a bug, reported in issue #98: setTickLabelFont is not respected for LogAxis if setNumberFormatOverride is used. It is fixed in branch v1.5.x. You can omit the override, use the workaround in the bug report, or build jfreechart-1.5.4-SNAPSHOT.jar, illustrated, like this.

image

import java.awt.EventQueue;
import java.awt.Font;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Random;
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.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** @see https://stackoverflow.com/q/70758061/230513 */
public class ExpoTest {

    private static final int N = 100;
    private static final Font FONT = new Font(Font.SANS_SERIF, Font.BOLD, 16);

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

    private void display() {
        Random r = new Random();
        XYSeries series = new XYSeries("rand");
        for (int i = 0; i < N; i++) {
            series.add(r.nextDouble(), r.nextDouble() / 1000);
        }
        NumberAxis domainAxis = new NumberAxis();
        domainAxis.setTickLabelFont(FONT);
        LogAxis rangeAxis = new LogAxis("log");
        rangeAxis.setTickLabelFont(FONT);
        rangeAxis.setNumberFormatOverride(new DecimalFormat("0.0E0"));
        XYPlot p = new XYPlot(new XYSeriesCollection(series), domainAxis,
            rangeAxis, new XYLineAndShapeRenderer());
        JFreeChart chart = new JFreeChart(p);
        JFrame f = new JFrame("Log Axis");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(chart));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement