Skip navigation
Farhad Navaei
Currently Being Moderated

Dynamically adding ColumnSeries doesn't work

May 22, 2013 11:24 AM

Tags: #flex #4.5 #actionscript #charting

I created the following code to create different series dynamically:

<?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx=""  
        xmlns:mx="library://" minWidth="955" minHeight="600">
            import mx.charts.series.ColumnSeries;
            import mx.collections.ArrayCollection;
            import mx.collections.Sort;
            import mx.collections.SortField;

            public var Loc01:ArrayCollection = new ArrayCollection([
                {date:2002, close:41.87, loc: Location 01},
                {date:2005, close:48.06, loc: Location 01},
                {date:2006, close:48.06, loc: Location 01},
                {date:2002, close:157.59, loc: Location 02},
                {date:2003, close:160.3, loc: Location 02},
                {date:2003, close:45.74, loc: Location 01},
                {date:2004, close:42.77, loc: Location 01},
                {date:2004, close:150.71, loc: Location 02},
                {date:2002, close:45.74, loc: Location 03},
                {date:2003, close:42.77, loc: Location 03},
                {date:2004, close:150.71, loc: Location 03},
                {date:2005, close:156.88, loc: Location 02}

        protected function application1_creationCompleteHandler(event:FlexEvent):void
            // TODO Auto-generated method stub
            var obj:Object;
            var arr:Array = [];
            var i:int;
            var ac1:ArrayCollection;
            var NS:ColumnSeries;
            var sortA:Sort = new Sort();
            var sortByLocation:SortField = new SortField(loc,false,false,false);
            var sortByDate:SortField = new SortField(date, true, false, false);

            sortA.fields=[sortByLocation, sortByDate];


            arr[i] = Loc01[0].loc;
            for each(obj in Loc01){
                if(arr[i] != obj.loc)
                    arr[i] = obj.loc;
            var str: String;
            for each(str in arr){
                ac1 = new ArrayCollection();
                for each(obj in Loc01){
                    if(str == obj.loc)
                    myChart.series.push(new ColumnSeries());
                NS = myChart.series[i];
                NS.dataProvider = ac1;
                NS.yField = close;
                NS.horizontalAxis = h1; 
                NS.displayName = str;
                NS.verticalAxis = v1;
       = cs + i.toString();


        <!-- Place non-visual elements (e.g., services, value objects) here -->

    <mx:Panel x="49" y="30" width="868" title="Column Chart With Multiple Axes">
        <!--mx:Legend width="769" dataProvider="{myChart}"/-->
        <mx:ColumnChart id="myChart" width="601" showDataTips="true" dataProvider="{Loc01}">
                <mx:CategoryAxis id="h1" categoryField="date"/>

                <mx:AxisRenderer placement="bottom" axis="{h1}"/>

                <mx:AxisRenderer placement="left" axis="{v1}"/>
                <mx:LinearAxis id="v1" minimum="30" maximum="170"/>           
                <mx:ColumnSeries id="cs0"/>



The hard coded ColumnSeries (the first one) works fine, but the next series which are created dynamically don't work. What's wrong in my code? Any help would be much appreciated.

  • Currently Being Moderated
    May 24, 2013 7:56 AM   in reply to Farhad Navaei

    Hi, check your loop.

    Move ac1 = new ArrayCollection(); out of the loop, like:


                ac1 = new ArrayCollection();

                for each(str in arr){

                    for each(obj in Loc01){

                        if(str == obj.loc)


    But I am not sure if this what you need.


    Mark as:
  • Currently Being Moderated
    May 29, 2013 5:41 AM   in reply to Farhad Navaei

    Glad to hear that, please try to mark your question answered.


    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points