Updating live data in charts with polling

The chart component nicely integrates with the <p:poll> component to update itself with an ever-changing data model.

How to do it…

A basic definition for a chart with live data is given here:

<p:poll interval="2" update="live" />
<p:chart id="live" type="pie" model="#{chartBean.livePieModel}"
  style="height:250px" />

The randomly generated model for the chart is implemented here:

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); liveChartModel.setLegendPosition("w"); liveChartModel.setShowDataLabels(true); return liveChartModel; ...

Get PrimeFaces Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.