java JFreeChart x 轴刻度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14054091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:54:11  来源:igfitidea点击:

JFreeChart x axis scale

javagraphjfreechart

提问by Biscuit128

I have a JFree XY Line chart which always starts at x = 0. Then based on user defined settings from a properties file, the application increments based on that number (this represents the time in minutes).

我有一个 JFree XY 折线图,它总是从 x = 0 开始。然后根据用户定义的属性文件设置,应用程序根据该数字递增(这表示以分钟为单位的时间)。

For example, x = 0 to start the user defined setting is 5 so the scale goes 0, 5, 10, 15, 20…, or the user setting is 3 so it goes 0, 3, 6, 9, 12… Pretty simple.

例如,x = 0 开始用户定义的设置为 5,因此比例变为 0、5、10、15、20……,或者用户设置为 3,因此变为 0、3、6、9、12……非常简单.

The issue I am having is the way in which the graph starts. If I start at 0, then 0 is in the middle of the graph rather than at the bottom left with -0.0000005, -0.000004, -0.000003… 0.000000 , 0.000001 , 0.000002… 0.000005

我遇到的问题是图表开始的方式。如果我从 0 开始,那么 0 位于图表的中间而不是左下角 -0.0000005, -0.000004, -0.000003… 0.000000 , 0.000001 , 0.000002… 0.000005

How can I just manually add the scale at the bottom, i.e. define it should be increments of 2 and then maintain it?

我怎样才能在底部手动添加比例,即定义它应该是 2 的增量然后保持它?

回答by obourgain

You should use NumberAxis, which contains a lot of methods to define the scale of your chart.

您应该使用NumberAxis,它包含许多定义图表比例的方法。

Example :

例子 :

// Create an XY Line chart
XYSeries series = new XYSeries("Random Data");
series.add(1.0, 500.2);
series.add(10.0, 694.1);
XYSeriesCollection data = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart("XY Series Demo", "X", "Y", data,
                                                  PlotOrientation.VERTICAL, 
                                                  true, true, false);

// Create an NumberAxis
NumberAxis xAxis = new NumberAxis();
xAxis.setTickUnit(new NumberTickUnit(2));

// Assign it to the chart
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainAxis(xAxis);

回答by trashgod

Based on this example, here's an ssccethat uses setTickUnit()to adjust the domain axis tick unit dynamically, starting from the value 5.

基于这个例子,这里有一个sscce用于setTickUnit()动态调整域轴刻度单位,从值开始5

image

图片

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** @see https://stackoverflow.com/a/14167983/230513 */
public class SSCCE {

    private static final int COUNT = 100;
    private static final int UNITS = 5;
    private static final Random r = new Random();

    public static void main(String[] args) {
        XYSeries series = new XYSeries("Data");
        for (int i = 0; i < COUNT; i++) {
            series.add(i, r.nextGaussian());
        }
        XYSeriesCollection data = new XYSeriesCollection(series);
        final JFreeChart chart = ChartFactory.createXYLineChart("TickUnits",
            "X", "Y", data, PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        final NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
        xAxis.setTickUnit(new NumberTickUnit(UNITS));
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("TickUnitDemo");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(new ChartPanel(chart));
                final JSpinner spinner = new JSpinner(
                    new SpinnerNumberModel(UNITS, 1, COUNT, 1));
                spinner.addChangeListener(new ChangeListener() {

                    @Override
                    public void stateChanged(ChangeEvent e) {
                        JSpinner s = (JSpinner) e.getSource();
                        Number n = (Number) s.getValue();
                        xAxis.setTickUnit(new NumberTickUnit(n.intValue()));
                    }
                });
                JPanel p = new JPanel();
                p.add(new JLabel(chart.getTitle().getText()));
                p.add(spinner);
                f.add(p, BorderLayout.SOUTH);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });

    }
}