Search code examples
apache-flexactionscript-3flex4

FLEX data manipulation separating data for bargraph display from a PHP call


I am a novice FLEX Developer I want to separate the data from the PHP call to Different CollectionArrays So that I can use the data for a bar-graph;

I created a static version of the bar-graph and separated the Collection array manually I don't want this done manually I need to do it dynamically. So what I am trying to understand is where to put the event-listener how to add to a collectionArray then once the collection array(s) are built. use that information to build the bar graphs...

    /*
         *
        36, > 2 years, Compliance
        6, 0-90 Days, Compliance
        32, 181-365 Days, Compliance
        72, 366-730 Days, Compliance
        15, 91-180 Days, Compliance
        4, > 2 years, Medium/Low
        118, 0-90 Days, Medium/Low
        143, 181-365 Days, Medium/Low
        29, 366-730 Days, Medium/Low
        67, 91-180 Days, Medium/Low
        10, > 2 years, Patient Safety
        2, 0-90 Days, Patient Safety
        17, 181-365 Days, Patient Safety
        18, 366-730 Days, Patient Safety
        5, 91-180 Days, Patient Safety 
        */

I need the above information translated to the bottom information using a onload finished event listener.

        [Bindable]
        public var _compliance:ArrayCollection = new ArrayCollection([
            {Count:36, Time : "> 2 years"       },
            {Count:6,  Time : "0-90 Days"       },
            {Count:32, Time : "181-365 Days"    },
            {Count:72, Time : "366-730 Days"    },
            {Count:15, Time : "91-180 Days"     }
        ]);

        [Bindable]
        public var _medlow:ArrayCollection = new ArrayCollection([
            {Count:4,   Time : "> 2 years"      },
            {Count:118, Time : "0-90 Days"      },
            {Count:143, Time : "181-365 Days"   },
            {Count:29,  Time : "366-730 Days"   },
            {Count:67,  Time : "91-180 Days"    }
        ]);;

        [Bindable]
        public var _patient:ArrayCollection = new ArrayCollection([
            {Count:10, Time : "> 2 years"       },
            {Count:2,  Time : "0-90 Days"       },
            {Count:17, Time : "181-365 Days"    },
            {Count:18, Time : "366-730 Days"    },
            {Count:5,  Time : "91-180 Days"     }
        ]);;

Solution

  • Like @J_A_X said, data like this makes things a bit more difficult because it becomes your job to parse it. But, no biggie. You can just do some split work to do the parsing here. Assuming your lines are delimited with "\n", then the following should work. I am using ActionLinq which makes data processing a breeze.

    I am assuming your data from the server has been put in a string called serverData:

    private function parseLine(line:String):Object {
        var split:Array = line.split(", ");
        return { Count: Number(split[0]), Time: split[1], Category: split[2] };
    }
    
    private function getDataBy(category:String):ArrayCollection {
        return Enumerable.from(serverData.split("\n"))
                .select(parseLine)
                .where(function(x:*):Boolean { return x.Category == category })
                .toArrayCollection();
    }
    
    [Bindable] public var _compliance:ArrayCollection;
    [Bindable] public var _medlow:ArrayCollection;
    [Bindable] public var _patient:ArrayCollection;
    

    Then, when you get your data back from the server, you can populate your ArrayCollections:

    _compliance = getDataBy("Compliance");
    _medlow = getDataBy("Medium/Low");
    _patient = getDataBy("Patient Safety");