Wednesday, June 27, 2007

Work with Open Flash Charts

After a longer-than-expected struggle to find a decent charting library, it looks like we will building our own, using Open Flash Charts as a starting point.

OFC already comes with a nice set of features... Plus, and most importantly, it's open-source and free, meaning we can do whatever we want with it (or at least whatever GPL allows us to do).

Although a great part of the library is organized in classes, a significant part of OFC is just bundled on the .FLA file, namely code that is shared by all types of charts available. This has some disadvantages:
  1. Encourages "spaghetti code", stuffing everything with no specific organization into the Flash file instead of in a separate ActionScript file.
  2. Doesn't make concurrent development easy: having a diff done on an Actionscript file is readable, while doing it on a .FLA file isn't.
  3. Does not allow development inside Eclipse. See below why coding Actionscript is much easier in Eclipse than in the Flash authoring environment itself.
Having said this, I have already successfully translated all the code in the .FLA into a separate class file, maintaining all existing functionalities, plus the ability to have Javascript links associated to each chart value. Such a simple task took a lot of my time, until I found that when embedding flash objects in HTML, you need to set:
<param name="allowScriptAccess" value="always" />

instead of

<param name="allowScriptAccess" value="sameDomain" />
if you want to test your files locally. Otherwise SWF movies will not be able to communicate with Javascript through getURL() or ExternalInterface.call(). Unfortunately it took a long time to find this on the web:P
I also changed the way in wich parameters are passed to the SWF movie. Instead of loading them from an external file as it's done in OFC, e.g. using:

<param name="movie" value="open-flash-chart.swf?data=mydata.txt" />

they are now passed as flashVars themselves, e.g.:

<param name="movie" value="open-flash-chart.swf?values=2,5,1&x_labels=Mon,Tues,Wed" />

This allows us to fetch information dynamically, for example with AJAX, and pass it directly to the movie without the need to create an external file.


As for the question "Why is it better to code Actionscript in Eclipse IDE?", once you try the FDT Eclipse plugin you'll understand... In short, it gives you all the functionalities you would have if you were programming in Java. Actually, there's no advantage in using it if you don't go object-oriented, but otherwise it gives you auto-completion, error highlighting, quickfixes, among others... I really recommend this, or at least some other tool that does the same job (if you know of some open-source alternative let me know).
You'll end up using the Flash authoring environment just for publishing your files.

Next steps:
  • decide on the best way to pass parameters to Flash from Javascript (this is already possible but it can probably be improved)
  • have OpenMRS building the code to include the open flash chart
  • create pie charts! (not available in OFC)
  • whatever else comes...

7 comments:

Paul Biondich said...

Hugo, I think another validation for actionscript is that it appears to be essentially the heir apparent ultimate prototype for javascript 2.0. It seems like a safe bet for the future as well.

monk.e.boy said...

Why not communicate with the Open Flash Chart team? Isn't open source all about helping each other?

Could you point me at your changes please (http://teethgrinder.co.uk/open-flash-chart/contact.php)

I was interested in your comments, especially the .fla vs .as. I agree. I also agree with passing data through get variables (or using javascript)

Your knowledge would be invaluable for Open Flash Chart - join up!!! Email me! Tell me where we're going wrong! send patches!

DO IT! (please)

Thanks,

monk.e.boy

monk.e.boy said...

oooh, i forgot:

http://teethgrinder.co.uk/open-flash-chart/gallery-js-2.php

for AJAX and stuff.

monk.e.boy

Unknown said...
This comment has been removed by the author.
Unknown said...

You can download a fixed .net version at:
View Demo

Unknown said...

Also A new application using the graphing tool that monitors your web server performance:
Site Stalker

Unknown said...

This blog is really interesting, actually i must to say i prefer flash because is a fast program and easy to use. I don´t like the complex program. I just looking for one that be eficient all the time and flash it is. that is why i prefer to buy viagra because i always have a great result in my sexual life.