开发者

Flex 4 - Need help with a custom legend using legendItemClass

I'm creating a custom legend using legendItemClass to highlight each item when the user rolls over, remove it when they roll off and highlight it a little differently when they click. That all works just fine but I also want the pie wedge to explode out when the user click. I have the piece of code but it requires a LegendMouseEvent. I'm also thinking I may be able to re-write the code if I could get the displayName but I'm a loss with that as well. Here's what I have:

Legend:

<mx:Legend id="legend" width="100%" direction="vertical"
  labelPlacemen开发者_如何学JAVAt="right" markerHeight="10" markerWidth="10"
  legendItemClass="CustomPieLegendItem"
  itemClick="explode(event)"
  color="{_color}" />

Explode:

public function explode(event:LegendMouseEvent) : void {
    var len : Number = PieSeries(event.item.source).legendData.length;
    var index : Number = 0;
    var arr:Array = new Array(len);
    if(over){
        event.item.alpha = 0.70;
    } else {
        event.item.alpha = 0.70;
        for(var i : Number = 0; i < len ; i++){
            if(event.item.label == event.item.source.legendData[i].label){
                index = i;
                arr[i] = 0.1;
            }else {
                arr[i] = 0;
            }
        }
        PieSeries(event.item.element).perWedgeExplodeRadius = arr;
    }
}

Class:

package
{
  import custom.charts;

  import flash.display.Sprite;
  import flash.events.MouseEvent;

  import ilog.core.ilog_internal;

  import mx.charts.LegendItem;
  import mx.charts.events.LegendMouseEvent;
  import mx.charts.series.PieSeries;
  import mx.charts.series.items.PieSeriesItem;

  public class CustomPieLegendItem extends LegendItem {
    public function CustomPieLegendItem(){
      super();
    }

    private var mouseSprite:Sprite;

    private var state:int=0;
    private static var IDLE_STATE:int=0;
    private static var OVER_STATE:int=1;
    private static var SELECTED_STATE:int=2;


    public function get selected():Boolean{
      return (state == OVER_STATE || state == SELECTED_STATE);
    }

    public function set selected(value:Boolean):void {
      if (value)
        state=SELECTED_STATE;
      else
        state=IDLE_STATE;
        invalidateDisplayList();
    }

    private function clickHandler(e:MouseEvent):void {
        charts(document).resetSeries();
        state=SELECTED_STATE;
        // returns the item USED for the displayName, not the actual name
        trace((element as PieSeries).displayName);
        // does not work because I need to pass a LegendMouseEvent
        charts(document).explode("NEEDS LegendMouseEvent HERE");
        invalidateDisplayList();
    }

    private function overHandler(e:MouseEvent):void {
      if (state != SELECTED_STATE){
        state=OVER_STATE;
        invalidateDisplayList();
      }
    }

    private function outHandler(e:MouseEvent):void {
      if (state != SELECTED_STATE){
        state=IDLE_STATE;
        invalidateDisplayList();
      }
    }

    override protected function createChildren():void {
        super.createChildren();
        mouseSprite=new Sprite();

        addChild(mouseSprite);
        mouseSprite.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
        mouseSprite.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
        mouseSprite.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
    }

    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
      super.updateDisplayList(unscaledWidth, unscaledHeight);
      mouseSprite.graphics.clear();
      mouseSprite.graphics.beginFill(0, 0);
      mouseSprite.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
      graphics.clear();
      if (state == OVER_STATE || state == SELECTED_STATE)
      {
        graphics.beginFill(0xCCCCCC, 0.2);
        if (state == SELECTED_STATE)
          graphics.lineStyle(1, 0xCCCCCC);
          graphics.drawRect(-2, 0, width+2, height);
      }
    }
  }
}


Why don't you just create a new LegendMouseEvent using below code new LegendMouseEvent(LegendMouseEvent.ITEM_CLICK, e, this) and pass it to explode. you can use nay types from LegendMouseEvent class , e - MouseEvent passed to clickhandler

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