views:

377

answers:

1

I am creating a chart using mxml. The mxml tags only create a chart with a horizontal axis and vertical axis.

My result event handler has actionscript code that loops through the xml result set and creates all the series (line series and stacked bar). This part of the code works fine.

Now I need to use the functionfill function to set individual colors to each series. All the examples I have found call the functionfill from within an MXML tag, like so:

       <mx:ColumnSeries id="salesGoalSeries" 
            xField="Name" 
            yField="SalesGoal" 
            fillFunction="myFillFunction" 
            displayName="Sales Goal">

I am having trouble calling functionfill from actionscript.

A portion of the code that build the data series is below:

if (node.attribute("ConfidenceStatus")=="Backlog" 
|| node.attribute("ConfidenceStatus")=="Billings") {
// Create the new column series and set its properties.
var localSeries:ColumnSeries = new ColumnSeries();
localSeries.dataProvider = dataArray;
localSeries.yField = node.attribute("ConfidenceStatus");
localSeries.xField = "TimebyDay";
localSeries.displayName = node.attribute("ConfidenceStatus");
localSeries.setStyle("showDataEffect", ChangeEffect);
localSeries.fillFunction(setSeriesColor(xxx));

// Back up the current series on the chart.
var currentSeries:Array = chart.series;

// Add the new series to the current Array of series.
currentSeries.push(localSeries);

//Add Array of series to columnset
colSet.series.push(localSeries);  

//assign columnset to chart
chart.series = [colSet];

My setSeriesColor function is:

private function setSeriesColor(element:ChartItem, index:Number):IFill {
var c:SolidColor = new SolidColor(0x00CC00);
var item:ColumnSeriesItem = ColumnSeriesItem(element);

//will put in logic here
return c;

}

What parameters do I put in the line localSeries.fillFunction(setSeriesColor(xxx)) ?

I tried localSeries as the first argument but I get an implicit coercion error telling me localSeries can't be cast as ChartItem.

How do I call the function correctly?

+1  A: 
localSeries.fillFunction = setSeriesColor;

The code you have right now is actually CALLING setSeriesColor the way you have it set up. You only want it to refer to a reference of the function, not calling it, so just send it "setSeriesColor" as a variable.

CookieOfFortune
Yup! Functions are Objects in AS3.
James Ward
ok, that makes sense and is very convenient. Flex automatically passes the correct parameters.How do you know when to call a function and when to refer to a reference of a function?
Ivan
I generally just think, "Whose responsibility is it to call the function?". If it is my responsibility, I'll call it myself, otherwise it gets passed as a variable and I will let someone else call it. It's a very common practice in AS3. Event handlers are written in that way, so you can practice some with that to get your head around the idea. In the docs, function variables usually refer to interfaces (IFill for fillFunction), so that can be an indicator too.
CookieOfFortune