Add values to a specified series in a DynamicTimeSeriesCollection

后端 未结 1 1213
温柔的废话
温柔的废话 2020-12-02 02:42

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 o

相关标签:
1条回答
  • 2020-12-02 03:07

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

    image

    0 讨论(0)
提交回复
热议问题