我认为饼图组件总是显示数据源中的所有项目。
我将处理 ArrayList 的更改事件并定义另一个列表,该列表不包含任何空元素。
像这样的东西(这里是一个工作示例):
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600" creationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
import mx.events.FlexEvent;
[Bindable]private var shareData:ArrayCollection = new ArrayCollection(
[
{name: "A", share: 20},
{name: "B", share: 50},
{name: "C", share: 0},
{name: "D", share: 10}
]);
[Bindable]private var shareDataDP:ArrayCollection = new ArrayCollection();
protected function init(event:FlexEvent):void
{
shareData.addEventListener(CollectionEvent.COLLECTION_CHANGE, onCollectionChange);
updateDP();
}
private function onCollectionChange(evt:CollectionEvent):void
{
updateDP();
}
private function updateDP():void
{
shareDataDP.removeAll();
for each (var item:Object in shareData)
{
if (item.share != 0)
{
shareDataDP.addItem(item);
}
}
}
]]>
</fx:Script>
<s:VGroup left="20" top="20">
<s:HGroup verticalAlign="bottom">
<s:Label text="Value for C: "/>
<s:TextInput id="valueC" text="0" width="40" restrict="0-9"/>
<s:Button label="Change" click="{shareData.getItemAt(2).share = int(valueC.text); shareData.refresh()}"/>
</s:HGroup>
<s:HGroup>
<s:Group width="300" height="300">
<mx:PieChart id="sharesDistribution" dataProvider="{shareData}" showDataTips="true"
height="100%" width="70%" left="0" right="0" bottom="0" top="0">
<mx:series>
<mx:PieSeries field="share" nameField="name" labelPosition="insideWithCallout" />
</mx:series>
</mx:PieChart>
<mx:Legend right="0" width="30%" direction="horizontal" dataProvider="{sharesDistribution}" />
</s:Group>
<s:Group width="300" height="300">
<mx:PieChart id="sharesDistributionDP" dataProvider="{shareDataDP}" showDataTips="true"
height="100%" width="70%" left="0" right="0" bottom="0" top="0">
<mx:series>
<mx:PieSeries field="share" nameField="name" labelPosition="insideWithCallout" />
</mx:series>
</mx:PieChart>
<mx:Legend right="0" width="30%" direction="horizontal" dataProvider="{sharesDistributionDP}" />
</s:Group>
</s:HGroup>
</s:VGroup>
</s:Application>