Book Image

Primefaces Cookbook Second Edition

Book Image

Primefaces Cookbook Second Edition

Overview of this book

Table of Contents (20 chapters)
PrimeFaces Cookbook Second Edition
Credits
Foreword
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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;
}

The visual output will be updated every 2 seconds, and the new model will be rendered with its randomly generated values.

PrimeFaces Cookbook Showcase application

This recipe is available in the demo web application...