First of all, wishing a very Happy New Year to all of you, New Year is like a blank page, fill it with happiness and good memories. This post is about a simple requirement – How to get selected slice value of dvt:pieChart?
So for this, we have to create a custom selection Listener in the managed bean that will be called whenever a user selects any slice of pieChart.
If you see documentation of pieChart it tells about two properties –
Selection of data items can be enabled using the data selection attribute. Selection can be processed using selectionListener on the server or the selection event type on the client.
dataSelection | String | Yes | Valid Values: none, single, multiple Default Value: noneSpecifies the data selection mode for the chart. Valid values are “none” (Default), “single”, and “multiple”. If the selection is “multiple”, marquee selection will be enabled for non-pie charts. |
See the steps to implement selection listener –
-
- Create a Fusion Web Application using Employees table of HR Schema
-
- Create a page and drop Employees viewObject as pieChart on page
-
- Select pieChart in structure window and in property inspector set DataSelection property to single and create a Selection Listener in the managed bean
-
- Copy value property of pieChart, it’ll be like this- #{bindings.Employees1.collectionModel} Now see code in selection listener that sets selected row as current row and then from iterator we can get current row and from that row we can get all attributes
import javax.el.ELContext; import javax.el.ExpressionFactory; import javax.el.MethodExpression; import javax.el.ValueExpression; import javax.faces.context.FacesContext; import oracle.jbo.Row; import org.apache.myfaces.trinidad.event.SelectionEvent; public class PieChartSelectionBean { public PieChartSelectionBean() { } /** * Programmatic invocation of a method that an EL evaluates to. * * @param el EL of the method to invoke * @param paramTypes Array of Class defining the types of the parameters * @param params Array of Object defining the values of the parametrs * @return Object that the method returns */ public static Object invokeEL(String el, Class[] paramTypes, Object[] params) { FacesContext facesContext = FacesContext.getCurrentInstance(); ELContext elContext = facesContext.getELContext(); ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory(); MethodExpression exp = expressionFactory.createMethodExpression(elContext, el, Object.class, paramTypes); return exp.invoke(elContext, params); } /** * Programmatic evaluation of EL. * * @param el EL to evaluate * @return Result of the evaluation */ public static Object evaluateEL(String el) { FacesContext facesContext = FacesContext.getCurrentInstance(); ELContext elContext = facesContext.getELContext(); ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory(); ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class); return exp.getValue(elContext); } /**Custom Selection Listener for dvt:pieChart * @param selectionEvent */ public void pieChartSelectionListener(SelectionEvent selectionEvent) { // Makes selected slice as current row invokeEL("#{bindings.Employees1.collectionModel.makeCurrent}", new Class[] { SelectionEvent.class }, new Object[] { selectionEvent }); // Get the selected row (Use pie chart iterator name) Row selectedRow = (Row) evaluateEL("#{bindings.Employees1Iterator.currentRow}"); // get the current selected row // Get any attribute from selected row System.out.println("Selected Employee is-" + selectedRow.getAttribute("FirstName")); } }
-
- Run and check application, pie looks like this
Select any slice-
Its Employee name appears on the log
- Run and check application, pie looks like this
All Done 🙂 Sample ADF Application- Download
Cheers 🙂 Happy Learning
Thanks ashish, really helpful article. For example, in pie chart, the dataSelection = 'mulitiple', how do we get all the values selected?
Hi Pavan
You can iterate over SelectedRowKeys of pie chart using this code
RowKeySet selectedEmps = getPieCartBind().getSelectedRowKeys();
Iterator selectedEmpIter = selectedEmps.iterator();
DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding empIter = bindings.findIteratorBinding("Employees1Iterator");
RowSetIterator empRSIter = empIter.getRowSetIterator();
while (selectedEmpIter.hasNext()) {
Key key = (Key) ((List) selectedEmpIter.next()).get(0);
Row currentRow = empRSIter.getRow(key);
System.out.println(currentRow.getAttribute("FirstName"));
}
In adf jdeveloper 11g release 1 instead of showing collectionModel it is showing me graphModel; therefore I think that the code is not working. Please help me out for this.
Hi Vigya
Check this for 11g graphModel code How to get Selected graph point details in ADF Graphs
Ashish
Thank you sir, but still I am facing an error…
ADF_FACES-60096:Server Exception during PPR, #1
javax.el.PropertyNotFoundException: Target Unreachable, identifier 'BarGraph' resolved to null
at com.sun.el.parser.AstValue.getTarget(Unknown Source)
at com.sun.el.parser.AstValue.invoke(Unknown Source)
at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)
Please tell me why is this error coming and how can I resolve it.
and what is this "BarGraph" in your code ?
bean class name
You need to share your graph xml source , Make sure bean is registered with taskFlow and accessible on page
This comment has been removed by the author.
Hi Ashish
My graph Value Propery has "#{bindings.Employee1.graphModel}" instead of "#{bindings.Employee1.collectionModel}". I'm using ADF 11.1.2.4.0. I don't see "collectionModel" when I expand the ADF Bindings tree.
Thanks.
Fede.
Hi Federico
Check link i have shared in above comments
Ashish