<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.23133-</id>
  <updated>2009-11-05T20:18:18Z</updated>
  <title>Comments for Ultra-lightweight Charts For AJAX (http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23133</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=23133" title="Ultra-lightweight Charts For AJAX" />
    <published>2008-03-14T13:12:54Z</published>
    <updated>2008-03-14T15:07:29Z</updated>
    <title>Ultra-lightweight Charts For AJAX</title>
    <summary>
To continue the theme of using seamless Flash/Flex/AJAX applications, here&apos;s a really simple example how you can use Flash and AJAX together seamlessly within your applications.  Yes, i said Flash, not Flex.   In this example I created an ultra-lightweight ActionScript-only charting project that can be used within AJAX applications.  </summary>
    <author>
      <name>Andrew Trice</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<div class="ap_r" style="margin: 0pt 16px 16px;"><a href="http://www.insideria.com/upload/2008/03/ultra-lightweight_charts_for_ajax/flasAjax.png" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/upload/2008/03/ultra-lightweight_charts_for_ajax/flasAjax.png"  alt="flasAjax.png" title="Click to enlarge" width="148"></a></div>
<p>To continue the <a href="http://www.insideria.com/2008/02/when-to-use-flex-ajax.html" target="_blank">theme of using seamless Flash/Flex/AJAX applications</a>, here's a really simple example how you can use Flash and AJAX together seamlessly within your applications.  Yes, i said Flash, not Flex.   In this example I created an ultra-lightweight ActionScript-only charting project that can be used within AJAX applications.   </p>

<p>One complaint that people have with Flash/Flex applications is that the file size is too large.   There are a few things you can do to minimize that.   In Flex 3, you can use <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=rsl_09.html" target="_blank">cached framework Runtime Shared Libraries (RSL)</a> to help reduce the file size of Flex applications.  Another solution is that you can just use ActionScript-only projects (non-flex), which are significantly smaller than Flex projects.   

<p>One thing to keep in mind with ActionScript-only projects:  You have to build everything you need yourself.  One of the major benefits of the Flex framework is that it has a large number of pre-existing controls to help in your development tasks.   It is a huge productivity boost.  ActionScript projects do not have the Flex libraries, so they are much smaller, but also have no access to the controls supplied within the Framework.</p>

<p>I wanted to quickly show how you can create a small-footprint dynamic Flash based chart within an AJAX application.  In this example, I created a basic charting component that communicates with the HTML container through <a href="http://livedocs.adobe.com/flex/2/langref/flash/external/ExternalInterface.html" target="_blank">ExternalInterface</a> and JavaScript.  Here, we are only using Flash for its graphical capabilities.  </p>

<p>When I say "small footprint", I really mean it.  The compiled swf file in this example is only 1813 <u><em><strong>Bytes</strong></em></u>.  Thats 1.78 KB, or .00173 MB.  This also happens to be smaller than your typical jpg image used for a chart.</p>

<p>You can see the output here:   This is actually four instances of the chart object.
<iframe src="http://www.tricedesigns.com/portfolio/ajaxcharts/AJAXCharts.html" width="400" height="440" style="border: 0 none"></iframe></p>

<p>You can click on the "generate data" button to push new data into the chart controls.</p>

<p>Now, on to what makes this work... When creating the ActionScript project, the application extends the basic "Sprite" class, which has everything you need for creating visual components.   </p>

<p>One of the first things you will need to do is register the callbacks so that your JavaScript can communicate with your Flex applications.</p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">private</span> <span class="category1">function</span> registerJSCallbacks() : <span class="category1">void</span>
{
 	ExternalInterface.addCallback( "<span class="quote">setData</span>", <span class="category2">setData</span> );
 	ExternalInterface.addCallback( "<span class="quote">setStyle</span>", <span class="category2">setStyle</span> );
}</pre>
</code>

</div></div> 

<p>This exposes two methods on the Flash application: "setData" and "setStyle".   "setStyle" is used to define how the chart will be dispalyed.  "setData" will be used to pass data into the charting components.
Here's "setData" in ActionScript:  You can see that it sets up all the style values for rendering the chart.  Once the styles are set, it then calls the "drawChart" function, which actually renders the chart.</p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">private</span> <span class="category1">function</span> <span class="category2">setStyle</span>( style : <span class="category2">Object</span> ) : <span class="category1">void</span>
{
 	<span class="category2">trace</span>( "<span class="quote">setStyle</span>", style );
 	<span class="category1">this</span>.styles = style ? style : {};
 	
 	<span class="category1">if</span> ( !styles.style )
 		styles.style = DEFAULT_STYLE;
 		
 	<span class="category1">if</span> ( !styles.bgcolor )
 		styles.bgcolor = DEFAULT_BG_COLOR;
 	
 	<span class="category1">if</span> ( !styles.fgcolor )
 		styles.fgcolor = DEFAULT_FG_COLOR; 
 		
 	<span class="category1">if</span> ( !styles.thickness )
 		styles.thickness = DEFAULT_THICKNESS; 
 	
 	<span class="category1">if</span> ( !styles.<span class="category2">min</span> )
 		styles.<span class="category2">min</span> = DEFAULT_MIN; 
 		
 	<span class="category1">if</span> ( !styles.<span class="category2">max</span> )
 		styles.<span class="category2">max</span> = DEFAULT_MAX; 
 		
 	<span class="category1">if</span> ( !styles.tips )
 		styles.tips = DEFAULT_TIPS; 
 
 	drawChart();
}</pre>
</code>

</div></div> 

<p>In javascript, this is invoked by the following:  You can see that it creates a style object, and passes the style object to the exposed "setStyle" method on the swf.</p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">var</span> swf = document.getElementById( '<span class="quote">AJAXCharts1</span>' );
<span class="category1">var</span> style = {style:"<span class="quote">line</span>", bgcolor:0xFFFFFF, fgcolor: 0x000000}
swf.<span class="category2">setStyle</span>( style );</pre>
</code>

</div></div> 

<p>The other publicly exposed function on the ActionScript project is "setData".  This function is actually quite simple;  It sets the internal private variable "data" and invokes the "drawChart" function.</p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">private</span> <span class="category1">function</span> <span class="category2">setData</span>( <span class="category2">data</span> : * ) : <span class="category1">void</span>
{
 	<span class="category1">this</span>.<span class="category2">data</span> = <span class="category2">data</span> as <span class="category2">Array</span>;
 	drawChart();
}</pre>
</code>

</div></div> 

<p>In JavaScript, this is invoked by the following code:  You can see that it creates a data array, and passes the the array to the exposed "setData" method on the swf, which invokes the ActionScript function above.</p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
<span class="category1">function</span> generateData()
{
 	<span class="category1">var</span> <span class="category2">data</span> = [];
 	
 	<span class="category1">for</span> ( <span class="category1">var</span> <span class="category2">x</span>=0; <span class="category2">x</span> &lt; 15; <span class="category2">x</span>++ )
 	{
  		<span class="category2">data</span>.<span class="category2">push</span>( <span class="category2">Math</span>.<span class="category2">random</span>() * 100 );	
  	}
 	
 	<span class="category1">var</span> swf = document.getElementById( '<span class="quote">AJAXCharts1</span>' );
 	swf.<span class="category2">setData</span>( <span class="category2">data</span> );
}</pre>
</code>

</div></div> 

<p>I'm not going to dig to far into the "drawChart" function now, but I'll just say that it handles all of the actual rendering of the charts.  My primary focus for this post was the JavaScript/ActionScript communication.   "drawChart" programmatically uses moveTo, lineTo, drawRect, and drawCircle to render the chart visualizations.   You can dig into the source here:</p>

View Application Source:<br/>
<a href="http://www.tricedesigns.com/portfolio/ajaxcharts/srcview/" target="_blank">http://www.tricedesigns.com/portfolio/ajaxcharts/srcview/</a>
<br/><br/>
Download Application Source:<br/>
<a href="http://www.tricedesigns.com/portfolio/ajaxcharts/srcview/AJAXCharts.zip" target="_blank">http://www.tricedesigns.com/portfolio/ajaxcharts/srcview/AJAXCharts.zip</a>
<br/><br/>
<p>This example shows how to use this technique to build your own lightweight custom charts.  I put this together in a few hours.   If you are looking for an existing, and more feature rich flash-based charting framework for AJAX applications, try looking at <a href="http://developer.yahoo.com/yui/charts/" target="_blank">Yahoo!'s YUI charts</a> (which still has a small footprint of 121 KB).</p>


]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23133-comment:2015879</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23133" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html#comment-2015879" />
    <title>Comment from Josh Tynjala on 2008-03-14</title>
    <author>
        <name>Josh Tynjala</name>
        <uri>http://developer.yahoo.com/flash/</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://developer.yahoo.com/flash/">
        <![CDATA[<p>I wish I could get the YUI Charts down to 1813 bytes. Haha.</p>

<p>To clarify the filesize a bit, the SWF for charts in YUI 2.5 is 59 KB. The supporting JavaScript specifically for charts is 17 KB (totaling 76 KB). The rest comes from dependencies in the core YUI library.</p>]]>
    </content>
    <published>2008-03-14T21:35:23Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23133-comment:2015880</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23133" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html#comment-2015880" />
    <title>Comment from Andrew Trice on 2008-03-14</title>
    <author>
        <name>Andrew Trice</name>
        <uri>http://www.tricedesigns.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.tricedesigns.com">
        <![CDATA[<p>Thanks for the clarification Josh.  Even with the core dependencies, it's still tiny.   </p>]]>
    </content>
    <published>2008-03-14T21:54:25Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23133-comment:2017522</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23133" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html#comment-2017522" />
    <title>Comment from Television Spy on 2008-06-03</title>
    <author>
        <name>Television Spy</name>
        <uri>http://whatareyouwatching.uni.cc</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://whatareyouwatching.uni.cc">
        <![CDATA[<p>Doesn't this depend on CANVAS - currently only supported in firefox?</p>]]>
    </content>
    <published>2008-06-03T19:53:27Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23133-comment:2017524</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23133" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html#comment-2017524" />
    <title>Comment from Andrew Trice on 2008-06-03</title>
    <author>
        <name>Andrew Trice</name>
        <uri>http://www.tricedesigns.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.tricedesigns.com">
        <![CDATA[<p>It is actually using Flash to perform all of the programmatic drawing.   There is communication between the AJAX and Flash components using the Flash ExternalInterface class.  It works in multiple browsers, and on multiple platforms.</p>]]>
    </content>
    <published>2008-06-03T20:40:51Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23133-comment:2018602</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23133" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html#comment-2018602" />
    <title>Comment from Glenn on 2008-07-09</title>
    <author>
        <name>Glenn</name>
        <uri>http://HotelMapSearch.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://HotelMapSearch.com">
        <![CDATA[<p>I am working on using Flash components to do some of the heavy graphical rendering on our site.  This was exactly the kind of example I needed to do some basic concept testing.  Thank you for providing such a succinct example.</p>]]>
    </content>
    <published>2008-07-09T13:39:53Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23133-comment:2018604</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23133" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html#comment-2018604" />
    <title>Comment from Glenn on 2008-07-09</title>
    <author>
        <name>Glenn</name>
        <uri>http://HotelMapSearch.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://HotelMapSearch.com">
        <![CDATA[<p>I am working on using Flash components to do some of the heavy graphical rendering on our site.  This was exactly the kind of example I needed to do some basic concept testing.  Thank you for providing such a succinct example.</p>]]>
    </content>
    <published>2008-07-09T13:41:00Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23133-comment:2056915</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23133" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/03/ultralightweight-charts-for-aj.html#comment-2056915" />
    <title>Comment from Steve on 2009-04-01</title>
    <author>
        <name>Steve</name>
        <uri>http://chart.inetsoft.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://chart.inetsoft.com">
        <![CDATA[<p>You can also get flash-level advanced charting with Ajax only at <a href="http://chart.inetsoft.com">style chart</a></p>]]>
    </content>
    <published>2009-04-01T15:20:49Z</published>
  </entry>

</feed
