简体   繁体   English

更改 y 轴和 x 轴值的字体大小

[英]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);

will produce this会产生这个在此处输入图像描述

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.

相关问题 amchart-使用同一X轴的多个Y轴 - amchart - Multiple Y-axis using the same X-axis x轴上单个日期的多个y轴值-Google visualzation API - multiple y-axis values for single date on x-axis - google visualzation api Androidplot图表正在复制标签值x轴和y轴 - Androidplot chart is duplicating label values x-axis & y-axis libgdx中的x轴高度和y轴宽度是吗? - Is height along the x-axis and width along the y-axis in libgdx? achartengine两行带有2个y轴和1个x轴的-&gt;第二行不适合 - achartengine two lines with 2 y-axis and 1 x-axis -> second line dont fit 我想要一个标签为 x 轴和 y 轴为字符串的图表 - I want a chart whose label is x-axis and y-axis are string 有没有办法在 apache poi 5.0.0 中设置我的 X 轴(类别)或 Y 轴(值)的 majorUnit、minorUnit、最小值和最大值? - Is there any way to set majorUnit, minorUnit, minimum and maximum of my X-axis( category) or Y-axis (value) in apache poi 5.0.0? opengl y轴未按x或z缩放 - opengl y-axis not scaling with x or z 是否可以使用vaadin插件InvientCharts动态更改x和y轴标题 - Is it possible to change the x- and y-axis caption dynamically with the vaadin addon InvientCharts 更改BarChart中的x轴位置 - change x-axis position in BarChart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM