JFreeChart: DynamicTimeSeries with a period of n milliseconds

I am trying to define an interface in which I want to build some values ​​received by an external device. These values ​​are received at a frequency that can be set via the interface. Of course, the period of the graph should vary depending on the period defined by the user. So I started defining a followint chart:

int periodMs = 200;
MilliDTSC dataset = new MilliDTSC(1,100, new MultipleOfMillisecond(periodMs));
dataset.setTimeBase(new MultipleOfMillisecond(periodMs))
dataset.addSeries(zeroSeries()),0,"Zero data") // zeroSeries returs a series with values set to 0
JFreeChart chart = createChart(dataset) // create the chart and set ranges and legends
ChartPanel panel = new ChartPanel(panel);

MilliDTSCis the following class proposed here :

public class MilliDTSC extends DynamicTimeSeriesCollection{
  public MilliDTSC(int nSeries, int nMoments, RegularTimePeriod timeSample){
    super(nSeries, nMoments, timeSample);
    if(timeSample instanceof Millisecond)
      this.pointsInTime = new Millisecond[nMoments]
    else if (timeSample instanceof MultipleOfMillisecond)
      this.pointsInTime = new MultipleOfMillisecond[nMoments]
  }
}

MultipleOfMillisecond - the following class:

public class MultipleOfMilliseconds extends Millisecond{
  MulitpleOfMilliseconds(int periodMs){
    this.periodMs = periodMs
  }

  public RegularTimePeriod previous(){
    RegularTimePeriod result = null;
    if(getMillisecond() - periodMs >= FIRST_MILLISECOND_IN_SECOND)
      result = new Millisecond((int)getMillisecond - periodMs, getSecond());
    else{
      Second previous = (Second)getSecond().previous();
      if(previous!=null)
        result = new Millisecond((int)(getMillisecond() - periodMS + LAST_MILLISECOND_IN_SECOND + 1), previous);
    }
    return result;
  }
  // similar for next()
}

I add the sample to the series as follows:

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

I expected that after I set the period to 200 ms, the chart reports on the X label will have more or less 5 time values:

00:00:00.000 00:00:05.000 00:00:10.000 00:00:15.000 00:00:20.000

And I expected 25 patterns in each "space".

, 25 "", X:

00:00:00.000 00:00:00.025 00:00:00.050 00:00:00.075 00:00:00.100

, 1 , 200 .

? , . !!

+2
3

, :

public class MultipleOfMilliseconds extends Millisecond
//                                          ^^^^^^^^^^^

true:

if(timeSample instanceof Millisecond)

, :

if(timeSample instanceof MultipleOfMillisecond)
  this.pointsInTime = new MultipleOfMillisecond[nMoments];
else if (timeSample instanceof Millisecond)
  this.pointsInTime = new Millisecond[nMoments];
+4

, . , . : D

public MilliDTSC(int nSeries, int nMoments, RegularTimePeriod timeSample) {
  super(nSeries, nMoments, timeSample);
  if(timeSample instanceof MultipleOfMillisecond){
    this.pointsInTime = new MultipleOfMillisecond[nMoments];
  }else if (timeSample instanceof Millisecond) {
    this.pointsInTime = new Millisecond[nMoments];
  } 
}

public class MultipleOfMillisecond extends Millisecond {

  private static final long serialVersionUID = 1L;
  private int periodMs = 100;

  public MultipleOfMillisecond(int periodMs){
    super();
    this.periodMs = periodMs;
  }

  public MultipleOfMillisecond(int periodMs, int millisecond, Second second){
    super(millisecond, second);
    this.periodMs = periodMs;
  }

  @Override
  public RegularTimePeriod next() {

    RegularTimePeriod result = null;
    if(getMillisecond() + periodMs <= LAST_MILLISECOND_IN_SECOND){
        result = new MultipleOfMillisecond( periodMs, (int)(getMillisecond() + periodMs), getSecond());
    }else{
        Second next = (Second)getSecond().next();
        if(next != null){
            result = new MultipleOfMillisecond(periodMs, (int)(getMillisecond() + periodMs - LAST_MILLISECOND_IN_SECOND - 1), next);
        }
    }
    return result;

  }

  @Override
  public RegularTimePeriod previous() {

    RegularTimePeriod result = null;
    if(getMillisecond() - periodMs >= FIRST_MILLISECOND_IN_SECOND){
        result = new MultipleOfMillisecond(periodMs, (int)getMillisecond() - periodMs, getSecond());
    }else{
        Second previous = (Second)getSecond().previous();
        if(previous != null){
            result = new MultipleOfMillisecond(periodMs, (int)(getMillisecond() - periodMs + LAST_MILLISECOND_IN_SECOND + 1), previous);
        }
    }
    return result;

  } 
}

10 5 , 500

Now I have 10 samples in 5 seconds, i set the period to 500 ms

+2

Instead, use the original MilliDTSCand Millisecondcall advanceTime()and add the old data as required before adding new data. For example, using 200 ms, follow these steps:

float[] newData = new float[1];
float[] oldData = new float[1];

@Override
public void actionPerformed(ActionEvent e) {
    newData[0] = randomValue();
    oldData[0] = newData[0];
    for (int i = 0; i < 200; i++) {
        dataset.advanceTime();
        dataset.appendData(oldData);
    }
    dataset.appendData(newData);
}

Note that there are now 5 samples per second, spaced 200 ms apart.

enter image description here

+1
source

Source: https://habr.com/ru/post/1794919/


All Articles