我一直在尝试在同一面板中同时设置两个可见的动态图(实时变化),但我做不到。如果有人可以帮助,那就太好了。基于此example,这是我的代码。
在代码中,有两个图(图表和chart2)。它只能显示chart或chart2,而不能同时显示chart和chart2(这是我想要的)。代码中有一部分是使其生效的注释(它是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);
}*/
});
}
}
最佳答案
通过在构造函数中为DynamicTimeSeriesCollection
指定所需的值并为nSeries
的每个刻度添加两个值,可以创建具有两个系列的Timer
,而不是多个帧。一个完整的例子被研究here。
DynamicTimeSeriesCollection dataset = new DynamicTimeSeriesCollection(2, …);