从flex中的对象数组构建堆栈条形图

时间:2010-04-21 15:52:48

标签: flex charts bar-chart

我有一个动态ArrayCollection,其中包含未知数量的MyObj类型的对象:

class MyObj
{
   type:String
   value:long
}

每个MyObj对象都有不同的type值。

如何从此数组构建单个堆叠条形图,其中堆积条形的每个部分代表MyObj的对象(代表type),其长度为value

1 个答案:

答案 0 :(得分:0)

检查此代码:

<?xml version="1.0" encoding="utf-8"?>

<mx:Script>
    <![CDATA[

    import mx.collections.ArrayCollection;

    [Bindable]
    private var countries:ArrayCollection = new ArrayCollection( [
        { Country: "Romania", Romanians: 0.7, Hungarians:0.2, Germans: 0.1 }]);
    ]]>
</mx:Script>

<mx:Panel title="BarChart Control" layout="horizontal" color="0xffffff" borderAlpha="0.15" width="600" height="240"
     paddingTop="10" paddingRight="5" paddingBottom="10" paddingLeft="5" horizontalAlign="center">

     <mx:BarChart id="bar" height="100%" color="0x323232" type="stacked"
         showDataTips="true" dataProvider="{countries}">

        <mx:verticalAxis>
            <mx:CategoryAxis categoryField="Country"/>
        </mx:verticalAxis>

        <mx:series>
            <mx:BarSeries yField="Country" xField="Romanians" displayName="Romanians"/>
            <mx:BarSeries yField="Country" xField="Hungarians" displayName="Hungarians"/>
            <mx:BarSeries yField="Country" xField="Germans" displayName="Germans"/>
        </mx:series>
    </mx:BarChart>

    <mx:Legend dataProvider="{bar}" color="0x323232"/>

</mx:Panel>