ActionScript Graphing Cookbook
上QQ阅读APP看书,第一时间看更新

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 its own function (don't forget to remove the original code in the constructor):
    private function onComplete(event:Event):void {
        var data:Array = parseCSV(event.target.data);
        for (var i:int = 0; i < data.length; i++)
        {
          graph.drawPoint(data[i][0], data[i][1]);
          graph.drawPoint(data[i][0], data[i][2], 0x9933ff);
        }
    }
  3. And finally replace the code you removed with the URLLoader class:
    var loader:URLLoader = new URLLoader();
    loader.load(new URLRequest("http://dl.dropbox.com/u/2497061/data.csv"));
    loader.addEventListener(Event.COMPLETE, onComplete);
  4. Run the program and you should see the same graph as in the previous chapter. Only now, it might take a few seconds before the file is downloaded and it appears.

How it works...

This recipe relies completely on the URLLoader class in ActionScript. This class makes it possible to load files from the Internet. Because loading files across a network might take some time, the class uses events to tell us what's happening.

In the example program we only listen to the Event.COMPLETE event. The URL loader will fire this event when loading of the resource is completed.

Notice that when moving the parsing code, we also changed the source of text data. Instead of using the embedded class, we use the data that is supplied by the COMPLETE event. This data property holds the raw data that was loaded from the URL.

There's more...

The code in this recipe only scratches the surface of the URLLoader class. Before you use it in a real program, it is best to enhance the application a little.

Other events

The URLoader class will fire many more events, some of which might be important for the correct functioning of your program:

  • IOErrorEvent.IO_ERROR: This event is triggered when something happens during transfer. If this event occurs, no COMPLETE event will follow, so it's best to listen to this one so you can show the user a friendly error message.
  • HTTPStatusEvent.HTTP_STATUS: This event allows you to react intelligently to a few problems that may occur at the server side. For instance, a 404 HTTP status means the file was not found.
  • ProgressEvent.PROGRESS: This event can be used to track the progress of the download. This is useful for big files.

Relative URL

In this recipe, we have used an absolute URL pointing to a DropBox folder. It is also possible to use a relative URL, for instance, new UrlRequest("../lib/data.csv"). In that case, the application will try to locate the file in the folder where it is running.

Security and cross-domain policies

If you try to put the application as it is on a random server, you will notice that it cannot load the data file. This is due to security restrictions inside Flash. By default, an ActionScript application can only access resources on exactly the same domain. If you want to read a file on another domain, that domain needs to have a cross-domain policy that allows access. Because we can't access nor change the DropBox cross-domain policy, we cannot make this recipe work in the current configuration. You will need to move the file to your own servers.

See also