In the following code, I want to reduce the font size of the y-axis and x-axis values.
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);
}
}
If you want to change the font size you need to use .setLabelFont()
instead of setTickLabelFont()
. You can see it on this post
this:
Font nwfont = new Font("Arial", Font.PLAIN, 10);
NumberAxis rangeAxis = (NumberAxis) p.getRangeAxis();
rangeAxis.setLabelFont(nwfont);
if I change the 10 to 1 I will get this wich is unreadble
If it's still not respond to your question can you elaborate more what you need?
EDIT:
You were right you need to use setTickLabelFont()
but in your Font you need to use the Font.PLAIN
instead of 0
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 .
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.