Efficient way to update live graphs

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
greearb
Posts: 3
Joined: Sun Aug 27, 2006 5:53 pm
Location: Ferndale, WA, USA
Contact:

Efficient way to update live graphs

Post by greearb » Thu Apr 02, 2020 5:25 pm

I belatedly re-discovered the fact that I have to update data series inside the main swing thread,
and while thinking about this, I believe that my current solution could be a lot more efficient.
What I would like to do is to efficiently (code wise and execution wise) disable data-changed events,
update all of the series, and then tell the charts to re-draw (and/or do whatever else they need to
become current after missing all of the previously disabled data-changed events.

As a secondary question: Assuming we can disable the data-changed events, could we then
update series and data-collection objects from a non-swing thread safely?

Any suggestions for ways to go about this are welcome.

Thanks,
Ben

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Efficient way to update live graphs

Post by John Matthews » Thu Apr 02, 2020 5:42 pm

Update the chart's dataset in your implementation of SwingWorker::process, illustrated here. From the SwingWorker::publish API, "multiple invocations to the publish() method might occur before the process() method is executed. For performance purposes all these invocations are coalesced into one invocation with concatenated arguments." The worker itself limits updates to a sustainable rate, and you can fine tune bulk updates in your process() method.

greearb
Posts: 3
Joined: Sun Aug 27, 2006 5:53 pm
Location: Ferndale, WA, USA
Contact:

Re: Efficient way to update live graphs

Post by greearb » Thu Apr 02, 2020 5:54 pm

From what I can tell, when you add a single datapoint to a series, the data-changed event is generated, which causes a lot of work. I may be adding 1000+ datapoints all at once, so I'd rather the graph wait until all 1000 data are updated and only then deal with the redraw, bounds checking, and other work caused by the data-changed events.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Efficient way to update live graphs

Post by John Matthews » Fri Apr 03, 2020 12:53 am

The List<V> passed to process() affords you the opportunity to cache an arbitrary number of values of type V for a bulk update. Extending a suitable subclass of AbstractDataset will allow you to control when to notify listeners.

Locked