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

Creating line, area, bar, and pie charts


PrimeFaces offers one base component named chart, which provides different charting according to the provided type attribute. In this recipe, we will create line, area, bar, and pie charts using this component.

How to do it…

A basic definition of a line chart with two series of data is given here:

<p:chart type="line" model="#{chartBean.model}"
  style="height:250px" />

The model defined for the line chart is given here:

private LineChartModel createLineModel() {
  LineChartModel model = new LineChartModel();
  LineChartSeries sales = new LineChartSeries();
  sales.setLabel("Sales");
  sales.set(2004, 1000);
  sales.set(2005, 1170);
  sales.set(2006, 660);
  sales.set(2007, 1030);

  LineChartSeries expenses = new LineChartSeries();
  expenses.setLabel("Expenses");
  expenses.set(2004, 400);
  expenses.set(2005, 460);
  expenses.set(2006, 1120);
  expenses.set(2007, 540);

  model.addSeries(sales);
  model.addSeries(expenses);
  model.setTitle("Company...