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

Format 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

FlexWebTestDrive.css

@namespace s "library://ns.adobe.com/flex/spark"; (...) mx|PieSeries{ fills:#6B96D0,#5B7FB2,#81A4D0,#7EC2EA,#6B84A5,#686BD0,#6A8AD0; } .grayFill{ fill:#999999; } mx|ColumnChart { axisTitleStyleName:boldTitles; } .boldTitles{ fontWeight:bold; }

FlexWebTestDrive.mxml

<s:Application ...> <fx:Script> <![CDATA[ (...) import mx.charts.HitData; import mx.charts.chartClasses.IAxis; import mx.charts.series.items.PieSeriesItem; protected function deptPieCht_itemClickHandler(event:ChartItemEvent):void { currentState="DepartmentDetails"; deptColCht.dataProvider=createDataProvider(event.hitData.item); expenseAxis.title=event.hitData.item.name+" Expenses"; estSeries.setStyle("fill",(event.hitData.chartItem as PieSeriesItem).fill); } protected function axisMoneyFormatter(labelValue:Object, previousValue:Object, axis:IAxis):String{ return moneyFormatter.format(labelValue); } protected function formatDeptPieTips(hitData:HitData):String{ return "<b>" + hitData.item.name + "</b><br/>"+"Budget: " + moneyFormatter.format(hitData.item.budget); } ]]> </fx:Script> <fx:Declarations> (...) <s:SolidColorStroke id="dkgrayStrokeThick" color="#4D4D4D" weight="5"/> <s:SolidColorStroke id="dkgrayStrokeThin" color="#4D4D4D" weight="1"/> <s:CurrencyFormatter id="moneyFormatter" useCurrencySymbol="true" currencySymbol="$" fractionalDigits="0"/> </fx:Declarations> (...) <mx:PieChart id="deptPieCht" showDataTips="true" itemClick="deptPieCht_itemClickHandler(event)" dataTipFunction="formatDeptPieTips" .../> (...) <mx:ColumnChart id="deptColCht" includeIn="DepartmentDetails" x="337" y="254" width="363" height="278" showDataTips="true"> <mx:series> <mx:ColumnSeries id="estSeries" displayName="Estimated" yField="est" showDataEffect="{interpolate}"/> <mx:ColumnSeries displayName="Actual" yField="actual" showDataEffect="{interpolate}" styleName="grayFill"/> </mx:series> <mx:horizontalAxis> <mx:CategoryAxis id="expenseAxis" title="Expenses" categoryField="field"/> </mx:horizontalAxis> <mx:verticalAxis> <mx:LinearAxis id="amountAxis" title="Amount" minimum="0" maximum="500000" labelFunction="axisMoneyFormatter"/> </mx:verticalAxis> <mx:verticalAxisRenderers> <mx:AxisRenderer verticalAxisTitleAlignment="vertical" axis="{amountAxis}" axisStroke="{dkgrayStrokeThick}" tickStroke="{dkgrayStrokeThin}"/> </mx:verticalAxisRenderers> <mx:horizontalAxisRenderers> <mx:AxisRenderer axis="{expenseAxis}" axisStroke="{dkgrayStrokeThick}" tickStroke="{dkgrayStrokeThin}"/> </mx:horizontalAxisRenderers> </mx:ColumnChart> <mx:Legend .../> </s:Application>

Tutorial

In this tutorial, you customize the pie and column charts. You change fill, axis, and tick colors; rotate axis titles; format axis titles and labels; and format data tips.

Step 1: Set chart fill colors.

In FlexWebTestDrive.css, create a PieSeries type selector with the fills style set to a list of at least seven colors (#6B96D0, #5B7FB2,#81A4D0, #7EC2EA, #6B84A5, #686BD0, #6A8AD0). Create a class selector called grayFill with fill set to some color (#5B7FB2). In FlexWebTestDrive.mxml, set the styleName property of the Actual ColumnSeries to grayFill.

Your new selectors should appear as shown here:

mx|PieSeries{ fills:#6B96D0,#5B7FB2,#81A4D0,#7EC2EA,#6B84A5,#686BD0,#6A8AD0; } .grayFill{ fill:#5B7FB2; }

Your ColumnSeries should appear as shown here:

<mx:ColumnSeries displayName="Actual" yField="actual" styleName="grayFill" .../>

Run the application and drill down into department data. You should see your new colors in the pie and column charts (see Figure 1). You will set the color of the first column series to match the color of the selected item in the pie chart next.

Customize the chart fill colors.
Figure 1. Customize the chart fill colors.

Step 2: Set a column fill color dynamically.

Assign the first ColumnSeries an id of estSeries and in the deptPieCht itemClick handler, use setStyle() to set the fill style to the color of the selected pie chart item: (event.hitData.chartItem as PieSeriesItem).fill.

Your ColumnSeries should appear as shown here:

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

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"; estSeries.setStyle("fill",(event.hitData.chartItem as PieSeriesItem).fill); }

Be sure to select PieSeriesItem from Code Assist so the appropriate import statement is written for you:

import mx.charts.series.items.PieSeriesItem;

Run the application. When you select a department in the pie chart, the first series in the column chart is now the same color (see Figure 2). Look at the position of the vertical axis title; you will flip this in the next step.

 Match the colors of the selected pie chart item and the first column series.
Figure 2. Match the colors of the selected pie chart item and the first column series.

Step 3: Rotate the axis title.

Set the LinearAxis id to amountAxis and set the ColumnChart verticalAxisRenderers property to an instance of the AxisRenderer class. For the AxisRenderer, set the verticalAxisTitleAlignment style to vertical and the axis property to amountAxis.

Your code should appear as shown here:

<mx:verticalAxis> <mx:LinearAxis id="amountAxis" title="Amount" minimum="0" maximum="500000"/> </mx:verticalAxis> <mx:verticalAxisRenderers> <mx:AxisRenderer verticalAxisTitleAlignment="vertical" axis="{amountAxis}"/> </mx:verticalAxisRenderers>

Run the application. The vertical axis title should now be rotated (see Figure 3).

Rotate the vertical axis title.
Figure 3. Rotate the vertical axis title.

Step 4: Make the axis titles bold.

In FlexWebTestDrive.css, create a ColumnChart type selector and set axisTitleStyleName to a class selector called boldTitles. Create the class selector called boldTitles and set its fontWeight to bold.

Your selectors should appear as shown here:

mx|ColumnChart { axisTitleStyleName:boldTitles; } .boldTitles{ fontWeight:bold; }

Run the application. The axis titles should now be bold (see Figure 4).

Make the axis titles bold.
Figure 4. Make the axis titles bold.

Step 5: Set axis and tick colors.

In the Declarations block, create a SolidColorStroke object called dkgrayStrokeThick and set its color (#4D4D4D) and its weight (5). Create a second SolidColorStroke object called dkGrayStrokeThin and set its color (#4D4D4D) and its weight (1). In the ColumnChart verticalAxisRenderer, set axisStroke and tickStroke to the dkgrayStrokeThick and dkgrayStrokeThin objects. Set the horizontalAxisRenderers property to an instance of the AxisRenderer class and set its axis to expenseAxis and its stroke styles the same as the other renderer.

Your declarations should appear as shown here:

<s:SolidColorStroke id="dkgrayStrokeThick" color="#4D4D4D" weight="5"/> <s:SolidColorStroke id="dkgrayStrokeThin" color="#4D4D4D" weight="1"/>

Your axis renderers should appear as shown here:

<mx:verticalAxisRenderers> <mx:AxisRenderer verticalAxisTitleAlignment="vertical" axis="{amountAxis}" axisStroke="{dkgrayStrokeThick}" tickStroke="{dkgrayStrokeThin}"/> </mx:verticalAxisRenderers> <mx:horizontalAxisRenderers> <mx:AxisRenderer axis="{expenseAxis}" axisStroke="{dkgrayStrokeThick}" tickStroke="{dkgrayStrokeThin}"/> </mx:horizontalAxisRenderers>

Run the application. The axes and ticks should now be dark gray (see Figure 5).

Change the axis and tick colors.
Figure 5. Change the axis and tick colors.

Step 6: Format axis labels as currencies.

In the Declarations block, create a Spark CurrencyFormatter called moneyFormatter and set its properties to format in your currency. In the LinearAxis tag, set its labelFunction to axisMoneyFormatter. In the Script block, include the formatter function shown below.

Here is an example of currency displayed in US dollars with no decimal places:

<s:CurrencyFormatter id="moneyFormatter" useCurrencySymbol="true" currencySymbol="$" fractionalDigits="0"/>

Your LinearAxis should appear as shown here:

<mx:LinearAxis id="amountAxis" title="Amount" minimum="0" maximum="500000" labelFunction="axisMoneyFormatter"/>

Here is the formatter function to place in your Script block:

import mx.charts.chartClasses.IAxis; protected function axisMoneyFormatter(labelValue:Object, previousValue:Object, axis:IAxis):String{ return moneyFormatter.format(labelValue); }

This function is called by the chart component for every label on the vertical axis. Its method signature (its arguments and return type) are defined by the component using it. You can look up the method's required signature in the API for the LinearAxis class.

Run the application. The vertical axis labels should now be formatted as currencies (see Figure 6).

Format the vertical axis labels as currencies.
Figure 6. Format the vertical axis labels as currencies.

Step 7: Format data tips.

Set the PieChart dataTipFunction to formatDeptPieTips. In the Script block, include the formatter function shown below.

Your PieChart opening 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)" dataTipFunction="formatDeptPieTips">

Here is the formatter function to place in your Script block:

import mx.charts.HitData; protected function formatDeptPieTips(hitData:HitData):String{ return "<b>"+hitData.item.name+"</b><br/>"+"Budget: " +moneyFormatter.format(hitData.item.budget); }

This function is called by the chart component before displaying every data tip. Just as for the labelFunction in the last step, its method signature is defined by the component using it, the PieChart, and you can look it up in the API for the PieChart class. Notice you can use basic HTML formatting in this function.

Run the application. The pie chart data tips should now be formatted to display currency amounts (see Figure 7).

 Format data tips.
Figure 7. Format data tips.

In this module you learned to add charts to your Flex application. You used a pie chart and a column chart and accomplished all the common customization tasks including drilling down into data; animating data changes; and formatting fills, axes, titles, labels, and data tips. 

This concludes your Test Drive of Flash Builder 4.5. In less than a day, you learned to build a Flex application that retrieves, displays, updates, adds, and deletes data in a database and you learned to debug, deploy, customize, and add charts to this application (see Figure 8). To build on your knowledge, continue on to the Flex in a Week training.

 Browse the finished application.
Figure 8. Browse the finished application.

Learn more

Documentation: Using Flex 4.5

  • Formatting charts
  • Displaying data and labels in charts
  • Using data tip objects
  • Spark formatters

ActionScript 3 Reference

  • mx.charts
  • mx.charts.effects
  • spark.formatters

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: Build an application in an hour
  • Flex Test Drive: Change the appearance of your application
  • Flex Test Drive: Test and debug your code
  • Flex Test Drive: Modify the database
  • Flex Test Drive: Test and debug your code
  • Flex Test Drive: Modify the database
  • Flex Test Drive: Change the appearance of your application
  • 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

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