java - JFreeChart AutoRange 不适用于同一绘图上的多个系列

标签 java swing jfreechart

在此代码中,我创建了 2 个 TimeSeries 并将它们添加到同一图中,但 axis.setAutoRange(true) 仅适用于第二个系列。

有没有办法让 AutoRange 在两个 TimeSeries 上都工作?

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import javax.swing.*;
import java.awt.*;

public class Graph extends ApplicationFrame {

    private TimeSeries seriesA;
    private TimeSeries seriesB;

    public Graph(final String windowTitle, int width, int height, String xTitle, String yTitle, String headerTitle, String graphTitle) {
        super(windowTitle);
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        this.seriesA = new TimeSeries(graphTitle);
        this.seriesB = new TimeSeries(graphTitle);
        dataset.addSeries(this.seriesA);
        dataset.addSeries(this.seriesB);

        final JFreeChart chart = ChartFactory.createTimeSeriesChart(
                headerTitle,//set title
                xTitle,//set x title
                yTitle,//set y title
                dataset,
                false,
                false,
                false
        );

        final XYPlot plot = chart.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setFixedAutoRange(60000.0);

        axis = plot.getRangeAxis();
        axis.setAutoRange(true);

        final ChartPanel chartPanel = new ChartPanel(chart);
        final JPanel content = new JPanel(new BorderLayout());
        content.add(chartPanel);

        chartPanel.setPreferredSize(new java.awt.Dimension(width, height));

        setContentPane(content);
    }

    public void addPointA(double y) {
        this.seriesA.add(new Millisecond(), y);
    }

    public void addPointB(double y) {
        this.seriesB.add(new Millisecond(), y);
    }


    public static void main(final String[] args) throws InterruptedException {
        final Graph demo = new Graph("Demo",500,500,"Time","Value",
                "Header1","graph1");//window title
        demo.pack();//doesnt matter
        RefineryUtilities.positionFrameOnScreen(demo,0.2,0.7);//manually choose window position %
        demo.setVisible(true);//show window

        double lastValue=80;//randomize input
        while (true){
            demo.addPointA(lastValue);
            demo.addPointB(lastValue-100);
            //randomize input
            lastValue*=Math.random()*0.2-0.1+1.001;
            lastValue+=Math.random()*2-1;

            //limit input rate
            Thread.sleep(100);
        }
    }
}

在此图中,axis.setAutoRange(true) 仅适用于红色图表(seriesB) enter image description here

最佳答案

几个值得关注的问题:

  • 组成 TimeSeriesCollection 的每个 TimeSeries 的名称用作 Comparable 索引;名称应该是唯一的,以便可靠的自动调整范围;启用图表工厂的图例即可看到效果。

  • 如发行版中包含的 org.jfree.chart.demo.TimeSeriesChartDemo1 所示,自动范围通常不需要特殊设置。

  • 仅在 event dispatch thread 上构造和操作 Swing GUI 对象.

  • 不要在事件调度线程上sleep();使用javax.swing.Timer加快更新速度。

  • 不要不必要地扩展顶级容器。

  • 不要不必要地嵌套容器。

image

import java.awt.EventQueue;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import javax.swing.*;
import java.awt.event.*;

public class Graph extends ApplicationFrame {

    private final TimeSeries seriesA = new TimeSeries("A");
    private final TimeSeries seriesB = new TimeSeries("B");

    public Graph(final String windowTitle, int width, int height,
        String xTitle, String yTitle, String headerTitle, String graphTitle) {
        super(windowTitle);
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(this.seriesA);
        dataset.addSeries(this.seriesB);
        final JFreeChart chart = ChartFactory.createTimeSeriesChart(
            headerTitle, xTitle, yTitle, dataset, true, true, false
        );
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(width, height));
        add(chartPanel);
    }

    public void addPointA(double y) {
        this.seriesA.add(new Millisecond(), y);
    }

    public void addPointB(double y) {
        this.seriesB.add(new Millisecond(), y);
    }

    public static void main(final String[] args) {
        EventQueue.invokeLater(() -> {
            Graph demo = new Graph("Demo", 640, 480,
                "Time", "Value", "Header", "Graph");
            demo.pack();//matters a great deal
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);

            new Timer(100, (new ActionListener() {
                double lastValue = 80;

                @Override
                public void actionPerformed(ActionEvent e) {
                    demo.addPointA(lastValue);
                    demo.addPointB(lastValue - 100);
                    lastValue *= Math.random() * 0.2 - 0.1 + 1.001;
                    lastValue += Math.random() * 2 - 1;
                }
            })).start();
        });
    }
}

关于java - JFreeChart AutoRange 不适用于同一绘图上的多个系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45382132/

相关文章:

java - 为什么java.util.concurrent.atomic.AtomicBoolean内部是用int实现的?

java - 检查 ActionPerformed 方法是否发生但来自另一个类

java - 调用类型变量的基本和派生静态方法

Java 8 流 : periodic sum on array

java - 如何为单个按钮设置多个 ActionListener

java - 未能在java上制作我自己的绘图小部件

jfreechart - Jfreechart中的虚线

java - 从 JFreeChart 获取多个数据集

java - 在 Java 中创建时尚的图表,例如使用 JFreeChart

java - IntelliJ 将数组值对齐到右边距