[英]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.在下面的代码中,我想减小 y 轴和 x 轴值的字体大小。
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 的字体大小,请使用以下代码:
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()
.如果要更改字体大小,则需要使用
.setLabelFont()
而不是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如果我将 10 更改为 1,我会得到这个不可读的
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你是对的,你需要使用
setTickLabelFont()
但在你的字体中你需要使用Font.PLAIN
而不是 0
This is a bug, reported in issue #98 : setTickLabelFont
is not respected for LogAxis
if setNumberFormatOverride
is used .这是一个错误,在问题 #98中报告:如果使用
setNumberFormatOverride
LogAxis
尊重setTickLabelFont
。 It is fixed in branch v1.5.x .它在分支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 .您可以省略覆盖,使用错误报告中的解决方法,或构建
jfreechart-1.5.4-SNAPSHOT.jar
,如图所示,像这样。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.