Search code examples
actionscript-3apache-flexflash-builderlinechart

flex mx line chart datafunction


I have a JSON service link that returns month as an Integer and a price as a Number which I display on an <mx:Linechart />.

Now, I don't want to display the month as an Integer instead as the string equivalent of the month value.

I have tried to create a datafunction like so

public function myDataFunction(series:LineSeries, item:Object, fieldName:String):Object {
    var months:Array = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var my_date:Date = new Date();

    if (fieldName == 'yValue')
        return(item.price);
    else if (fieldName == "xValue")
    {
        return((months[my_date.month]));
    }
    else
        return null;
}

I would like to post reputations but i don't have enough reputations. Anybody please help.

Thanks


Solution

  • To show months names in the horizontal axis, you need to assign labelFunction to it.

    When declaring line chart, you specify horizontal axis like this:

    <mx:Linechart>
         <mx:horizontalAxis>
             <mx:CategoryAxis categoryField="xValue"/>
         </mx:horizontalAxis>
    </mx:Linechart>
    

    To modify values of horizontal axis, you need to specify labelFunction:

    <mx:CategoryAxis categoryField="xValue" labelFunction="labelFunction"/>
    

    labelFunction should look like this:

    function labelFunction(categoryValue:Object, previousCategoryValue:Object, axis:CategoryAxis, categoryItem:Object):String {
        return "label";
    }
    

    For more info see: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/charts/CategoryAxis.html#labelFunction