3

I'm trying to define an applet with a chart that have to be updated every n milliseconds. For example every 500 milliseconds. This is a part of the code:

dataSet = new DynamicTimeSeriesCollection(1, 200, new Millisecond());
dataSet.setTimeBase(new Millisecond());

When I launch the application it returns me a NullPointerException raised by the second line. If I replace Milliseconds with Seconds it works.

The question is: how can I set a period of n milliseconds without having exceptions?

Thanks

Maverik
  • 2,358
  • 6
  • 34
  • 44

1 Answers1

4

It looks like pointsInTime is not being initialized for Millisecond, but you can do so in a subclass constructor:

private static 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];
        }
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • So, is that a JFreeCharts bug, then? – Adrian Petrescu Jul 27 '11 at 20:39
  • 1
    Arguably, yes; the comment in the source suggests more of a _todo_. – trashgod Jul 27 '11 at 20:45
  • I will try it tomorrow morning at the office. How can I set a 500 milliseconds period? Does it depend on the frequency with which I add new data? – Maverik Jul 27 '11 at 20:59
  • 1
    It works! Thanks a lot !!! Now I only have the doubts about the period of 500 ms. – Maverik Jul 27 '11 at 21:12
  • @trashgod. You are perfectly right. As I said in the question if I use seconds insted milliseconds it works. Anyway the solution proposed in this answer works. – Maverik Jul 27 '11 at 21:15
  • You can `advanceTime()` as needed; see also this [example](http://stackoverflow.com/questions/5048852/using-jfreechart-to-display-recent-changes-in-a-time-series/5048863#5048863). – trashgod Jul 27 '11 at 21:16
  • Sorry, it will be the last question ;) I looked the example and i used it as guide when I started building my application. It seems that the period 'x' value of a sample in a series depends on the time in which I add it. It's correct? If a will ad A sample at the time 0.1 sec and another one at the time 0.3 sec then 0.1 and 0.3 will be the 'x' values of the two samples? Thanks – Maverik Jul 27 '11 at 21:27
  • 2
    Ah, I think you can use `addValue()` to get the effect you want. – trashgod Jul 27 '11 at 21:48
  • Yes! That's what i tried to explain in the previous comment ;) Thanks – Maverik Jul 27 '11 at 21:53
  • Mmm...unfortunately it doesn't work...if I add the data every 500 milliseconds the gap between two samples in the chat remains 1 ms. How can I manage it? I need at least to set the period on the x values to a multiple of 1 ms, for example 500ms. – Maverik Jul 28 '11 at 07:16
  • I did that: dataset = new MilliDTSC(new Millisecond()); dataset.setBaseTime(new Millisecond); I add samples every 500 ms but the times reported on the X axis are not consistent. For example I see 10:00:00.000 and the next one is 10:00:00:025. I have 25 samples in this space, one for each millisecond. What I expected is to have 10:00:00:000 and 10:00:12:500. – Maverik Jul 28 '11 at 08:35
  • I'm tryng to extend the class Millisecond and overriding the methods next and previous in such a way that they return a t + 500ms value and a t-500ms value. But it doesn't work...I'm missing something – Maverik Jul 28 '11 at 08:38
  • @trashgod Just for completeness this is what I do when I add a sample: `dataSet.advanceTime()` and `dataset.appendData(newData)`. – Maverik Jul 28 '11 at 09:00
  • This is a little hard to follow in comments. You might want to put put together an [ssce](http://sscce.org/) and pose a new question. – trashgod Jul 28 '11 at 09:09
  • @trashgod I did it [here](http://stackoverflow.com/questions/6856979/jfreechart-dynamictimeseries-with-period-of-n-milliseconds) – Maverik Jul 28 '11 at 09:46