Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center / Flex Test Drive /

Flex Test Drive: Add charts and graphs

by Adobe

Adobe logo

Modified

27 June 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Builder Flex RIA

Video | Code | Tutorial | Links

Add pie and column charts

 

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the Test Drive solution files (ZIP, 14 MB)

Code

 

<s:Application ...> <fx:Script> <![CDATA[ (...) import mx.charts.events.ChartItemEvent; protected function chartBtn_clickHandler(event:MouseEvent):void { currentState="DepartmentChart"; } protected function deptPieCht_itemClickHandler(event:ChartItemEvent):void { currentState="DepartmentDetails"; deptColCht.dataProvider=createDataProvider(event.hitData.item); expenseAxis.title=event.hitData.item.name+" Expenses"; } protected function createDataProvider(item:Object):Array{ var dp:Array= [{field:'salaries',actual:item.actualsalary,est:item.estsalary}, {field:'travel',actual:item.actualtravel,est:item.esttravel}, {field:'supplies',actual:item.actualsupplies,est:item.estsupplies}, {field:'contractors',actual:item.actualcontractors,est:item.estcontractors}]; return dp; } ]]> </fx:Script> <fx:Declarations> (...) <mx:SeriesInterpolate id="interpolate" duration="1000"/> </fx:Declarations> (...) <mx:PieChart id="deptPieCht" includeIn="DepartmentDetails,DepartmentChart" x="50" y="250" width="282" height="282" dataProvider="{deptDg.dataProvider}" showDataTips="true" itemClick="deptPieCht_itemClickHandler(event)"> <mx:series> <mx:PieSeries field="budget" nameField="name" labelField="name" labelPosition="inside"/> </mx:series> </mx:PieChart> <s:Button id="chartBtn" x="580" y="217" includeIn="DepartmentDetails,Departments,DepartmentChart" label="Chart Data" click="chartBtn_clickHandler(event)" styleName="actionButton" enabled.DepartmentChart="false" enabled.DepartmentDetails="false"/> <mx:ColumnChart id="deptColCht" includeIn="DepartmentDetails" x="337" y="254" width="363" height="278" showDataTips="true"> <mx:series> <mx:ColumnSeries displayName="Estimated" yField="est" showDataEffect="{interpolate}"/> <mx:ColumnSeries displayName="Actual" yField="actual" showDataEffect="{interpolate}"/> </mx:series> <mx:horizontalAxis> <mx:CategoryAxis id="expenseAxis" title="Expenses" categoryField="field"/> </mx:horizontalAxis> <mx:verticalAxis> <mx:LinearAxis title="Amount" minimum="0" maximum="500000"/> </mx:verticalAxis> </mx:ColumnChart> <mx:Legend includeIn="DepartmentDetails" x="580" y="290" dataProvider="{deptColCht}"/> </s:Application>

Tutorial

In the previous five modules, you learned to build, debug, deploy, style, and skin a Flex application. In this final module, you add and format charts.

In this tutorial, you first display department budget data in a pie chart. Then when a pie chart item is clicked, individual expenses for that department are displayed in a column chart.

Step 1: Create a new DepartmentChart state with a pie chart.

Base the chart on the Departments state. Add a PieChart from the Charts section of the Components view and name it deptPieCht (see Figure 1). Arrange it as shown in Figure 2.

Add a PieChart component.
Figure 1. Add a PieChart component.
Lay out the DepartmentChart state as shown in this figure.
Figure 2. Lay out the DepartmentChart state as shown in this figure.

Step 2: Specify the data for the chart.

In Source mode, bind the PieChart dataProvider to that for the DataGrid, deptDg.dataProvider. Set the PieSeries field to budget and its nameField to name. Remove the displayName property.

The field is the property of the objects in the dataProvider to chart. The nameField is the property of the objects in the dataProvider to display in the legend.

Your code should appear as shown here:

<mx:PieChart id="deptPieCht" includeIn="DepartmentChart" x="50" y="250" width="282" height="282" dataProvider="{deptDg.dataProvider}"> <mx:series> <mx:PieSeries field="budget" nameField="name"/> </mx:series> </mx:PieChart> <mx:Legend includeIn="DepartmentChart" dataProvider="{deptPieCht}" x="339" y="252"/>

Step 3: Switch states when a button is clicked.

Add a button below the lower-right corner of the DataGrid. Give it an id of chartBtn, a label of Chart Data, and include it in the Departments and DepartmentChart states. Disable it in the DepartmentChart state. Generate a click handler for it. Make it the click handler for all states. Inside the handler, set the currentState to DepartmentChart. Make this button the action button instead of the Bigger Text ToggleButton.

Your handler should appear as shown here:

protected function chartBtn_clickHandler(event:MouseEvent):void { currentState="DepartmentChart"; }

To make the Chart Data button the action button, move the styleName property from the Bigger Text ToggleButton to the tag for this button. Your Button tag should appear as shown here:

