Monday, May 12, 2008

Creating Line Charts in Flex 3

Related Posts:

Making a Line Chart in 5 steps

There are many ways to make your charts, what follows is a recipe that allows you to tweak common aspects of the chart. Steps 3 through 5 are not necessarily required.

1. Create the Application and LineChart tags

Start by creating a Flex application w/the Application tag.

Then use a <Script> block to write some ActionScript. Declare an ArrayCollection named myArrayCollection. In a real application this data could be retreived from the web server as XML.

Declare a LineChart named myChart in MXML. Then bind the myArrayCollection to the LineChart's dataProvider property, using the curly brace notation:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 layout="vertical">

 <mx:Script>
     <![CDATA[

     import mx.collections.ArrayCollection;

     [Bindable] private var myArrayCollection : ArrayCollection =
         new ArrayCollection(
         [ { sales: 101000, month: new Date( '01/01/2008' ) },
         { sales: 350000, month: new Date( '02/01/2008' ) },
         { sales: 475000, month: new Date( '03/01/2008' ) },
         { sales: 425000, month: new Date( '04/01/2008' ) }
         ] );

     ]]>
 </mx:Script>

 <mx:LineChart id="myChart"
     dataProvider="{myArrayCollection}"
     width="400" height="400"
     showDataTips="true"
     >

 </mx:LineChart>
</mx:Application>

2. Configure the series property of the LineChart object

The series property defines the data we're plotting. For a LineChart, you need to define this as a LineSeries object. We're going to use the LineSeries to specify where the X and Y values are in the chart's dataProvider. So we specify field names from our canned data in the xField and yField properties.

If you want to plot multiple data series, tack on a LineSeries object for each series.

We want to override the default colors Flex uses to render the chart lines. To do this we set the lineStroke property to a Stroke object, this specifies the color and size of the painting "stroke".

Add the following, as a child tag to the LineChart element:

<mx:series>
 <mx:LineSeries displayName="Sales by Month"
     yField="sales" xField="month" >

     <mx:lineStroke>
         <mx:Stroke
             color="0x11538C"
             weight="3"
             alpha="1"
             />
     </mx:lineStroke>
        
 </mx:LineSeries>
</mx:series>

3. Configure the horizontalAxis and verticalAxis properties of the LineChart object

This is not mandatory, but I recommend it for better control over how your axis labels are displayed. You can specify four different types of axes:

  • LinearAxis - for numeric values
  • LogarithmicAxis - for numeric values
  • DateTimeAxis - for date values
  • CategoryAxis - the default, for your own categories

You can set various properties on these axis objects, we are using a DateTimeAxis and a LinearAxis in this chart. Add the following, as a child tag to the LineChart element:

<mx:horizontalAxis>
 <mx:DateTimeAxis id="hAxis"
     dataUnits="months"
     alignLabelsToUnits="true"
     displayLocalTime="true"/>
</mx:horizontalAxis>

<mx:verticalAxis>
 <mx:LinearAxis id="vAxis" />
</mx:verticalAxis>

4. Configure the horizontalAxisRenderer and verticalAxisRenderer properties of the LineChart object

Axis renderers control how each axis is displayed, and are optional in this approach. Note how we use data binding to specify which axis each renderer should apply to. And again, add the following, as a child tag to the LineChart element:

<mx:horizontalAxisRenderers>
 <mx:AxisRenderer axis="{hAxis}"
     showLine="true"
     tickPlacement="outside"
     />
</mx:horizontalAxisRenderers>

<mx:verticalAxisRenderers>
 <mx:AxisRenderer axis="{vAxis}"
     showLine="false"
     tickPlacement="none"
 />
</mx:verticalAxisRenderers>

5. Add a Legend to your chart

You can optionally add a legend. You do this with data binding, by setting the dataProvider property of a Legned object. Add the Legend object as a child to the Application (not the LineChart):

<mx:Legend dataProvider="{myChart}" />

Keep on tweakin'

Check out the Flex 3 API for details on all of the classes mentioned here. Here are some more properties of the LineChart object that you can tweak:

  • seriesFilter - use this to modify the filters for each line, you can set this to a blank array to remove drop shadows
  • backgroundElements - use this to configure grid lines
  • fills - use this property to specify the background color of the chart

If you've been following along, you should have produced the app below. Right click to view the final source.

8 comments:

  1. Thanks for sharing all this stuff man. Very helpful.

    ReplyDelete
  2. Yes, but how can i achieve the datetimeformat

    YYYY-MM-DD HH:mm:ss

    It seems impossible

    Thanks

    /Niel

    ReplyDelete
  3. You need to use a dateformatter.

    ReplyDelete
  4. Hi Niel,

    As Anonymous just pointed out you need to use a DateFormatter to format your date with the desired format string.

    Then, if you want to use this date string for the axis labels on your chart, you can use the axis' "labelFunction" property/call back function to print the dates using the DateFormatter.

    I have an example of using the labelFunction call back for a LinearAxis here:
    http://blog.sunild.com/2008/08/inverting-chart-series.html

    Cheers!

    ReplyDelete
  5. Nice example , thank you.
    Is there a way to have a second line graph over the current one, but the second line chart has a different y axis value.

    eg, overlay price -date line chart over sales-date line chart.

    ReplyDelete
  6. hey great work .very helpful for me.
    Thanks for sharing.

    ReplyDelete
  7. Thanks, this was very helpful.

    ReplyDelete
  8. Hello,

    Thanks.

    The above one is really helpful..but i dont want the graph to start from origin, Graph should start from middle i.e 1/08 on x-axis should be beside 0, not at 0.

    ReplyDelete