I have a php function that brings in a simple arraycollection (called QuestionSeries) to FlashBuilder, with a variety of values including "Answers" and "Results" which I need in a format useable in a FlashBuilder 4 pie chart, another arraycollection (called ChartData) which looks something like:
ChartData = [0->[Answer->{Answer},Result->{Result}] , 1->[Answer->{Answer},Result->{Result}] , 2->[Answer->{Answer},Result->{Result}] , ... ]
However, the number of [{Answer},{Result}] pairs that I need to call in is dependent on the number that exist in the QuestionSeries arraycollection, although this number is given in QuestionSeries by a value, "Num_Options".
So, currently I have a bit of script that looks something like:
protected function graphsetup():void{
if(QuestionSeries.Num_Options>=1)
{
ChartData.addItem({Answer:QuestionSeries.Ans01, Result:QuestionSeries.Ans01_n})
}
if(QuestionSeries.Num_Options>=2)
{
ChartData.addItem({Answer:QuestionSeries.Ans02, Result:QuestionSe开发者_Go百科ries.Ans02_n})
}
if(QuestionSeries.Num_Options>=3)
{
ChartData.addItem({Answer:QuestionSeries.Ans03, Result:QuestionSeries.Ans03_n})
}
...
Which is ok at the moment, as I only ever have between 2 and 6 answers but a) this obviously isn't optimal even at the moment and b) at some point I may have anything up to 100 answers. So I basically want to put it into a clever for- or while-loop, however my ActionScripting skills aren't quite up to scratch quite frankly and I was hoping someone might be able to point me in the right direction?
I struggle because I can't state "QuestionSeries.Ans[i]"...
, which was the crux of my initial attempt.
I think one approach may be sort the original QuestionSeries array (all the required values come under Ans[i] and Ans[i]_n so they sit adjacent when sorted alphabetically by index) and then pop or shift elements off the end of it, but being fairly inept in terms of actionscript ability... I couldn't make it work.
Thanks a lot in advance, I'd really appreciate anything anyone might have to say on this topic.
Josh
I think you should really rethink about the structure and the naming convention in your arraycollection for easier iteration. The things you want to iterate over begin from 1 and contain leading zeroes. And I think it would be better to contain all your answers and results as tuples in a simple indexed array.
Still if that's not an option, you can use something along the lines of:
protected function addLeadingZero(number:int):String {
if (number > 10) return number.toString();
return "0" + number.toString();
}
protected function graphSetup():void {
for (var i:int, l:int = int(QuestionSeries.Num_Options); i < l; i++) {
ChartData.addItem({
Answer:QuestionSeries["Ans" + addLeadingZero(i + 1)],
Result:QuestionSeries["Ans" + addLeadingZero(i + 1) + "_n"]
});
};
}
精彩评论