java - JFreeChart 格式 Y 轴以显示 Power 中的值

标签 java graph jfreechart

如何设置 y 轴格式以将值显示为 5*10^5、1*10^6、2*10^6...而不是 500,000、1,000,000、2,000,000...并且可被 5 或 10 整除?

enter image description here

最佳答案

一个LogAxis使用默认的刻度单位似乎可以工作。此相关example使用整数刻度单位。

image

import java.awt.Dimension;
import java.awt.EventQueue;
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/a/22450677/230513
 * @see https://stackoverflow.com/a/10353270/230513
 */
public class Test {

    private static final int N = 7;

    private void display() {
        XYSeries series = new XYSeries("Series");
        for (int i = 0; i <= N; i++) {
            series.add(i, Math.pow(10, i));
        }
        NumberAxis xAxis = new NumberAxis("X");
        xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        LogAxis yAxis = new LogAxis("Y");
        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 f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(480, 240);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}

关于java - JFreeChart 格式 Y 轴以显示 Power 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22447540/

相关文章:

java - 无法在java模式匹配中获得正确的输出

java - 将 JasperReport jrxml 加载到 JFrame

java - 通过Java代码在jasper Report中类别轴标签表达式对齐

java - jfreecharts中如何调整x、y轴线

java - JFreeChart MeterPlot

java - 如何限制对安全页面的直接访问?

Java 'Email Sending Code' > 已知异常(索引 : 14, 大小 : 14)?

java - 如何在没有内存溢出的情况下检查图中的所有循环?

c++ - 有效地在 Boost BGL 图中找到所有可达的顶点

algorithm - 了解收缩层次结构