The chart
component offers AJAX behavior events to interact with the chart by item selection.
A basic definition for a chart with <p:ajax>
bundled inside is given here:
<p:chart type="bar" id="withAjax" model="#{chartBean.barModel}" style="height:250px"> <p:ajax event="itemSelect" listener="#{chartBean.itemSelect}" update="growl" /> </p:chart>
The itemSelect
method retrieves an instance of org.primefaces.event.ItemSelectEvent
, which enables us to access the item index and series index of the selected chart item. The usage of the itemSelect
method is given here:
public void itemSelect(ItemSelectEvent event) { MessageUtil.addInfoMessageWithoutKey("Item selected", "Item Index:" + event.getItemIndex() + ", Series Index: " + event.getSeriesIndex()); }
This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.
The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter9/chartInteraction.jsf
.
18.117.137.12