问题描述
程序将每秒接收数据并在时间序列图表上绘制。但是,一旦我创建了两个系列,我就无法为其添加新值。它只显示一条直线。
The program will receive data every second and draw them on time Series chart. However, once I create two series, I cannot add new value to it. It displays a straight line only.
如何将数据附加到指定的系列?即 YYY
。基于此,以下是我正在做的事情:
How do I append data to a specified series? I.e. YYY
. Based on this example, here's what I'm doing:
...
// Data set.
final DynamicTimeSeriesCollection dataset =
new DynamicTimeSeriesCollection( 2, COUNT, new Second() );
dataset.setTimeBase( new Second( 0, 0, 0, 1, 1, 2011 ) );
dataset.addSeries( gaussianData(), 0, "XXX" );
dataset.addSeries( gaussianData(), 1, "YYY" );
// Chart.
JFreeChart chart = createChart( dataset );
this.add( new ChartPanel( chart ), BorderLayout.CENTER );
// Timer.
timer = new Timer( 1000, new ActionListener() {
@Override
public void actionPerformed ( ActionEvent e ) {
dataset.advanceTime();
dataset.appendData( new float[] { randomValue() } );
}
} );
...
private JFreeChart createChart ( final XYDataset dataset ) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
TITLE, "", "", dataset, true, true, false );
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange( true );
ValueAxis range = plot.getRangeAxis();
range.setRange( -MINMAX, MINMAX );
return result;
}
推荐答案
假设你是从这里,您已经指定了一个包含两个系列的数据集,但您只是附加一个值计时器
的每个刻度。每个tick都需要两个值。以下是我修改原文以获得下图的方法:
Assuming you started from here, you've specified a dataset with two series, but you're only appending one value with each tick of the Timer
. You need two values for each tick. Here's how I modified the original to get the picture below:
final DynamicTimeSeriesCollection dataset =
new DynamicTimeSeriesCollection(2, COUNT, new Second());
...
dataset.addSeries(gaussianData(), 0, "Human");
dataset.addSeries(gaussianData(), 1, "Alien");
...
timer = new Timer(FAST, new ActionListener() {
// two values appended with each tick
float[] newData = new float[2];
@Override
public void actionPerformed(ActionEvent e) {
newData[0] = randomValue();
newData[1] = randomValue();
dataset.advanceTime();
dataset.appendData(newData);
}
});
这篇关于将值添加到DynamicTimeSeriesCollection中的指定系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!