Barseries中的Labelfunction以及plotseries

时间:2011-07-29 08:57:34

标签: flex charts labelfunction

我有一张Flec条形图。它有一个Bar系列。我需要在条形图的顶端显示数据标签。为此我使用labelFunction.This工作正常。现在我想在同一个图表中添加一个plotseries。一旦添加,条形系列中的labelfunction就不起作用了。标签不可见。任何想法?谢谢

<mx:Application  xmlns:mx="http://www.adobe.com/2006/mxml">          
  <mx:Script><![CDATA[
    import mx.charts.series.items.PlotSeriesItem;
    import mx.charts.series.items.BarSeriesItem;
    import mx.charts.ChartItem;
    import mx.controls.Label;   
    import mx.collections.ArrayCollection;  
    //Dataprovider for chart    
[Bindable]
 private var medalsAC:ArrayCollection = new ArrayCollection([
{ Country: "USA", Gold: 35, Silver:39, Bronze: 29,prevRank:"1",isIncrease:true },
{ Country: "China", Gold: 32, Silver:17, Bronze: 14,prevRank:"2",isIncrease:false }
 ]);
     private function setCustomLabel(element:ChartItem):String {                  
var data:BarSeriesItem = BarSeriesItem(element);      
 return (data.item.prevRank).toString();    
     }
 public function init(element:Object):String {
                    var data:PlotSeriesItem = PlotSeriesItem(element);                      
                    if(data.item.isIncrease){    
                            return "images/increase.png";
                    }
                    else{
                            return "images/decrease.png";         
                    }          
     }
]]></mx:Script>
 <mx:VBox>
<mx:BarChart id="bar" showDataTips="true"
            dataTipMode="multiple" >           
            <mx:verticalAxis>
                    <mx:CategoryAxis id="v1"  categoryField="Country"  dataProvider="{medalsAC}"/>
                </mx:verticalAxis>
                 <mx:verticalAxisRenderers>
                    <mx:AxisRenderer placement="left" axis="{v1}"
                        verticalAxisTitleAlignment="vertical">
                    </mx:AxisRenderer>                 
                </mx:verticalAxisRenderers>
            <mx:series>
                <mx:BarSeries id="bs2"
                    yField="Country" 
                    xField="Silver"                    
                   dataProvider="{medalsAC}" labelFunction="setCustomLabel"
                />
              <mx:PlotSeries id="plotSeries" yField="Country" xField="Silver"   dataProvider="{medalsAC}"                   
                    displayName="Current Trend">
                    <mx:itemRenderer>
                        <mx:Component>
            <mx:Image source="{outerDocument.init(data)}" horizontalAlign="center" height="16" width="16"/>
                        </mx:Component>
                    </mx:itemRenderer>
             </mx:PlotSeries>
            </mx:series>
        </mx:BarChart>

</mx:VBox>
</mx:Application>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

<mx:Application  xmlns:mx="http://www.adobe.com/2006/mxml">          
    <mx:Script>
        <![CDATA[
            import mx.charts.ChartItem;
            import mx.charts.chartClasses.Series;
            import mx.charts.series.items.BarSeriesItem;
            import mx.charts.series.items.PlotSeriesItem;
            import mx.collections.ArrayCollection;
            import mx.controls.Label;  

        //Dataprovider for chart    
        [Bindable]
        private var medalsAC:ArrayCollection = new ArrayCollection([
            { Country: "USA", Gold: 35, Silver:39, Bronze: 29,prevRank:"1",isIncrease:true },
            { Country: "China", Gold: 32, Silver:17, Bronze: 14,prevRank:"2",isIncrease:false }
        ]);

        private function setCustomLabel(element:ChartItem, series:Series):String
        {                  
            var data:BarSeriesItem = BarSeriesItem(element);      
            return (data.item.prevRank).toString();    
        }
        public function init(element:Object):String 
        {
            var data:PlotSeriesItem = PlotSeriesItem(element);    
            if(data && data.item)
            {
                if(data.item.isIncrease){    
                    return "images/increase.png";
                }
                else{
                    return "images/decrease.png";         
                }     
            }
            return "";
        }
    ]]>
    </mx:Script>
    <mx:VBox>
        <mx:BarChart id="bar" showDataTips="true" dataProvider="{medalsAC}"
                     dataTipMode="multiple" >           
            <mx:verticalAxis>
                <mx:CategoryAxis id="v1"  categoryField="Country"  dataProvider="{medalsAC}"/>
            </mx:verticalAxis>
            <mx:verticalAxisRenderers>
                <mx:AxisRenderer placement="left" axis="{v1}"
                                 verticalAxisTitleAlignment="vertical">
                </mx:AxisRenderer>                 
            </mx:verticalAxisRenderers>
            <mx:series>
                <mx:BarSeries id="bs2"
                              yField="Country" 
                              xField="Silver"        
                              labelPosition="inside"
                              labelFunction="setCustomLabel"
                              />
                <mx:PlotSeries id="plotSeries" yField="Country" xField="Silver" dataProvider="{medalsAC}"                   
                               displayName="Current Trend">
                    <mx:itemRenderer>
                        <mx:Component>
                            <mx:Image source="{outerDocument.init(data)}" horizontalAlign="center" height="16" width="16"/>
                        </mx:Component>
                    </mx:itemRenderer>
                </mx:PlotSeries>
            </mx:series>
        </mx:BarChart>

    </mx:VBox>
</mx:Application>

但是,必须说代码很乱,而你正在使用Flex 3,而Flex 4已经用完了。您应该在单独的类中创建项呈示器,并始终检查空对象,因为它们可以(并且将)创建错误。