Search code examples
javajfreechart

JFreeChart: Add multiple series to DynamicTimeSeriesCollection and update them properly


I have a dynamic live graph made with JFreeChart. My question is :
How to add multiple series to DynamicTimeSeriesCollection and update them properly, because I need 3 lines /series/ with different color in my plot. ?

Here is my code:

    dataset = new DynamicTimeSeriesCollection(1, 200, new Second());
    dataset.setTimeBase(new Second(0, 0, 0, 23, 1, 2014));
    //Add series
    dataset.addSeries(new float[1], 0, "key1");

Everything is working fine until I add second series with different key:

 dataset.addSeries(new float[1], 0, "key2");

The problem is that my

dataset.advanceTime();
dataset.appendData(newData);

methods does not know to which series to refer because they take no parameter like series key, for example.


Solution

  • Add series and use the second parameter as a key:

    dataset.addSeries(new float[1], 0, "X");
    dataset.addSeries(new float[1], 1, "Y");
    dataset.addSeries(new float[1], 2, "Z");
    

    Use the key set in the series as a place in the float array.

    public void update(float valueX, float valueY , float valueZ) {
        float[] newData = new float[3];
        newData[0] = valueX;
        newData[1] = valueY;
        newData[2] = valueZ;
        dataset.advanceTime();
        dataset.appendData(newData);
    }