Wednesday, 27 February 2008

flex: a single object as a dataProvider for a chart

I'm still really enjoying Flex and 3.0 (and Air 1.0) has officially been released now. After working with Vince, my relatively new colleague, I can see that I'm still in the learning curve for Flex, though I'm confident I could hold my own and will catch up pretty quickly.

This evening I'm tinkering with my "webwar" game again. I decided to use a ColumnChart instead of a form and labels (ahem, very developerish!) to show the status of "resources". The problem I was having was that my resources are expressed as properties on a single object:

[Bindable]
public class Resources
{
public var earth:int = 0;
public var fire:int = 0;
public var metal:int = 0;
public var water:int = 0;
public var wood:int = 0;
}


The dataProvider for a ChartBase subclass has to implement the IList or ICollectionView interface which obviously Object does not. I played around with various bindings, setting the dataProvider on the ColumnSeries instead of the chart, with little success. Eventually I came up with this, which seems to work, but I'm not sure if it's the most appropriate method or not (will check with Vince tomorrow!)


<mx:ArrayCollection id="chartData">
<mx:Object
Resource="Earth"
Value="{resources.earth}"
/>
<mx:Object
Resource="Fire"
Value="{resources.fire}"
/>
<mx:Object
Resource="Metal"
Value="{resources.metal}"
/>
<mx:Object
Resource="Water"
Value="{resources.water}"
/>
<mx:Object
Resource="Wood"
Value="{resources.wood}"
/>
</mx:ArrayCollection>


So rather than setting the object as the dataProvider, I set this array collection as the dataProvider and bind the elements of the Resource object to my custom collection.

So apart from that, we're still doing some cool stuff with Flex and OSGi at work and we will have an alpha release of our Solstice framework this week! Promise.

No comments: