在JFreeChart中组合动态图

时间:2016-11-14 09:34:43

标签: java jfreechart

我一直试图在同一个面板中同时显示两个动态图(它实时变化),但我不能。如果有人能提供帮助,那就太好了。基于此example,这是我的代码。

在代码中,有两个图(图表和图表2)。它只能显示图表或图表2,但不能显示图表和图表2(这是我想要的)。代码中有一部分是注释使它工作(它是图表2)。

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import java.awt.Color;

public class zzz extends JPanel {

    private final DynamicTimeSeriesCollection dataset;
    private final JFreeChart chart;
    private final JFreeChart chart2;

    public zzz(final String title) {
        setBackground(Color.WHITE);
        dataset = new DynamicTimeSeriesCollection(1, 1000, new Second());
        dataset.setTimeBase(new Second(0, 0, 0, 23, 1, 2014));
        dataset.addSeries(new float[1], 0, title);
        chart = ChartFactory.createTimeSeriesChart(
            title, "Time", title, dataset, true, false, false);

         chart2 = ChartFactory.createTimeSeriesChart(
                title, "Time", title, dataset, true, false, false); 

        final XYPlot plot = chart.getXYPlot();
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setFixedAutoRange(1000);
        axis.setDateFormatOverride(new SimpleDateFormat(/*"SS.ss"*/));

        final XYPlot plot2 = chart2.getXYPlot();
        DateAxis axis2 = (DateAxis) plot2.getDomainAxis();
        axis2.setFixedAutoRange(1000);
        axis2.setDateFormatOverride(new SimpleDateFormat(/*"SS.ss"*/));
    }

    public void update(float value) {
        float[] newData = new float[1];
        newData[0] = value;
        dataset.advanceTime();
        dataset.appendData(newData);
    }

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

            @Override
            public void run() {

                JFrame frame = new JFrame("testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final DynamicTimeSeriesChart chart
                    = new DynamicTimeSeriesChart("Alternating data");
                frame.getContentPane().add(chart);
                frame.pack();


               Timer timer = new Timer(1000, new ActionListener() {
                    private int b;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        chart.update((float) (Math.random()*50));
                    }
                });
                timer.start();
                frame.setVisible(true);
            }

               /* public void run2() {

                    JFrame frame2 = new JFrame("testing");
                    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    final DynamicTimeSeriesChart chart2  
                        = new DynamicTimeSeriesChart("Alternating data");
                    frame2.getContentPane().add(chart2);

                    frame2.pack();


                    Timer timer = new Timer(1000, new ActionListener() {
                        private int b;

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            chart2.update((float) (Math.random()*-20+2));
                        }
                    });
                    timer.start();
                    frame2.setVisible(false);
            }*/

        });
    }
}

1 个答案:

答案 0 :(得分:1)

您可以通过在构造函数中指定nSeries的所需值并为每个tick添加两个值,而不是多个框架,而是创建一个包含两个系列的DynamicTimeSeriesCollection Timer。检查完整的示例here

DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(2, …);

image