1 Reply Latest reply on Aug 1, 2007 1:52 PM by Kenny Yates

    Dispatching an ItemClick event from a chart

    JoeADSK
      I am trying to provide a simple drill down by getting a name from a pie slice and then putting the slices contents in a datagrid based on that name as a filter.

      I am trying to use an event dispatcher and an event listener, but im not exactly sure im doing it right.
      The dispatcher would dispatch when something is clicked, and the string item of what is clicked would be passed into the
      remote object. (Not sure how the ItemClick string is passed by an event)
      I know i'm a newb with events.... thanks for any input.

      here is the chart component

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Panel xmlns:mx=" http://www.adobe.com/2006/mxml" title="Defect Chart (Status not Closed)" layout="absolute" width="100%" height="100%">

      <mx:Script>
      <![CDATA[
      import mx.charts.ChartItem;
      import mx.collections.ArrayCollection;


      //Label function for the Defect Pie Chart
      private function labels(dataItem:Object, field:String ,Index:int, dataPercent:Number ):String
      {
      return dataItem.BG_SEVERITY + " " + dataItem.CNT;

      }

      private function ItemClick(name:String):void
      {
      dispatchEvent(new ChartItemEvent.);
      trace("event is dispatched");
      }
      ]]>
      </mx:Script>



      <mx:PieChart id="DefectPie"
      width="100%"
      height="100%"
      showDataTips="true"
      y="40"
      itemClick="{ItemClick(event.hitData.chartItem.item.toString())}"
      >

      <mx:series>
      <mx:PieSeries id="series"
      field="CNT"
      nameField="BG_SEVERITY"
      labelPosition="callout"
      labelFunction="labels"
      >





      This is called in main's init()
      this.addEventListener("name" , ItemClickHandler);

      And this is the handler function:

      private function ItemClickHandler(event:Event):void
      {
      trace(event);
      dataManager.soe_bug_list_detail(event['BG_SEVERITY']);
      }
        • 1. Re: Dispatching an ItemClick event from a chart
          Kenny Yates
          JoeADSK,

          This might sound like a bit over-kill but you might go ahead and use a seperate EventHandler class and a DTO(Data Transfer Object) class to accomplish the event even though you are simply sending 1 piece of data.

          Here is an example if you need one:

          ==Event Handler Class==
          package event
          {
          import flash.events.Event;
          import myApp.dto.ChartItemDTO;

          public class ChartClickEvent extends Event
          {
          public var chartName:ChartItemDTO;

          public function ChartClickEvent(chartName:ChartItemDTO, type:String)
          {
          super(type);
          this.chartName = chartName;
          }

          override public function clone():Event
          {
          return new ChartClickEvent(chartName, type);
          }
          }
          }

          DTO Class:

          package myApp.dto
          {
          public class ChartItemDTO
          {
          public var name:String;

          public function ChartItemDTO(name:String)
          {
          this.name = name;
          }
          }
          }

          Here is how you use it in your mxml for or whatever:

          import event.ChartClickEvent;
          import myApp.dto.ChartItemDTO;

          DONT FORGET TO ADD THIS EVENT BLOCK:
          <mx:Metadata>
          [Event(name="chartName", type="event.ChartClickEvent")]
          </mx:Metadata>

          HERE IS AN EXAMPLE FUNCTION NON-WORKING===

          private function setChartName(name:String):void
          {
          var chrtName:ChartItemDTO = new ChartItemDTO(name);
          var chrtClickEvent:ChartClickEvent = new ChartClickEvent(chrtName, "chartName");
          dispatchEvent(chrtClickEvent);
          }

          Now this is just hacked together quickly not a working model but you will get the idea.
          Basically the event must be passed a Data Transfer Object to hand off from the Click event.

          Like I said this is over-kill but a good place to start as you venture forth in the the Event Handling world in Flex.

          Good luck, hope this helps,
          Kenny