Lahiru Chandima
Lahiru Chandima

Reputation: 24118

Vaadin Charts: Pie chart won't update dynamically

I created a Vaadin pie chart with following code.

Chart chart = new Chart(ChartType.PIE);
DataSeries dataSeries = new DataSeries("Logins");
chart.getConfiguration().setSeries(dataSeries);

I need to update my chart dynamically. I tried following (this code gets executed whenever new data is available).

adding new item:

dataSeries.add(new DataSeriesItem("New item", value), true, false);

updating existing item:

DataSeriesItem dataSeriesItem = dataSeries.get(0);
dataSeriesItem.setY(newValue);
dataSeries.update(dataSeriesItem);

But none of the above worked.

The only solution I could find is clearing the chart (chart.clear()), re, populating the data series and re drawing the chart (chart.drawChart()).

This method is not optimal since it re-draws the chart, and also the selection in the chart gets lost.

Does pie chart support dynamic updating? Can anyone suggest a way to fix this?

Upvotes: 0

Views: 1160

Answers (4)

code_mechanic
code_mechanic

Reputation: 1148

Late to the question, but I encountered similar problem in 2020, I solved it using below code:

dataSeries.clear();
dataSeries.add(new DataSeriesItem("Expense", 120));
dataSeries.add(new DataSeriesItem("Saving", 20));
dataSeries.add(new DataSeriesItem("Balance", 100));
chart.getConfiguration().setSeries(dataSeries);
chart.drawChart();

Upvotes: 0

Weles
Weles

Reputation: 1275

I know this question is old, but I will share how I solved this problem to Chart Pie in DChart 1.7.0.

DataSeries dataSeries = new DataSeries(); 

dataSeries.newSeries();
dataSeries.add( "Food", 500);
dataSeries.add( "Clothes", 1000);

chart.setDataSeries(dataSeries);
chart.show(); //this will update the chart in the browser;

Upvotes: 0

Youness
Youness

Reputation: 2080

I had the same problem and fixed it with chart.drawChart() after updating the data series in configurations (chart.getConfiguration.setSeries(newSeries)). In my case (vaadin version 7.6.3), chart.clear() was not required.

There are other more complicated ways like vaadin-push plugin as well which I didn't find necessary for my case.

Upvotes: 0

Víctor Gómez
Víctor Gómez

Reputation: 722

Try to call chart.getConfiguration().addSeries(dataSeries) before updating items. If that doesn't work, I think you should re-draw your chart.

Upvotes: 0

Related Questions