簡體   English   中英

更改 y 軸和 x 軸值的字體大小

[英]Change the font size of the y-axis and x-axis values

在下面的代碼中,我想減小 y 軸和 x 軸值的字體大小。

在此處輸入圖像描述

我搜索並找到了這些代碼:

假設您想減小數字軸的字體大小,請使用以下代碼:

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

假設您想減小 CategoryAxis 的字體大小,請使用以下代碼:

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

但不幸的是,軸的大小並沒有減小。 我做錯什么了嗎?

此示例代碼:

 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);
        }

    }

如果要更改字體大小,則需要使用.setLabelFont()而不是setTickLabelFont() 你可以在這個帖子上看到

這個:

Font nwfont = new Font("Arial", Font.PLAIN, 10);
NumberAxis rangeAxis = (NumberAxis) p.getRangeAxis();
rangeAxis.setLabelFont(nwfont);

會產生這個在此處輸入圖像描述

如果我將 10 更改為 1,我會得到這個不可讀的在此處輸入圖像描述

如果它仍然沒有回答你的問題,你能詳細說明你需要什么嗎?

編輯:

你是對的,你需要使用setTickLabelFont()但在你的字體中你需要使用Font.PLAIN而不是 0

在此處輸入圖像描述

這是一個錯誤,在問題 #98中報告:如果使用setNumberFormatOverride LogAxis尊重setTickLabelFont 它在分支v1.5.x中修復。 您可以省略覆蓋,使用錯誤報告中的解決方法,或構建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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM