Monday, April 23, 2007

Scientific Graphing with Flex

Anyone who has ever asked me "Why would you want to use a player based solution instead of AJAX?" has likely gotten an ear-full of talk about real-time drawing of vector graphics and the bitmap manipulation APIs. These capabilities are truly what differentiate Flash/Flex and Silverlight(WPF/e) from AJAX and DHTML solutions. They open up entirely new realms of possibilities for your applications. The vector drawing and image manipulation capabilities lay the foundation for rich experiences. These capabilities enable everything from rich charting, tweens and animations, to video playback. In essence, they provide the gateway to the next level of interactivity, analytical power, and engaging experiences.

Let's take a simple piece of code in AS3, and build upon that:
var g : Graphics = this.graphics;
g.moveTo( 25, 25 );
g.lineTo( 25, 50 );
This example is about as basic as you can get. It simply draws a line from the point (25,25) to the point (25,50). Despite the simplicity of this code, it is extremely powerful. The feature-rich charting capabilities of the Flex framework rely heavily upon code that originates from this basic scenario. It all boils down to being able to draw vector graphics at run time. Even better, you have the power to build custom graphing or charting components yourself; you are not limited to what is provided by the Flex framework.

Last night, I randomly had the idea... "I wonder how hard it would be to create a scientific graphing component in Flex...". Back in high school, the TI-82 was an indispensable tool if you wanted to pass algebra, trigonometry, and calculus classes. I have always been fascinated by geometry and graphing, so this is just a natural progression for me. In a matter of hours this morning it had come to life. The great part is that this entire application was built in less than 250 lines of code (including whitespace).



You can check it out here:In the bottom right corner, there is a text area where you can enter equations. You can have it display multiple at a time. You just need to enter the "x-side" of the equation. For example, to graph the equation y=x, you would just need to enter "x". To graph the equation y=x*x, you would just enter "x*x". Here are the rules:
  • Use the operators "+", "-", "*", "/"
  • You can use parentheses "(" and ")"
  • You can use math functions such as (but not limited to) "Math.sin(x)", "Math.cos(x)", and "Math.sqrt(x)".
There are a few bugs in there, but hey, I just threw this together this morning. It's not quite as robust as my trusty TI-82; this is just to show what can be done using a player-based runtime. I believe that this is a perfect of example of what you can do to create rich applications. The Flash platform enables us to quickly and easily build powerful real-time analytical tools to meet our business needs... Talk about a competitive advantage.

7 Comments:

Blogger James said...

Hey Andrew, I was wondering do you use FLEX only to do this or did you embed a swf?

Wed Jul 11, 02:21:00 PM EDT  
Blogger Andrew Trice said...

It's all flex. It actually uses ExternalInterface to use javascript's eval statement since AS3 doesn't allow you to do an actual eval. Since it uses js eval, its a bit slower than using as3, but in order to do this in as3, you'd need to create an expression parser. I just did this for a blog post to show the power of the drawing API, so I didn't bother creating a parser (which could be a pretty big undertaking). Javascript did the lifting for me.

-Andy

Wed Jul 11, 03:38:00 PM EDT  
Blogger Bling said...

Hi Andrew! What is your email? I would like to ask you some questions about the project "Capturing still images from a webcam". Please mail me at:

garfield @ post5 . tele . dk

Thanks!

Kind regards
Martin

Thu Aug 23, 11:12:00 PM EDT  
Blogger Romeo Pasion said...

Hey andrew i really like your blog and every flash tutorials that you have published. Do you have any experience in flash chatroom using mediaserver2? well you see i have a bit of problem in my lobby room.


-romeopasion

Sat Sep 08, 06:16:00 AM EDT  
Blogger Jagadish said...

Hi,
Cool application. Is it all just moveto and lineto? Or have you done something to get such smooth curves? I guess that just linetos and movetos will not give you such overall smooth curves.

Sat Feb 02, 05:39:00 AM EST  
Blogger Jagadish said...

To follow up on my last comment, my problem is that I don't get smooth antialiased curves( like the ones you have) by joining line segments, or for that matter using curvetos, if I use a custom scale that is very much different from the default scale.

I am mapping the height or width( whichever is smaller) to 10 units ( -5 to 5 ). And setting the origin at the center. I am drawing on a Flex Canvas component called ‘pnl’.

var w:int = pnl.width;
var h:int = pnl.height;
var cscale:Number = (w < h)?w/10.0:h/10.0;
var boxMat:Matrix = new Matrix();
boxMat.createBox(cscale,-cscale,0,w*0.5,h*0.5);
pnl.transform.matrix = boxMat;

Now with this mapping if I try to draw some curve it is not smoothly joined. i don't understand why this should be so because internally flex or flash must be converting everything to the pixel coordinates scale.

Wed Apr 09, 02:30:00 PM EDT  
Blogger digcode said...

Well done job.
Can i get source code? =)
thx

Tue Nov 11, 12:07:00 PM EST  

Post a Comment

<< Home