PDA

View Full Version : Adding items to ArrayCollection with variables


Ironwil616
07-14-2010, 11:26 PM
I'm fairly new to ActionScript, and all of the examples I could find showed ArrayCollections being populated with static content, which isn't very useful. I have set up a HTTPService in Flex that grabs some XML content to be used in Flex charts. I really just need the values of a couple items, which I plan to assign to corresponding values in the ArrayCollection. Here's the ArrayCollection and other relevant code:


[Bindable]
private var onTime:Number;
[Bindable]
private var late:Number;

private function ServiceResult(event:ResultEvent):void
{
projectData = XML(event.result);

onTime = projectData.project[0].@ontime;
late = projectData.project[0].@late;
}

[Bindable]
public var projects:ArrayCollection = new ArrayCollection([
{Project:"On Time", Percentage:"{onTime}"},
{Project:"Late", Percentage:"{late}"}
]);


I've tried several things, but getting the variables to resolve right there in the ArrayCollection initialization doesn't seem to be working. If I replace "{onTime}" with a number and do the same with the other variable, all works just fine. How can I use variables to define values for the ArrayList item(s)?

92Garfield
07-14-2010, 11:36 PM
I'm not sure what you want, but this might be it.

public var projects:ArrayCollection = new ArrayCollection([
{Project:"On Time", Percentage: onTime},
{Project:"Late", Percentage: late}
]);

Ironwil616
07-15-2010, 09:01 PM
Unfortunately, that doesn't work. If I just put the names of the variables (I tried that first yesterday), then the chart simply doesn't render. This isn't an issue of the variables not having the values assigned to them. I've stepped through it in the debugger, and the variables are getting the values from the HTTPService as expected. The problem also isn't the chart code (aside from the variables). For instance, this works:


[Bindable]
public var projects:ArrayCollection =
new ArrayCollection([
{Project:"On Time", Percentage:50},
{Project:"Late", Percentage:50}
]);


But if I replace the '50' in each ArrayCollection item with the name of the variable, the chart doesn't render. I can't figure out why yet. Here is the entire code for the page. Maybe someone can see the issue with everything included.


<?xml version="1.0"?>
<!-- charts/BasicPie.mxml -->
<mx:Application xmlns:mx="url ommitted due to forum rules"
creationComplete="this.chartService.send()">

<mx:Script>

<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;

[Bindable]
private var onTime:Number;
[Bindable]
private var late:Number;
private var projectData:XML;

private function ServiceResult(event:ResultEvent):void
{
projectData = XML(event.result);

onTime = projectData.project[0].@ontime;
late = projectData.project[0].@late;
}

[Bindable]
public var projects:ArrayCollection =
new ArrayCollection([{Project:"On Time", Percentage:50},
{Project:"Late", Percentage:50}
]);
]]>
</mx:Script>

<mx:HTTPService id="chartService" resultFormat="e4x" result="ServiceResult(event)" url="url ommitted due to forum rules" />

<mx:Panel title="Completed Projects">
<mx:PieChart id="myChart"
dataProvider="{projects}"
showDataTips="true">

<mx:series>
<mx:PieSeries field="Percentage"
nameField="Project"
labelPosition="callout"/>
</mx:series>
</mx:PieChart>
<mx:Legend dataProvider="{myChart}"/>
</mx:Panel>

</mx:Application>

Ironwil616
07-15-2010, 09:28 PM
It turned out to be an issue with the order of operations. When I assigned a static value to the variables, those were available when the chart rendered. When I grabbed them via the HTTPService, I should have assigned their values in the ResultEvent event handler. Here's what worked:


public var onTime:int;
public var late:int;
private var projectData:XML;
[Bindable]
public var projects:ArrayCollection;

private function ServiceResult(event:ResultEvent):void
{
projectData = XML(event.result);
onTime = projectData.project[0].@ontime;
late = projectData.project[0].@late;
projects = new ArrayCollection([
{Project:"On Time", Percentage:onTime},
{Project:"Late", Percentage:late}
]);
}