<s:Button id="chartBtn" includeIn="DepartmentChart,Departments" x="580" y="217" label="Chart Data" styleName="actionButton" click="chartBtn_clickHandler(event)" enabled.DepartmentChart="false" />

Run the application and click the Chart Data button. The budget data is displayed in the pie chart (see Figure 3). Mouse over the chart; nothing happens.

 View the department budget data in a pie chart.
Figure 3. View the department budget data in a pie chart.

Step 4: Display chart labels and data tips.

Set the PieChart showDataTips property to true. Set the PieSeries labelField property to name and the labelPosition style to inside. Delete the legend, which is no longer needed.

The labelField is the property of the objects in the dataProvider to display on the PieChart. You have to set the labelPosition to inside, outside, callout, or insideWithCallout to specify where the labels should appear; the default value is none.

Your code should appear as shown here:

<mx:PieChart id="deptPieCht" includeIn="DepartmentChart" x="50" y="250" width="282" height="282" dataProvider="{deptDg.dataProvider}" showDataTips="true"> <mx:series> <mx:PieSeries field="budget" nameField="name" labelField="name" labelPosition="inside"/> </mx:series> </mx:PieChart>

Run the application and click the Chart Data button in the Departments state. You should now see labels on the chart and when you mouse over a slice, you should see a data tip (see Figure 4).

Add labels and data tips to the pie chart.
Figure 4. Add labels and data tips to the pie chart.

Step 5: Create a new state, DepartmentDetails, with a column chart.

Base it on the DepartmentChart state. Add a ColumnChart from the Components view called deptColCht. Arrange it as shown in Figure 5.

Lay out the DepartmentDetails state as shown in this figure.
Figure 5. Lay out the DepartmentDetails state as shown in this figure.

Step 6: Switch states when a department slice is clicked in the pie chart.

Add an itemClick handler to the PieChart and inside the handler set the currentState to DepartmentDetails. Make it the handler for all states.

Your opening PieChart tag should appear as shown here:

<mx:PieChart id="deptPieCht" includeIn="DepartmentChart,DepartmentDetails" x="50" y="250" width="282" height="282" dataProvider="{deptDg.dataProvider}" showDataTips="true" itemClick="deptPieCht_itemClickHandler(event)">

Your handler should appear as shown here:

protected function deptPieCht_itemClickHandler(event:ChartItemEvent):void { currentState="DepartmentDetails"; }

If you did not generate the event handler automatically, be sure to select ChartItemEvent from Code Assist so the appropriate import statement is written for you:

import mx.charts.events.ChartItemEvent;

Run the application and click an item in the pie chart. You should see a column chart with no data displayed (see Figure 6).

Display a column chart when a pie slice is clicked.
Figure 6. Display a column chart when a pie slice is clicked.

Step 7: Create the ColumnChart dataProvider.

Inside the PieChart itemClick handler, set the ColumnChart dataProvider equal to the array of objects returned by the function below.

Copy and paste the following createDataProvider() function into your Script block. It creates an array of objects with properties called field, actual, and est for each of the items in a department's budget and expense data.

protected function createDataProvider(item:Object):Array{ var dp:Array= [{field:'salaries',actual:item.actualsalary,est:item.estsalary}, {field:'travel',actual:item.actualtravel,est:item.esttravel}, {field:'supplies',actual:item.actualsupplies,est:item.estsupplies}, {field:'contractors',actual:item.actualcontractors, est:item.estcontractors}]; return dp; }

Call this function inside the PieChart itemClick handler and pass to it the selected item in the PieChart—which you get from the event object, event.hitData.item. Set the ColumnChart dataProvider equal to the array of objects returned by this function. Your code should appear as shown here:

protected function deptPieCht_itemClickHandler(event:ChartItemEvent):void { currentState="DepartmentDetails"; deptColCht.dataProvider=createDataProvider(event.hitData.item); }

Step 8: Specify the data to chart.

Set the ColumnSeries yField property to est and its displayName property to Estimated.

Your ColumnChart should appear as shown here:

<mx:ColumnChart id="deptColCht" includeIn="DepartmentDetails" x="337" y="254" height="278" width="363"> <mx:series> <mx:ColumnSeries displayName="Estimated" yField="est"/> </mx:series> </mx:ColumnChart> <mx:Legend includeIn="DepartmentDetails" dataProvider="{deptColCht}" x="580" y="290"/>

Run the application. When you click an item in the pie chart, you should see details for that department in the column chart (see Figure 7).

View department expense data in a column chart.
Figure 7. View department expense data in a column chart.

Step 9: Specify axis types and titles.

Set the ColumnChart horizontalAxis property to an instance of the CategoryAxis class with a categoryField of field and a title of Expenses. Set the ColumnChart verticalAxis property to an instance of the LinearAxis class with a minimum of 0, a maximum of 500000, and a title of Amount.

