Quantcast
Channel: Ashish Awasthi's Blog
Viewing all articles
Browse latest Browse all 165

Get selected slice of dvt:pieChart using custom selection listener in ADF

$
0
0
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 managed bean that will be called whenever 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 dataSelection attribute. Selection can be processed using selectionListener on the server or the selection event type on the client.

dataSelectionStringYesValid Values: none, single, multiple
Default Value: none

Specifies the data selection mode for the chart. Valid values are "none" (Default), "single", and "multiple". If selection is "multiple", marquee selection will be enabled for non-pie charts.

See the steps to implement selection listner -


  • 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 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


  • importjavax.el.ELContext;

    importjavax.el.ExpressionFactory;

    importjavax.el.MethodExpression;
    importjavax.el.ValueExpression;

    importjavax.faces.context.FacesContext;


    importoracle.jbo.Row;

    importorg.apache.myfaces.trinidad.event.SelectionEvent;

    publicclassPieChartSelectionBean{
    publicPieChartSelectionBean(){
    }

    /**
    * 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
    */
    publicstatic 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
    */
    publicstatic 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
    */
    publicvoidpieChartSelectionListener(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-
    It's Employee name appears on log



All Done :) Sample ADF Application- Download
Cheers :) Happy Learning

Viewing all articles
Browse latest Browse all 165

Trending Articles