Updating live data in charts with polling
It's possible to update the chart in a timely manner with the help of the <p:poll>
component.
How to do it...
The following definition updates the pie chart every 3 seconds:
<p:poll interval="3" update="live" /> <p:pieChart id="live" value="#{pieChartController.livePieModel}" style="height:250px" />
The data model definition with the getter method, which will randomly create chart data, is given as follows:
PieChartModel liveChartModel = new PieChartModel(); liveChartModel.set("Candidate 1", 500); liveChartModel.set("Candidate 2", 300); public PieChartModel getLivePieModel() { int random1 = (int)(Math.random() * 1000); int random2 = (int)(Math.random() * 1000); liveChartModel.getData().put("Candidate 1", random1); liveChartModel.getData().put("Candidate 2", random2); return liveChartModel; }
PrimeFaces Cookbook Showcase application
This recipe is available in the PrimeFaces Cookbook Showcase application on GitHub at https...