0

I've been trying to do that two dynamics plots (it is changing in real time) set visible at the same time in the same panel but I can't. If someone can help, it will be great. Based on this example, here is my code.

In the code, there are two plots (chart and chart2). It can only show chart or chart2 but not chart and chart2 at the same (It is what i want to get). There is a part in the code that is a comment to make it work (it is chart2).

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);
            }*/

        });
    }
}
Community
  • 1
  • 1

1 Answers1

1

Instead of multiple frames, you can create a DynamicTimeSeriesCollection with two series by specifying the desired value for nSeries in the constructor and adding two values for each tick for the Timer. A complete example is examined here.

DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(2, …);

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045