Your ColumnChart should appear as shown here:

<mx:ColumnChart id="deptColCht" includeIn="DepartmentDetails" x="337" y="254" height="278" width="363"> <mx:horizontalAxis> <mx:CategoryAxis title="Expenses" categoryField="field"/> </mx:horizontalAxis> <mx:verticalAxis> <mx:LinearAxis title="Amount" minimum="0" maximum="500000"/> </mx:verticalAxis> <mx:series> <mx:ColumnSeries displayName="Estimated" yField="est"/> </mx:series> </mx:ColumnChart>

Run the application. You should see the names of the fields displayed on the horizontal axis and titles for both axes (see Figure 8).

Specify axis types and titles.
Figure 8. Specify axis types and titles.

Step 10: Add a second series and show data tips.

Add a second ColumnSeries tag to the ColumnChart series property and set its yField to actual and its displayName to Actual. Set the ColumnChart showDataTips property to true.

Your ColumnChart tag should appear as shown here:

<mx:ColumnChart id="deptColCht" includeIn="DepartmentDetails" x="337" y="254" height="278" width="363" showDataTips="true"> <mx:horizontalAxis> <mx:CategoryAxis title="Expenses" categoryField="field"/> </mx:horizontalAxis> <mx:verticalAxis> <mx:LinearAxis title="Amount" minimum="0" maximum="1000000"/> </mx:verticalAxis> <mx:series> <mx:ColumnSeries displayName="Estimated" yField="est"/> <mx:ColumnSeries displayName="Actual" yField="actual"/> </mx:series> </mx:ColumnChart>

Run the application. You should see two sets of data, a legend, and data tips when you mouse over the columns (see Figure 9).

Add a second data series and data tips.
Figure 9. Add a second data series and data tips.

Step 11: Display the selected department name in the axis title.

Assign the CategoryAxis an id of expenseAxis and inside the PieChart itemClick handler, set the title of this axis to the name of the selected item plus the string, Expenses.

Your CategoryAxis should appear as shown here:

<mx:CategoryAxis id="expenseAxis" title="Expenses" categoryField="field"/>

Your handler should appear as shown here:

protected function deptPieCht_itemClickHandler(event:ChartItemEvent):void { currentState="DepartmentDetails"; deptColCht.dataProvider=createDataProvider(event.hitData.item); expenseAxis.title=event.hitData.item.name+" Expenses"; }

Run the application and select different items in the pie chart. You should see the horizontal axis title change (see Figure 10).

Display the selected department name in the axis title.
Figure 10. Display the selected department name in the axis title.

Step 12: Animate the data change.

In the Declarations block, create an instance of the SeriesInterpolate class called interpolate and set its duration property to 1000. To both of the ColumnSeries objects, add a new attribute called showDataEffect and set it equal to the interpolate object.

The duration property is set to a length of time in milliseconds. Your new declaration should appear as shown here:

<fx:Declarations> (...) <mx:SeriesInterpolate id="interpolate" duration="1000"/> </fx:Declarations>

Your ColumnSeries objects should appear as shown here:

<mx:ColumnSeries displayName="Estimated" yField="est" showDataEffect="{interpolate}"/> <mx:ColumnSeries displayName="Actual" yField="actual" showDataEffect="{interpolate}"/>

Run the application and click different department slices in the pie chart. Instead of instantly changing size, the columns should now grow larger or smaller as the underlying data changes.

In this tutorial, you displayed data in pie and column charts. In the next tutorial, you customize the pie and column charts, setting colors and formatting titles, labels, and data tips.

Learn more

Documentation: Using Flex 4.5

  • Introduction to charts
  • Chart types
  • Using pie charts
  • Using column charts
  • Using multiple data series
  • Displaying data and labels in charts
  • Events and effects in charts

ActionScript 3 Reference

  • mx.charts
  • mx.charts.effects

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Flex Test Drive: Modify the database
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Modify the database
  • Flex Test Drive: Add charts and graphs
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Change the appearance of your application
  • Flex Test Drive: Change the appearance of your application
  • Flex Test Drive: Test and debug your code

Tutorials and samples

Tutorials

  • Flex mobile performance checklist
  • Flex and Maven with Flexmojos – Part 3: Journeyman
  • Migrating Flex 3 applications to Flex 4.5 – Part 4

Samples

  • Twitter Trends
  • Flex 4.5 reference applications
  • Mobile Trader Flex app on Android Market

Flex user forum

More
07/25/2011 Flash Player Debug Issues - Safari 5.1 & Chrome 13
04/22/2012 Loader png - wrong color values in BitmapData
04/22/2012 HTTPService and crossdomain.xml doesn't work as expected
04/23/2012 Memory related crashes in Flex application

Flex Cookbook

More
04/06/2012 How to detect screen resize with a SkinnableComponent
02/29/2012 Embed Stage3D content inside Flex application components
02/15/2012 Custom WorkFlow Component
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement