Search code examples
apache-flexflex-chartingflex-charts

How can I use filterFunction in ColumnSeries


I have a set of data like the following:

        [Bindable]
    public var Loc:ArrayCollection = new ArrayCollection([
        {date:"2002", close:41.87, loc: "Location 01"},
        {date:"2003", close:45.74, loc: "Location 01"},
        {date:"2004", close:42.77, 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:"2004", close:150.71, loc: "Location 02"},
        {date:"2005", close:156.88, loc: "Location 02"},
    ]);

and I'd like to create a chart like the following, in which each location is displayed in different color and grouped by each year: enter image description here

I was thinking that I must use "mx:ColumnSeries" tag and use its "filterFunction" property to define how to filter the data for each location as a separate series, but I don't know how to do that. Does anyboy have any recommendation or sample code for creating such charts? Thank you


Solution

  • Here an example:

    private function filterSeries1(cache:Array):Array
    {
        var filteredCache:Array = [];
        for (var i:int = 0; i < cache.length; i++)
        {
            var item:ColumnSeriesItem = ColumnSeriesItem(cache[i]);
            if(item.item.loc == "Location 01")
                filteredCache.push(item);
        }
        return filteredCache;
    }
    
    private function filterSeries2(cache:Array):Array
    {
        var filteredCache:Array = [];
        for (var i:int = 0; i < cache.length; i++)
        {
            var item:ColumnSeriesItem = ColumnSeriesItem(cache[i]);
            if(item.item.loc == "Location 02")
                filteredCache.push(item);
        }
        return filteredCache;
    }
    

    And:

    <mx:series>
        <mx:ColumnSeries yField="close" xField="date"
                         labelPosition="outside"
                         filterFunction="{filterSeries1}"/>
        <mx:ColumnSeries yField="close" xField="date"
                         labelPosition="outside"
                         filterFunction="{filterSeries2}"/>
    </mx:series>