Flex Charting: Per-Item Fills

Get the Flash Player to see this player.

(Flex Charting is now Flex Data Visualization.)

Interested in how this was accomplished? Flex application located at the bottom of the post.

XML

The data for this sample application is being loaded from an XML file. The structure is similar to the following:

XML:
  1. <questions>
  2.     <question name="Question" score="90.4935" color="0x00529B">
  3.         <month name="December 2007" score="90.4935">
  4.             <subquestion name="Sub Question" score="90.4935">
  5.                 <breakdown name="Male" score="87.3264" color="0x333333"/>
  6.                 ...
  7.             </subquestion>
  8.             ...
  9.         </month>
  10.         ...
  11.     </question>
  12.     ...
  13. </questions>

Attributes to colors

Each <Question ...> tag contains an attribute named color with a string value of "0xVALUE". We need that string value parsed into a value that we can use to create a color. Easy enough:

Actionscript:
  1. /**
  2. * Replace "0x333333" with a reference to your string value
  3. */
  4. var color:SolidColor = new SolidColor(parseInt("0x333333") as uint);

Colors to Per-Item Fills

Now that we know how to parse a string to a SolidColor, let's use that to fill chart series items. We will use a ColumnChart as an example:

Actionscript:
  1. <mx:Script>
  2.     <![CDATA[
  3.         import mx.charts.ChartItem;
  4.         import mx.graphics.IFill;
  5.         import mx.graphics.SolidColor;
  6.         import com.domain.project.model.ModelLocator;
  7.        
  8.         /**
  9.          * Model
  10.          */
  11.         [Bindable]
  12.         private var model:ModelLocator = ModelLocator.getInstance();
  13.  
  14.         /**
  15.          * Chart Per-Item Fill function
  16.          */
  17.         private function itemFillFunction(element:ChartItem, index:Number) : IFill
  18.         {
  19.             var color:SolidColor = new SolidColor(parseInt(element.item.color) as uint);
  20.             return color;
  21.         }
  22.     ]]>
  23. </mx:Script>
  24.  
  25. <mx:ColumnChart dataProvider="{model.data}">
  26.     <mx:series>
  27.         <mx:ColumnSeries dataProvider="{model.data}"
  28.             yField="score" xField="name"
  29.             fillFunction="itemFillFunction"/>   <!-- notice: fillFunction-->
  30.     </mx:series>
  31.     <mx:horizontalAxis>
  32.         <mx:CategoryAxis categoryField="name"/>
  33.     </mx:horizontalAxis>
  34. </mx:ColumnChart>

The ColumnSeries sends each ChartItem to the itemFillFunction and expects back an object which implements IFill, such as SolidColor. Since each of the objects in the data provider contain a property called color with a string representation of the color value, we can parse out the color and return it to the chart item.

Noteworthy: Since the series fillFunction expects back anything which implements IFill, you could also return a LinearGradient or RadialGradient.

Try it out

If you found this helpful, please let me know! Thanks.

1 Response to “Flex Charting: Per-Item Fills”


  1. 1 benhuangua

    it's very useful and awesome. so cool.
    can you mail me the file? thx.

Leave a Reply