Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
ActionScript Graphing Cookbook
ActionScript Graphing Cookbook

ActionScript Graphing Cookbook: Learn how to create appealing and interactive visual presentations of your data in ActionScript with this book and ebook.

eBook
$26.09 $28.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

ActionScript Graphing Cookbook

Chapter 2. Working with Data

In this chapter, we will cover:

  • Preparing your data

  • Embedding CSV files

  • Loading a data file from the Internet

  • Loading a file from the local hard drive

  • Loading data with XML

  • Loading data with Excel files

  • Consuming the REST services

  • Exporting data as CSV to import in Excel

  • Exporting data to a PDF file

Introduction


In the previous chapter we looked at the basics of graph drawing. We investigated the different ActionScript features that allow us to draw graphs of various shapes and sizes. We also looked at how to structure the data to easily create graphical representations.

In this chapter, we are going to investigate how to get that data into your program. Various sources will be discussed. These range from plain files to remote web services.

The final recipes discuss the opposite, extracting the data from your application into a format that other programs can use.

Preparing your data


This first short recipe discusses some general rules that will make your live easier when dealing with graph data.

It also sets you up with a few test files to use throughout the chapter.

Getting ready

Most data in graphs has a tabular form and will usually originate either from a spreadsheet or a database. It's possible to process pretty much any input format with ActionScript 3.0, but some will be easier than others. We will focus on data available in some spreadsheet format. See the There's more... section of this recipe for a discussion on databases.

The spreadsheet examples use Microsoft Excel because it is probably the most wide-spread application, but you can use any other spreadsheet application such as the free OpenOffice, or LibreOffice spreadsheet, or even the spreadsheet functionality in Google Docs.

For CSV (comma separated values) and XML files even a plain text editor, such as Notepad, will suffice.

How to do it...

  1. Start by creating a text file, named data.csv...

Embedding CSV files


In this recipe, we will demonstrate how to embed the CSV file shown in the previous recipe, how to read and parse the data in that file, and ultimately how to display the data in the file.

Getting ready

Create a new project for this chapter and place the three datafiles from the previous recipe in the lib folder of the project (data.csv, data.xlsx and data.xml). Copy the Graph and Legend classes from the previous chapter into the src folder. We will be using those again.

If you didn't complete the chapter, you can just start from the example project supplied with the book.

To avoid excessive code, we will also use the StringHelper class from the Adobe ActionScript live docs that can be found at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#includeExamplesSummary. Create a class called StringHelper in the src folder and copy the code into it.

Now create a new Recipe1 class in the src folder of your project:

