我可以将多个ItemRenderer与一个LineSeries一起使用吗?

时间:2013-07-01 11:11:45

标签: flex charts flex4

目前我创建了以下类,它扩展了ItemRenderer,为折线图中的所有数据点添加标签。

package lib

{
    public class LineChartLabelRenderer extends UIComponent implements IDataRenderer, IFactory

    {

        private var _label:Label;

        public var solidColor:SolidColor;

        public function newInstance():*
        {
            return new LineChartLabelRenderer();
        }
        public function LineChartLabelRenderer():void

        {
            super();
            _label = new Label();
            addChild(_label);
            _label.setStyle("color",0x000000);
            _label.setStyle("fontSize",9);
        }

        private var _chartItem:ChartItem;
        public function get data():Object
        {
            return _chartItem;  
        }

        public function set data(value:Object):void

        {
            if (_chartItem == value){
                return;
            }
            _chartItem = ChartItem(value);
            if(_chartItem != null){
                _label.text = LineSeriesItem(_chartItem).yValue.toString();
            }

        }

        override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void

        {

            super.updateDisplayList(unscaledWidth, unscaledHeight);
            var ls:LineSeries = _chartItem.element as LineSeries;
            solidColor = new SolidColor(ls.getStyle("fill"));
            _label.setActualSize(_label.getExplicitOrMeasuredWidth(),20);
            _label.move(unscaledWidth - _label.getExplicitOrMeasuredWidth() / 2 , this.unscaledHeight - _label.getExplicitOrMeasuredHeight() - 5);

        }
    }

}

效果很好。但是,由于LineSeries可能已使用其他ItemRenderer设置,例如CircleItemRendererBoxItemRenderer。我发现没有地方让我用一个LineSeries设置多个ItemRender。我想知道最好的方法是做什么。

1 个答案:

答案 0 :(得分:0)

感谢Sunil D.

最后,我创建了扩展ItemRenderer,如下所示

public class LSLabelCircleItemRenderer extends CircleItemRenderer
    {
        private var _label:Label;
        public function LSLabelCircleItemRenderer():void
        {
            super();
            _label = new Label();
        }
        override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            if(data != null){
                var ls:LineSeries = ChartItem(data).element as LineSeries;
                label.text = LineSeriesItem(data).yValue.toString();
                skin.parent.addChild(label);
                label.setStyle("color",ls.getStyle("fill"));
                label.move(skin.x - label.getExplicitOrMeasuredWidth() / 5,skin.y - label.getExplicitOrMeasuredHeight());
            }
        }       
    }