Tuesday, December 6, 2011

DVT Graphs Event Handlers

While writing event handling code for graphs, we need to take care of the following hierarchy:



That's because different graphs click events return different type of ComponentHandle references.
For example to handle Pie Graph's click event, we need to write the code similar to the following code snippet:

    public void pieClick(ClickEvent event) {
        // Add event code here...
        ComponentHandle handle = event.getComponentHandle();
        String seriesName = null;
        String seriesValue = null;
        Long clickedValue = null;
       
        if (handle instanceof DataComponentHandle){
                DataComponentHandle dhandle = (DataComponentHandle)handle;
                clickedValue = (Long)dhandle.getValue(DataComponentHandle.UNFORMATTED_VALUE);
                // Get the series attributes
                Attributes [] seriesInfo = dhandle.getSeriesAttributes();
                if(seriesInfo != null)
                {
                    for(Attributes attrs: seriesInfo)
                    {
                        seriesName = attrs.getValue(Attributes.LABEL_ATTRIBUTE).toString();
                        if(seriesName.equals("Status"))
                            seriesValue = attrs.getValue(Attributes.LABEL_VALUE).toString();   
                       
                    }
                }
        }
    . . .
    }   

Whereas while handling Line Graph's click event, the event handler looks like :

    public void graphClicked(ClickEvent clickEvent) {
          // Add event code here...
          ComponentHandle handle = clickEvent.getComponentHandle();
          if (handle instanceof SeriesComponentHandle) {
                  // Get the series attributes
                  Attributes [] seriesInfo = ((SeriesComponentHandle)handle).getSeriesAttributes();
                  String data = "";
                  if(seriesInfo != null) {
                          for(Attributes attrs: seriesInfo) {
                                  data += "Series value: " + attrs.getValue(Attributes.LABEL_VALUE);
                                  data += " Series name: " + attrs.getValue(Attributes.LABEL_ATTRIBUTE);
                                  data += " Series value id: " + attrs.getValue(Attributes.ID_VALUE);
                                  data += " Series name id: " + attrs.getValue(Attributes.ID_ATTRIBUTE);
                          }
                  }
          }
      . . .
    }

For Pie Graph, we look for DataComponentHandle to fetch desired attribute values, whereas for Line Graph, we look for SeriesComponentHandle.

Event handlers for other graphs are listed in the above image.

No comments:

Post a Comment