vaadinvaadin-flowvaadin-chartsvaadin20

Vaadin: How to update a chart's data?


I am difficulties finding a way to update a chart with new data. One adds data to a chart using:

Configuration conf = chart.getConfiguration();
conf.addSeries(series);

But there is no corresponding "remove(series)"!?! Thus, if I want to update the data being displayed and add another series I actually add another line (or bar-graph or whatever visualization I have chosen) to the chart, which is clearly NOT what I want/need.

My first attempt to overcome that was to get hold of the old data series and remove them from the chart, but with getSeries() I am only getting a non-modifyable copy of the existing series, so that attempt ended in a java.lang.UnsupportedOperationException.

Thus my question: how do I convince an existing chart to remove all old data series? Don't tell me, that I need to recreate the entire chart-component each time, please!


Solution

  • You didn't mention any versions, but at least it used to be possible to remove items from the DataSeries itself. So instead of removing the series, you can just empty and repopulate it. Save the series reference when you set it. Alternatively you can e.g. create a new ArrayList from the unmodifiable list, make your changes there, and set the series again (using setSeries, not addSeries). Then calling chart.drawChart(); should trigger the re-draw.