2010-06-09 37 views
0

我已經研究了用GroupingCollections和AdvancedDataGrids分組XML數據,但我無法弄清楚如何在圖表中顯示該數據。如何在Flex餅圖中顯示分組的XML數據?

基本上,我想要做的是按類別字段分組數據,這應該給我兩行下紅色,一個下藍色,一個下綠色。將此數據輸入到餅圖時,它應占用適當的空間量(紅色爲1/2,藍色和綠色爲1/4)。我不需要other_data字段,因爲我想使用組名稱(在本例中爲類別)作爲標註。

有什麼建議嗎?

的樣本數據:

<row> 
    <category>red</category> 
    <other_data>this shouldn't really matter</other_data> 
</row> 
<row> 
    <category>blue</category> 
    <other_data>this shouldn't really matter</other_data> 
</row> 
<row> 
    <category>red</category> 
    <other_data>this shouldn't really matter</other_data> 
</row> 
<row> 
    <category>green</category> 
    <other_data>this shouldn't really matter</other_data> 
</row> 
+0

@Mike我能夠將數據拉入GroupingCollection並將其輸出到AdvancedDataGrid中(並且它已正確分組),但是當我試圖定義圖表的dataSource屬性時,它會出錯並且不顯示。 – mclaughlinj 2010-06-10 16:00:57

回答

1

我會拍攝。有可能是一個更優雅的方式,或者一些效率,但是......

把你的XML轉換對象

的ArrayCollection {類:「紅」,other_data:「不要緊」} 。 。 。

從那裏......

var categoryGroup:GroupingCollection=new GroupingCollection(); 
categoryGroup.source=ac; // you're ArrayCollection 

var grouping:Grouping=new Grouping(); 
var groupingField:GroupingField=new GroupingField("category"); 
grouping.fields=[groupingField]; 

categoryGroup.grouping=grouping; 
categoryGroup.refresh(); 

var categoryAC:ArrayCollection=categoryGroup.getRoot() as ArrayCollection; 

chart.dataProvider=categoryAC; 

除此之外,你必須做在圖表上一些神奇......

<mx:PieChart id="chart" height="100%" width="100%"> 
    <mx:series> 
     <mx:PieSeries dataFunction="myDataFunction" labelFunction="myLabelFunction" labelPosition="callout"/> 
    </mx:series> 
</mx:PieChart> 

你的圖表功能處理

public function myDataFunction(series:Series, item:Object, fieldName:String):Object 
{ 
    return (item.children.length); 
} 

public function myLabelFunction(data:Object, field:String, index:Number, percentValue:Number):String 
{ 
    return data.GroupLabel; 
} 

這可能有點過於沉重,但它會做到這一點,並且可以在某種程度上擴展到其他方案。