package  
{
    import flash.display...

Loading a data file from the Internet


This recipe looks at using the URLLoader class to load a CSV data file from the Internet. It's very similar to the previous recipe, but it gives you the ability to dynamically load files from the Internet. Thus you can update the files without needing to change the ActionScript program.

Getting ready

Make sure you have a copy of the data.csv file somewhere on the Internet. If you don't have a site, you can use DropBox as described in the first recipe of this chapter. This recipe will use a link to my own DropBox folder, but it's much more instructive if you can use your own and change the data to see the effect.

Start by creating a copy of the previous Recipe1 class and name it Recipe2. This will be our basis.

How to do it...

In the following steps, we'll replace the embed code with code that will load the file from the Internet:

  1. Remove the embed code and the DataClass property. We won't need these anymore.

  2. Now move the parsing and function drawing code into...

Loading a file from the local hard drive


In the previous recipe, we've already seen one way of loading a file from the hard drive through relative URLs. There are a few shortcomings to that approach though, as follows:

  • If the Flash application is on a website, you cannot use the URLLoader class to load a file from the user's hard drive

  • Even if this was possible, we'd need to implement something so that the user can choose which file to load

In this recipe, we will demonstrate how to use a file dialog to let the user select which file to show.

Getting ready

Again, copy and paste the previous recipe's code into a new class called Recipe3. Rename the class name and the constructor and remove the three lines that create and start the URL loader. Leave the onComplete method as we will re-use it in this recipe.

package  
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.HTTPStatusEvent;
    import flash.events.MouseEvent;
    import flash.net.FileFilter;
   ...

Loading data with XML


In this recipe, we'll rewrite the embedded recipe so it can read XML files. ActionScript 3.0 has very thorough support for the XML format, so you may notice that this recipe is the easiest one of the entire chapter.

Because so much data, these days, is transmitted in XML or variants such as RSS, it may also prove to be the most useful.

Getting ready

Create a Recipe4 document class and have it extend Sprite. Set up the Graph class as in the previous recipes:

package
{
    import flash.display.Sprite;
    public class Recipe4 extends Sprite
    {
        private var graph:Graph;

        public function Recipe4() 
        {
           graph = new Graph( -50, 550, 750, -50);
           addChild(graph);
           graph.drawHorizontalAxis(0, 0, 700, 50, ["0", "700"]);
           graph.drawVerticalAxis(0, 0, 500, 50, ["0", "250", "500"]);

        }

    }

}

How to do it...

  1. Embed the XML file and associate a class with it. If you use FlashDevelop's Generate Embed Code option...

Loading data from Excel files


Lots of chart data originates in Excel, so it is only natural to want to directly read Excel files. In general, reading Excel files can be complicated and error prone. Many of the tools available for ActionScript are also fairly experimental. So for most applications, you will probably want to use CSV or XML files as mentioned in the previous recipes, or JSON as we'll discuss in the next recipe.

However, in circumstances where you have good control over the version(s) of Excel used and the layout of the Excel spreadsheet, reading them directly could be a huge time-saver for your users.

Getting ready

For this recipe, we will use the freely available ActionScript 3 XLSX reader by Ben Morrow. It can be downloaded from the GitHub page, https://github.com/childoftv/as3-xlsx-reader. If you are comfortable using Git and GitHub, it is easiest to clone the repository on your local drive. If you don't have the Git software installed, it is best to use the Downloads link...

Consuming the REST services


Many websites expose their data through web services. These services usually fall into two categories, SOAP or REST. Many data APIs now tend to prefer the more lightweight REST, which is what we will use in this recipe.

A REST service will usually provide its data in the XML or JSON format. JSON is actually a way of representing data in a piece of JavaScript code. This makes it easy for JavaScript applications to use the data.

For instance, the data we've previously used could look like the following code snippet:

{
  "data" : {
    "0":   [20, 50],
    "50":  [70, 40],
    "100": [0, 100],
    "150": [150, 150],
    "200": [300, 200],  
    "250": [200, 170],
    "300": [170, 160],
    "350": [20, 120],
    "400": [60, 80],
    "450": [250, 150],
    "500": [90, 20],
    "550": [50, 40],
    "600": [110, 90],
    "650": [150, 150],
    "700": [320, 200]
  }
}

You'll notice many similarities with ActionScript. More information on the JSON format can be found at http...

Exporting data as CSV to import in Excel


Until now, we've always read files and displayed them. In the final recipes of the chapter, we will show how to export the data that we have available inside the program. This can be useful if you want to allow further processing of the data in other programs.

For instance, imagine that you import and process data from a REST service. Users may want to take that data out of the application and use it in Excel to perform their own calculations or create a custom graph.

This recipe looks at exporting to the CSV format that can be used to load the data in Excel and other spreadsheet programs.

Getting ready

For this recipe, we will start from a simple data array, but of course the data could be coming from anywhere.

Start by creating a Recipe7 document class:

package  
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.FileReference;
    import flash.text.TextField;
    import flash...

Exporting data to a PDF file


Except for extracting the data for further processing, it is also typical that you would want to create reports of the data. In this case, PDF is one of the most popular formats, because it looks virtually identical on every system.

Generating PDF files is a huge topic, so we can't show everything, but this recipe should get you started.

Getting ready

We will use the purePDF library to create the PDF file. It can be found at http://code.google.com/p/purepdf/. Download the library through the download link. At the time of this writing, purePDF_0.77.20110116.zip was the current version.

The library comes packaged as an SWC class library. To use these in FlashDevelop, move both files into the lib folder of your project. Inside FlashDevelop, right-click on the files and choose Add to Library. Their filenames should turn blue in the project view.

Now create a Recipe8 document class and copy the same starting code from the previous recipe.

How to do it...

Add the export...

Left arrow icon Right arrow icon

Key benefits

  • Learn how to create various visually dynamic graphs and charts
  • Import data from different sources, such as web services
  • Learn how to map geographical data, visualize in 3D

Description

"A picture is worth a thousand words" has never been more true than when representing large sets of data. Bar charts, heat maps, cartograms, and many more have become important tools in applications and presentations to quickly give insight into complicated issues.The "ActionScript Graphing Cookbook" shows you how to add your own charts to any ActionScript program. The recipes give step-by-step instructions on how to process the input data, how to create various types of charts and how to make them interactive for even more user engagement.Starting with basic ActionScript knowledge, you will learn how to develop many different types of charts.First learn how to import your data, from Excel, web services and more. Next process the data and make it ready for graphical display. Pick one of the many graph options available as the book guides you through ActionScript's drawing functions. And when you're ready for it, branch out into 3D display.The recipes in the "ActionScript Graphing Cookbook" will gradually introduce you into the world of visualization.

Who is this book for?

The "ActionScript Graphing Cookbook" is aimed at any ActionScript developer who wants to add data visualization to their skill set. The reader should be familiar with ActionScript basics, but no deep knowledge of any graphical functions is required.

What you will learn

  • Import data from various sources
  • Organize the various visual elements of a graph
  • Draw many types of charts, such as bar, line and pie charts, meters and many more
  • Make graphs interactive with hover and zoom effects
  • Work with geographical data and maps
  • Animate graphs with real time data
  • Show and interact with network displays
  • Discover three dimensional drawing and graphing
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 21, 2012
Length: 288 pages
Edition : 1st
Language : English
ISBN-13 : 9781849693202
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Nov 21, 2012
Length: 288 pages
Edition : 1st
Language : English
ISBN-13 : 9781849693202
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 153.97
C++ Game Development By Example
$49.99
ActionScript Graphing Cookbook
$48.99
HTML5 Graphing and Data Visualization Cookbook
$54.99
Total $ 153.97 Stars icon

Table of Contents

10 Chapters
Getting Started with Graph Drawing Chevron down icon Chevron up icon
Working with Data Chevron down icon Chevron up icon
Creating Bar Charts Chevron down icon Chevron up icon
Drawing Different Types of Graphs Chevron down icon Chevron up icon
Adding Interaction Chevron down icon Chevron up icon
Mapping Geographical and Spatial Data Chevron down icon Chevron up icon
Animating a Graph Chevron down icon Chevron up icon
Creating a Relational Network Chevron down icon Chevron up icon
Creating Three-Dimensional Graphs Chevron down icon Chevron up icon
Working with Various 3D Graph Types Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Igor Jan 29, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book has all that you need for creating math graphs. The concept of the book is learning by exampleif you learn this way than this is book for you. Every problem is firstly explained and then the solutionfor the problem is presented with code. There are some more useful information's after that.This book does not concentrate on explaining how sprites, movieclips and animation can bedrawn with ActionScript it is mainly for graphs that are used in different fields statistics,mathematics and other similar fields. So if you need that than this is book for you.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon