Flex 2 Charting with XML tutorial

Flex 2 Charting with XML tutorial – xml, flex, charting, builder, 2, adobe, customizing, rich, internet, application, tutorial

Adobe Flex is not well documented (well, let’s say information is hard to find). Even more so for the charting component for Flex Builder 2. Hereby a tutorial where we set up a column chart, import XML data and format the chart.

Update: Added formatting paragraph and the end result.

Setting up the project

  • Create a new Basic Flex Project called ‘ChartingTutorial’.
  • Switch to Design View in the workspace.
  • Drag a column chart from your components window on the workspace and name it ‘columnchart’.
  • Align the chart and legend, and play around with the width and height of your Flex Application.

Loading data

  • Before we load data, we need to have actual data. In your navigator window right click on your projects bin directory and create a new file. Name the file ‘stats.xml’.

    Add the following XML code to this file:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <data>
    3.     <row name="Q1">
    4.         <expenses>64</expenses>
    5.         <revenue>98</revenue>
    6.         <profit>34</profit>
    7.     </row>
    8.     <row name="Q2">
    9.         <expenses>68</expenses>
    10.         <revenue>96</revenue>
    11.         <profit>28</profit>
    12.     </row>
    13.     <row name="Q3">
    14.         <expenses>62</expenses>
    15.         <revenue>89</revenue>
    16.         <profit>27</profit>
    17.     </row>
    18.     <row name="Q4">
    19.         <expenses>68</expenses>
    20.         <revenue>96</revenue>
    21.         <profit>26</profit>
    22.     </row>
    23. </data>
  • To load XML data we will use a HTTPService called ‘dataRequest’. Open your main application MXML file (ChartingTutorial.mxml) and switch to source view.

  • Add the following code inside the mx:Application node:

    1. <mx:HTTPService id="dataRequest" url="stats.xml" result="dataRequestResultHandler(event);" fault="dataRequestFaultHandler(event);" />

    The attributes ‘result’ and ‘fault’ are respectively for handling the result of the request and handling errors. These attributes point to actionscript which we are going to add now.

  • Add the following code inside the mx:Application node:

    1. <mx:Script>
    2.     <![CDATA[
    3.         //Code to add.
    4.     ]]>
    5. </mx:Script>
  • Within the script and CDATA tags add the following import statements.

    1. import mx.controls.Alert;
    2. import mx.rpc.events.FaultEvent;
    3. import mx.rpc.events.ResultEvent;
    4. import mx.collections.ArrayCollection;
  • Next we define the handler functions being called by the HTTPService together with a bindable variable in which we will store the XML result data:

    1. [Bindable]
    2. private var stats:ArrayCollection;
    3.  
    4. private function dataRequestResultHandler(event:ResultEvent):void {
    5.     stats = event.result.data.row;
    6. }
    7.  
    8. private function dataRequestFaultHandler(event:FaultEvent):void {
    9.     Alert.show(event.fault.message);
    10. }
  • We need to send the data request. We will do this by adding a creation complete attribute to the mx:Application element:

    1. creationComplete="dataRequest.send()"

 

Inserting data

  • Now it’s time to insert the data into the chart. Add a dataProvider attribute to your columnChart element pointing to the stats variable:

    1. dataProvider="{stats}"
  • If you look in the XML file you will see that every row contains expenses, revenue and profit data data. These need to be added as columnSeries in your columnChar. The yField values point to these childs in the XML line node.

    1. <mx:series>
    2.     <mx:ColumnSeries displayName="Expenses" yField="expenses"/>
    3.     <mx:ColumnSeries displayName="Revenue" yField="revenue"/>
    4.     <mx:ColumnSeries displayName="Profit" yField="profit"/>
    5. </mx:series>

    You can now run the application! You should some something similar to this:

  • The horizontal axis doesn’t have the right description yet. The description data is also located in the XML file in the name attribute in the row nodes. Changing the description is done by adding a horizontal axis within the columnChart which pointing to the dataset stats and the attribute name:

    1. <mx:horizontalAxis>
    2.     <mx:CategoryAxis dataProvider="{stats}" categoryField="name" />
    3. </mx:horizontalAxis>

 

Formating the chart
We are going to change the charts looks into a clean and smooth chart.

  • A Flex application background is always a gradient. In our case we will change it to a white background. It can be done in Design view, but changing it manually is also peanuts. Add the following attribute to the mx:Application node:

    1. backgroundGradientColors="[#ffffff, #ffffff]"
  • The shadow doesn't really fit in a clean and smooth appearance. To lose it we need to overrule the standard 'filter array' with an empty array by adding this bit code inside the mx:ColumnChart node:

    1. <mx:seriesFilters><mx:Array/></mx:seriesFilters>
  • Let's lose those ugly huge horizontal and vertical axis. This is done by a axisRenderer element for both axis. Add the following code inside the mx:ColumnChart node:

    1. <mx:horizontalAxisRenderer>
    2.     <mx:AxisRenderer>
    3.         <mx:axisStroke>
    4.             <mx:Stroke color="#999999" weight="1"/>
    5.         </mx:axisStroke>
    6.     </mx:AxisRenderer>
    7. </mx:horizontalAxisRenderer>
    8.  
    9. <mx:verticalAxisRenderer>
    10.     <mx:AxisRenderer>
    11.         <mx:axisStroke>
    12.             <mx:Stroke color="#999999" weight="1" />
    13.         </mx:axisStroke>
    14.     </mx:AxisRenderer>
    15. </mx:verticalAxisRenderer>
  • The bars need some new colors too! Maybe nice blue tints?
    This is done by adding fill elements within the mx:ColumnSeries nodes. To make it easy for you, just replace the complete mx:Series element with this code:

    1. <mx:series>
    2.     <mx:ColumnSeries displayName="Expenses" yField="expenses">
    3.         <mx:fill>
    4.             <mx:SolidColor color="#738994" />
    5.         </mx:fill>
    6.     </mx:ColumnSeries>
    7.     <mx:ColumnSeries displayName="Revenue" yField="revenue">
    8.         <mx:fill>
    9.             <mx:SolidColor color="#a7c8d8" />
    10.         </mx:fill>
    11.     </mx:ColumnSeries>
    12.     <mx:ColumnSeries displayName="Profit" yField="profit">
    13.         <mx:fill>
    14.             <mx:SolidColor color="#406374" />
    15.         </mx:fill>
    16.     </mx:ColumnSeries>
    17. </mx:series>

Et violà. Behold my result:

Combine it with dynamic XML data and you will have a powerful chart.

Author rcz Time Jan 8, 2008 Comment on 'Flex 2 Charting with XML tutorial' Add comment Bookmark 'Flex 2 Charting with XML tutorial' Bookmark Permalink to 'Flex 2 Charting with XML tutorial' Permalink
Bookmark 'Flex 2 Charting with XML tutorial' to Netvouz Bookmark 'Flex 2 Charting with XML tutorial' to Facebook Bookmark 'Flex 2 Charting with XML tutorial' to Furl Bookmark 'Flex 2 Charting with XML tutorial' to Technorati Bookmark 'Flex 2 Charting with XML tutorial' to Stumble Upon Bookmark 'Flex 2 Charting with XML tutorial' to Digg Bookmark 'Flex 2 Charting with XML tutorial' to Del.icio.us

Comments (10)

Comment author Marius Time Jan 10, 2008 09:28 GMT #1
Thanks Face: Grin I've been indeed searching my ass off for some stuff (loading actual XML data in particular)
Comment author Ed Time Mar 7, 2008 04:42 GMT #2
Thank you. That was my first step getting to grips with Flex charting.
Comment author Cyberthug Time May 29, 2008 05:03 GMT #3
The code didn't work in Flex 3. I get a parse error on the binding.
Comment author rcz Time Jun 2, 2008 11:44 GMT #4
The code didn't work in Flex 3. I get a parse error on the binding.
The tutorial is written for Flex version 2. When I have the time I'll try to make an updated version.
Comment author Brian Time Jun 6, 2008 04:39 GMT #5
Face: Devil Grin
Comment author Fernanda Gomez Comment author website www.adsmap.net Time Jun 7, 2008 07:08 GMT #6
Great tutorial, thanks!
Comment author manjul Comment author email manjulkatare<at>gmail.com Time Jul 9, 2008 11:09 GMT #7
It really helped me......It works fine with FLEX 3.0 as well, just take out fault="dataRequestFaultHandler(event);"

Add for chart legend :
<mx:Legend dataProvider="{columnchart}"/>

Thanks
Comment author John Myers Comment author email johndog<at>gmail.com Time Jul 23, 2008 06:17 GMT #8
Flex 3 fix:

1. Right click on your project folder in the Flex Navigator and click Properties.
2. Click on Flex Compiler
3. In the input for Additional compiler arguments, enter -use-network=false
Comment author hsTed Comment author email hsTedd<at>gmail.com Time Jul 26, 2008 07:50 GMT #9
Your tutorial helped me to accomplish a tough project. thanks!
Comment author riyas Comment author email riyasahamedakr<at>gmail.com Time Aug 25, 2008 08:26 GMT #10
how to draw a area chart according to the input given by user in text box ?

Add comment

Your name
Your email or website (optional)
Icon
Confirmation code
Confirmation code
Message
face:happy face:glasses face:devilgrin face:crying face:grin face:laughing face:sad face:kiss face:shocked face:wink face:monkey face:angel