簡體   English   中英

JFreeChart 固定范圍軸刻度

[英]JFreeChart Fixed range axis ticks

我正在嘗試生成折線圖,但我無法弄清楚如何

a) 使用 0.001、0.01、0.1、1、10 和 100 的固定刻度設置范圍軸

b) 僅顯示圖表中的那些刻度

c) 自定義 label 那些滴答聲(只能在 a 和 b 之后解決)。

我正在嘗試使用 SymbolAxis,但我無法讓它工作。

這就是我所追求的:

在此處輸入圖像描述

經過一番實驗。

在此處輸入圖像描述

public static void main(String[] args) {
    //setting up my ticks
    final TickUnits standardUnits = new TickUnits();
    standardUnits.add(new NumberTickUnit(0.001));
    standardUnits.add(new NumberTickUnit(0.01));
    standardUnits.add(new NumberTickUnit(0.1));
    standardUnits.add(new NumberTickUnit(1));
    standardUnits.add(new NumberTickUnit(10));
    standardUnits.add(new NumberTickUnit(100));

    //data
    XYSeries series = new XYSeries("Series");
    series.add(0, 130);
    series.add(1, 0.35);
    series.add(2, 0.021);
    series.add(3, 0.0042);
    series.add(4, 0.002);
    series.add(5, 0.003);
    series.add(6, 10130);
    
    NumberAxis xAxis = new NumberAxis("X");
    xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    LogAxis yAxis = new LogAxis("Y");
    yAxis.setBase(10);      
    yAxis.setNumberFormatOverride(new DecimalFormat());
    yAxis.setRange(0.001, 100);
    yAxis.setStandardTickUnits(standardUnits); // NumberAxis.createIntegerTickUnits());
    XYPlot plot = new XYPlot(new XYSeriesCollection(series), xAxis, yAxis, new XYLineAndShapeRenderer(true, false));
    JFreeChart chart = new JFreeChart("Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
    JFrame frame = new JFrame("LogAxis Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ChartPanel(chart));
    frame.pack();
    frame.setVisible(true);
}

這種方法說明了使用帶有LogAxis刻度單位的 LogAxis,如此處所建議 顯示的特定刻度單位集有時是必要的,但在下面的變體中可以看到更通用的解決方案; 它依賴於TickUnitSource創建的NumberAxis

yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

而且,

  • 這里所討論的,缺少ChartFactory時,合適的ChartTheme “為影響幾何的元素建立一致的默認值,例如字體度量和軸偏移。”

  • 如此處所述必須在事件調度線程上構造和操作 Swing GUI 對象。

對數軸圖像

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.text.NumberFormat;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
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;

public class LogTest {

    /**
     * @see https://stackoverflow.com/a/70648615/230513
     * @see https://stackoverflow.com/a/10353270/230513
     */
    private void display() {
        XYSeries series = new XYSeries("Series");
        series.add(0, 130);
        series.add(1, 0.35);
        series.add(2, 0.021);
        series.add(3, 0.0042);
        series.add(4, 0.002);
        series.add(5, 0.003);
        series.add(6, 10130);

        NumberAxis xAxis = new NumberAxis("X");
        xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        LogAxis yAxis = new LogAxis("Y");
        yAxis.setBase(10);
        yAxis.setNumberFormatOverride(NumberFormat.getNumberInstance());
        yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        XYPlot plot = new XYPlot(new XYSeriesCollection(series),
            xAxis, yAxis, new XYLineAndShapeRenderer(true, false));
        JFreeChart chart = new JFreeChart("Chart",
            JFreeChart.DEFAULT_TITLE_FONT, plot, false);
        StandardChartTheme theme = new StandardChartTheme("Custom");
        theme.setPlotBackgroundPaint(Color.WHITE);
        theme.setChartBackgroundPaint(new GradientPaint(
            0, 0, Color.WHITE, 0, 480, new Color(0xc0ffff)));
        theme.apply(chart);

        JFrame f = new JFrame("LogAxis Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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