<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rails on the Run &#187; charts</title>
	<atom:link href="http://railsontherun.com/tag/charts/feed/" rel="self" type="application/rss+xml" />
	<link>http://railsontherun.com</link>
	<description>Rails experiments by Matt Aimonetti</description>
	<lastBuildDate>Tue, 23 Feb 2010 07:28:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Googlecharts 1.5.1</title>
		<link>http://railsontherun.com/2010/02/03/googlecharts-1-5-1/</link>
		<comments>http://railsontherun.com/2010/02/03/googlecharts-1-5-1/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 05:18:23 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Gems & Libs]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[googlecharts]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[google charts]]></category>

		<guid isPermaLink="false">http://railsontherun.com/?p=1722</guid>
		<description><![CDATA[To celebrate the relaunch of this site and since we are waiting for Rails 3.0 beta to be released, I figured I should share with you what I worked on the other night.
I merged patches, refactored and released a new version of googlecharts, my Gem to create graphs using Google Chart API.
sudo gem install googlecharts
Here [...]]]></description>
			<content:encoded><![CDATA[<p>To celebrate the relaunch of this site and since we are waiting for Rails 3.0 beta to be released, I figured I should share with you what I worked on the other night.</p>
<p>I merged patches, refactored and released a new version of googlecharts, my Gem to create graphs using Google Chart API.</p>
<p><code>sudo gem install googlecharts</code></p>
<p>Here is a quick example of how the API works when dealing with a complex graph:</p>
<pre class="brush:ruby">require 'gchart' # or require 'googlecharts' if you prefer to use the Googlecharts constant.
title = "Player Count"
size = "575x300"
data = [85,107,123,131,155,172,173,189,203,222,217,233,250,239,256,267,247,261,275,295,288,305,322,307,325,347,331,346,363,382,343,359,383,352,374,393,358,379,396,416,377,398,419,380,409,426,453,432,452,465,436,460,480,440,457,474,501,457,489,507,347,373,413,402,424,448,475,488,513,475,507,530,440,476,500,518,481,512,531,367,396,423,387,415,446,478,442,469,492,463,489,508,463,491,518,549,503,526,547,493,530,549,493,520,541,564,510,535,564,492,512,537,502,530,548,491,514,538,568,524,548,568,512,533,552,577,520,545,570,516,536,555,514,536,566,521,553,579,604,541,569,595,551,581,602,549,576,606,631,589,615,650,597,624,646,672,605,626,654,584,608,631,574,597,622,559,591,614,644,580,603,629,584,615,631,558,591,618,641,314,356,395,397,429,450,421,454,477,507,458,490,560,593]
Gchart.line(:title =&gt; title, :size =&gt; size, :data =&gt; data, :axis_with_labels =&gt; 'x,y', :line_color =&gt; '1e60cc', :axis_labels =&gt; [(1.upto(24).to_a &lt;&lt; 1)], :max_value =&gt; 700, :custom =&gt; 'chg=10,15,1,0')
</pre>
<p>Which provides you with the url or image tag (or downloaded file) that produces the following graph:</p>
<p><img title="Player Count" src="http://chart.apis.google.com/chart?chxl=0:|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|1&amp;chxt=x,y&amp;chco=1e60cc&amp;chg=10,15,1,0&amp;chd=s:HJKLNPPQRTTUWVWXVXYaZbcbcedeghefhfhifhjkhjlhklomopmoqmopsorsehkjlnqrtqsumqstqtvgjliknqnprprsprtwsuwruwruvxtvxrtvsuwrtvyuwytvwzuwytvxtvyuwz1vy0wz1wz130250357135z13y03x025z13z23x024bfijlnloqsorx0&amp;chtt=Player+Count&amp;cht=lc&amp;chs=575x300&amp;chxr=1,85,700" alt="Google Chart" width="575" height="300" /></p>
<p>This release works great with Ruby 1.9 and <a title="MacRuby" href="http://macruby.org" target="_blank">MacRuby</a>, lots of bugs got fixed and some new features were added. Something a lot of people complained was that the gem was called googlecharts but that the main class was called Gchart. The problem was that I wrote my gem and called it Gchart and when I went to register the rubyforge project page, the name was already taken. In this release, I fixed this problem by allowing users to require and use the constant name they want, Gchart or Googlecharts. I also spent quite a lot of time cleaning up the old code which I was a bit ashamed of. Class variables are now removed and overall, the code should be a bit more sane.</p>
<p>The source code can be found in my <a href="http://github.com/mattetti/googlecharts/" target="_blank">GitHub accout</a> and the documentation <a href="http://mattetti.github.com/googlecharts/" target="_blank">there</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2010/02/03/googlecharts-1-5-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Googlecharts 0.1.0 released with new site</title>
		<link>http://railsontherun.com/2007/12/12/googlecharts-0-1-0-released-with-new-site/</link>
		<comments>http://railsontherun.com/2007/12/12/googlecharts-0-1-0-released-with-new-site/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 08:02:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Gems & Libs]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[googlecharts]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[googlechart]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/12/12/googlecharts-0-1-0-released-with-new-site</guid>
		<description><![CDATA[I just released Googlecharts 0.1.0 (few bug fixes and enhancements).

The first thing you want to do is to install the gem


sudo gem install googlecharts


The second thing you want to do is check the great brand new website for googlecharts




The third thing you want to do is to recommend me on Working with Rails  


The [...]]]></description>
			<content:encoded><![CDATA[<p>I just released Googlecharts 0.1.0 (few bug fixes and enhancements).</p>
<ul>
<li>The first thing you want to do is to install the gem</li>
</ul>
<pre class='brush:bash'>
sudo gem install googlecharts
</pre>
<ul>
<li>The second thing you want to do is check <a href="http://googlecharts.rubyforge.org/">the great brand new website</a> for <a href="http://googlecharts.rubyforge.org/">googlecharts</a></li>
</ul>
<p><img src="http://content.screencast.com/media/40c8b83c-06e5-4d07-89d5-8f05ae37e3a9_74569570-772f-4886-b2ea-f305d1ede3aa_static_0_0_00000037.png" alt="website"/></p>
<ul>
<li>
<p>The third thing you want to do is to <a href="http://www.workingwithrails.com/recommendation/new/person/6065-matt-aimonetti">recommend me on Working with Rails</a> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
</li>
<li>
<p>The fourth thing you want to do is to submit patches, suggestions etc&#8230;</p>
</li>
<li>
<p>The fifth thing you want to do is to develop an awesome website using sexy googlecharts</p>
</li>
</ul>
<p>This gem and its website have been made possible by <a href="http://drnicwilliams.com/">DrNic</a> and his awesome <a href="http://newgem.rubyforge.org/">new gem generator</a> and has been developed following the BDD approach thanks to <a href="http://rspec.rubyforge.org">RSpec</a> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/12/12/googlecharts-0-1-0-released-with-new-site/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Google Chart Gem</title>
		<link>http://railsontherun.com/2007/12/10/google-chart-gem/</link>
		<comments>http://railsontherun.com/2007/12/10/google-chart-gem/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 06:43:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Gems & Libs]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[googlecharts]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[gchart]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[sexy charts]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/12/12/google-chart-gem</guid>
		<description><![CDATA[update Nov 11: the gem is finally available there or simply:

  sudo gem install googlecharts

Note that I&#8217;m working on merging this gem with another Google Charts gem. (see comments for more info about that)

I&#8217;ve been working on a Google Chart Gem that I have ready for a beta release but unfortunately, getting a new [...]]]></description>
			<content:encoded><![CDATA[<h2>update Nov 11: the gem is finally available <a href="http://rubyforge.org/projects/googlecharts/">there</a> or simply:</h2>
<pre class="brush:bash">
  sudo gem install googlecharts
</pre>
<p>Note that I&#8217;m working on merging this gem with another Google Charts gem. (see comments for more info about that)</p>
<p><img src="http://chart.apis.google.com/chart?chtt=Rails+on+the+run&amp;cht=p3&amp;chs=200x90&amp;chd=s:Hellobla&amp;chl=May|Jun|Jul|Aug|Sep|Oct&amp;chco=0000ff" alt="gchart"/></p>
<p>I&#8217;ve been working on a Google Chart Gem that I have ready for a beta release but unfortunately, getting a new project setup on RubyForge takes forever. (apparently 72 hours)</p>
<p>It&#8217;s mainly a wrapper for the great GChart API, but instead of using a helper to generate your graphs, you can simply do:</p>
<pre class="brush:ruby">
  Gchart.bar(:title => 'My Mojo', :data => [1,2,4,67,100,41,234], :max_value => 300, :bg => 'c3c3c3')

  Gchart.line(:title => 'My Mojo',
              :data => [[1,2,4,67,100,41,234],[41,63,96,17,100,14,423]],
              :bg => '666666',
              :graph_bg => 'cccccc',
              :line_colors => 'ff0000,00ff00',
              :legend => ['morning','evening'])
</pre>
<pre class="brush:ruby">
  Gchart.pie(:data => [20,10,15,5,50], :title => 'SDRuby fu', :size => '400x200', :labels => ['matt', 'rob', 'patrick', 'jordan', 'ryan'])
</pre>
<p><img src="http://chart.apis.google.com/chart?chs=400x200&amp;chd=s:YMSG9&amp;chtt=SDRuby+fu&amp;chl=matt|rob|patrick|jordan|ryan&amp;cht=p" alt="img"/></p>
<p>As far as I know this is most complete Ruby wrapper for Google Chart API, but feel free to look around.</p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/12/10/google-chart-gem/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Sexy Chart &#8211; The video</title>
		<link>http://railsontherun.com/2007/11/20/sexy-chart-the-video/</link>
		<comments>http://railsontherun.com/2007/11/20/sexy-chart-the-video/#comments</comments>
		<pubDate>Tue, 20 Nov 2007 04:46:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[amcharts]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[podcast]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[san diego]]></category>
		<category><![CDATA[sdruby]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/11/20/sexy-chart-the-video</guid>
		<description><![CDATA[A bit more than a month ago I posted a tutorial on how to use Flash with Rails to create some awesome/sexy graphs.

Since a lot of people seemed interested by the topic, the SDRuby guys asked me to do a intro talk on how to create Sexy Charts with super sexy Rails.
In the mean time, [...]]]></description>
			<content:encoded><![CDATA[<p>A bit more than a month ago I posted a <a href="http://railsontherun.com/2007/10/4/sexy-charts-in-less-than-5-minutes">tutorial</a> on how to use Flash with Rails to <a href="http://railsontherun.com/2007/10/4/sexy-charts-in-less-than-5-minutes">create some awesome/sexy graphs</a>.</p>
<p><img src="http://content.screencast.com/media/e0605640-1100-43dc-90f9-d8c2083c0f7c_74569570-772f-4886-b2ea-f305d1ede3aa_static_0_0_00000016.png" alt="chart"/></p>
<p>Since a lot of people seemed interested by the topic, the <a href="http://sdruby.com/">SDRuby</a> guys asked me to do a intro talk on how to create Sexy Charts with super sexy Rails.</p>
<p>In the mean time, a lot of people were asking for a example app to look at. People knowing me know that I&#8217;m quite lazy and I don&#8217;t like repeating tasks. I therefore decided to kill 2 birds with one stone and wrote a demo app that I would use during my <a href="http://podcast.sdruby.com/2007/11/13/episode-037-sexy-charts">presentation</a></p>
<p>As I was writing the demo app, I quickly realized that my talk would be even sexier if I would show some best practices. After all, an introduction talk is meant to help newbies learning the tricks that will change them in ninjas! </p>
<p>Sexy charts are sexy now, but in 15 years they might not look so sexy anymore. However <a href="http://en.wikipedia.org/wiki/Behavior_driven_development">BDD</a> is super hot now and will always be sexy! (even though we&#8217;ll probably adopt other even hotter approaches).</p>
<p>Based on the circumstances I decided that Sexy charts would become an excuse to show people how to do BDD using <a href="http://rspec.rubyforge.org">RSpec</a> and how to test a XML view  as described in <a href="http://railsontherun.com/2007/10/31/how-to-test-a-xml-builder-view">this previous post</a></p>
<p>During my presentation I totally forgot to show people what what kind of XML we were trying to feed amCharts, so here is the file:</p>
<p><a href="http://pastie.caboo.se/120055">http://pastie.caboo.se/120055</a></p>
<p>The code used in the presentation is also available <a href="http://railsontherun.com/assets/sexy_charts.zip">here</a></p>
<p>Presentation available <a href="http://podcast.sdruby.com/2007/11/13/episode-037-sexy-charts">here</a> (the sound is a bit saturated, sorry about that. Note that we made the video big enough so you can follow with the code if you don&#8217;t understand my accent <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<p>Feel free to watch the other <a href="http://podcast.sdruby.com/">SDRuby podcasts</a> or even better, <a href="http://feeds.feedburner.com/sdrbpodcast">subscribe to our feed</a>.</p>
<h2>Next SDRuby meeting will be Thursday, December 6 @ 7:30pm</h2>
<p>Location: <a href="http://tinyurl.com/2f486e">UCSD CS Building</a></p>
<p>We&#8217;ll be talking about <a href="http://groups.google.com/group/sdruby/browse_thread/thread/d488b70d67f84a5f#">Unobtrusive Javascript</a>, the <a href="http://groups.google.com/group/sdruby/browse_thread/thread/d488b70d67f84a5f#">Facebook API</a>, and hosting our first Rails Roundtable.</p>
<h2>Newbies and experts welcome!</h2>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/11/20/sexy-chart-the-video/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sexy charts in less than 5 minutes</title>
		<link>http://railsontherun.com/2007/10/04/sexy-charts-in-less-than-5-minutes/</link>
		<comments>http://railsontherun.com/2007/10/04/sexy-charts-in-less-than-5-minutes/#comments</comments>
		<pubDate>Thu, 04 Oct 2007 03:06:00 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[5 minutes]]></category>
		<category><![CDATA[amcharts]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[gruff]]></category>
		<category><![CDATA[jfreechart]]></category>
		<category><![CDATA[rails 2.0]]></category>
		<category><![CDATA[swfobject]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml builder]]></category>

		<guid isPermaLink="false">http://railsontherun.com/2007/11/05/sexy-charts-in-less-than-5-minutes</guid>
		<description><![CDATA[NOV 04 Update: demo app now available there. Sexy charts BDD style presentation at the SDRuby group to be posted soon on video podcast
Last time, in our &#8216;do it in less than 5 minutes&#8217; series, we saw how to add quickly and simply add Ajax pagination.
This time we&#8217;ll see how to add some sexy/fancy charts [...]]]></description>
			<content:encoded><![CDATA[<h2>NOV 04 Update: demo app now available <a href="http://railsontherun.com/assets/sexy_charts.zip">there</a>. Sexy charts BDD style presentation at the <a href="http://sdruby.com">SDRuby group</a> to be posted soon on <a href="http://podcast.sdruby.com/">video podcast</a></h2>
<p><a href="http://railsontherun.com/2007/9/27/ajax-pagination-in-less-than-5-minutes">Last time</a>, in our <em>&#8216;do it in less than 5 minutes&#8217;</em> series, we saw how to add <a href="http://railsontherun.com/2007/9/27/ajax-pagination-in-less-than-5-minutes">quickly and simply add Ajax pagination</a>.</p>
<p>This time we&#8217;ll see how to add some sexy/fancy charts to your rails app.</p>
<p>The goal is to end up with something like:</p>
<p><img src="http://farm2.static.flickr.com/1322/1480241002_e67637a659_o.png" alt="chart"/></p>
<p><img src="http://farm2.static.flickr.com/1414/1479377969_805d23a55d_o.png" alt="charts2"/></p>
<h2>Various options</h2>
<p>You might have heard or even tried solution such as <a href="http://nubyonrails.com/pages/gruff">Gruff</a> or <a href="http://www.jfree.org/jfreechart/">JFreeChart</a>.</p>
<p>While these solutions are great, they are certainly a pain in the butt. Gruff requires RMagick (avoid RMagick as much as can) and creates static files (a real pain when your graphs change all the time) JFreeChart on the other hand requires Java, Java skills and I hate <a href="http://wiki.rubyonrails.org/rails/pages/HowtoGenerateJFreeCharts">the way</a> you create graphs:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  <span class="r">def</span> <span class="fu">CreateChart</span><tt>
</tt>         pipe = <span class="co">IO</span>.popen <span class="s"><span class="dl">&quot;</span><span class="k">java -cp C:</span><span class="ch">\\</span><span class="k">InstantRails</span><span class="ch">\\</span><span class="k">rails_apps</span><span class="ch">\\</span><span class="k">project</span><span class="ch">\\</span><span class="k">jfree</span><span class="ch">\\</span><span class="k">src;C:</span><span class="ch">\\</span><span class="k">InstantRails</span><span class="ch">\\</span><span class="k">rails_apps</span><span class="ch">\\</span><span class="k">project</span><span class="ch">\\</span><span class="k">jfree</span><span class="ch">\\</span><span class="k">lib</span><span class="ch">\\</span><span class="k">jcommon-1.0.0-rc1.jar;C:</span><span class="ch">\\</span><span class="k">InstantRails</span><span class="ch">\\</span><span class="k">rails_apps</span><span class="ch">\\</span><span class="k">project</span><span class="ch">\\</span><span class="k">jfree</span><span class="ch">\\</span><span class="k">lib</span><span class="ch">\\</span><span class="k">jfreechart-1.0.0-rc1.jar; CreateChart</span><span class="dl">&quot;</span></span> <tt>
</tt>         pipe.close<tt>
</tt>         redirect_to <span class="s"><span class="dl">&quot;</span><span class="k">/graph/report</span><span class="dl">&quot;</span></span> <tt>
</tt>      <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Anyway, none of these solutions would let us create our charts in less than 5 minutes so let&#8217;s cut the story short. The best solution IMHO is to use Flash. But <em>wait</em>, you don&#8217;t need to know ActionScript or to own a license of Flash or Flex, we have libraries available for us to use without any Flash knowledge <img src='http://railsontherun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.maani.us/xml_charts/index.php?menu=Gallery">XML/SWF</a> is cool Flash library which should fulfill our needs, you can even find a <a href="http://ziya.liquidrail.com/">rails plugin</a> to make things easier.</p>
<h2>amCharts</h2>
<p>But, to be honest I&#8217;d like to have something a bit &#8220;cleaner/sexy/fancy&#8221; and easier to setup.  So we&#8217;re going to use <a href="http://www.amcharts.com/">amCharts</a>  Don&#8217;t get me wrong, XML/SWF is a great library and you can make your graphs look nice (but you have to pay for support).<br />
Since we are running out of time let&#8217;s see how to implement a nice graph using *my* favorite library.</p>
<p><img src="http://www.amcharts.com/images/logo.gif" alt="amcharts"/></p>
<p>[DISCLAIMER: amCharts is <em>NOT open source</em> and <em>NOT free</em>. But, it's <em>cheap</em> (85 euros per site) especially when you think of how much time you will save. AND there is a <em>FREE version</em>. The Free version is the same as the full version but with a link back to amcharts.com]</p>
<h2>Setup</h2>
<p>Let&#8217;s go ahead and download one of the package: <a href="http://www.amcharts.com/column/download/">http://www.amcharts.com/column/download/</a> for instance.</p>
<p>Unpack the files and put them in their own folder in your public folder.<br />
Make sure you have the .swf file (amcolumn.swf for instance), a XML settings file and the fonts folder.<br />
(You might want to also create an empty amcharts_key.txt in the same folder since the plugin tries to load the key and you don&#8217;t want to pollute your logs.)</p>
<h2>Usage</h2>
<p>Now you need to understand how amCharts works. </p>
<p>After being loaded, amCharts expects a datastream. The datastream is then parsed and displayed as a chart.<br />
You can modify the aspect of any chart by changing its settings.<br />
Settings are set at runtime and/or in a setting file.</p>
<p>Great! I won&#8217;t cover the settings file. It&#8217;s a well documented XML file you just copied in your public folder. (or check the documentation)</p>
<p>What we want to focus on, is the <em>datastream</em>. Basically we just need to create a XML file that can be parsed by amCharts.</p>
<p>Let&#8217;s imagine that we have a reports_controller.rb file  We want to display the population of the cities in California.</p>
<p>let&#8217;s add a new action to render our XML file:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  <span class="r">def</span> <span class="fu">population</span><tt>
</tt>    <span class="iv">@cities</span> = <span class="co">City</span>.find(<span class="sy">:all</span>)<tt>
</tt>    <span class="iv">@population_data_link</span> = formatted_population_reports_url(<span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml</span>)<tt>
</tt>    respond_to <span class="r">do</span> |format|<tt>
</tt>      format.html<tt>
</tt>      format.xml  { render <span class="sy">:action</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">population.xml.builder</span><span class="dl">&quot;</span></span>, <span class="sy">:layout</span> =&gt; <span class="pc">false</span> }<tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>(notice that I&#8217;m using rails 2.0 and that&#8217;s why my XML template is not RXML)</p>
<p>As you can see, we have 2 values: @cities and @population<em>data</em>link</p>
<p>@cities contains all the City records, including their population etc..</p>
<p>@population<em>data</em>link contains the url to retrieve the datastream.</p>
<p>If you wonder how I got this url? I&#8217;m simply using a named route defined in my routes.rb:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  map.resources <span class="sy">:reports</span>, <span class="sy">:collection</span> =&gt; {<span class="sy">:population</span> =&gt; <span class="sy">:get</span>}</pre>
</td>
</tr>
</table>
<p>(note that you don&#8217;t need to create a restful route for that, a simple named route would have worked too)</p>
<h2>Flash detection</h2>
<p>Since we are going to use Flash, we want to make sure that people have the Flash plugin installed on their browser. For that we will use <a href="http://blog.deconcept.com/swfobject/">swfobject</a>. Simply make sure to add swfobject.js (available in any amChart package) to your public/javascript folder. Then make sure you linked the javascript in your header:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  &lt;%= javascript_include_tag <span class="s"><span class="dl">'</span><span class="k">swfobject</span><span class="dl">'</span></span> <span class="s"><span class="dl">%&gt;</span></span></pre>
</td>
</tr>
</table>
<p>We now need to create our 2 views: <em>population.html.erb</em> and <em>population.xml.builder</em></p>
<h2>population.html.erb</h2>
<p>Basically, this view only loads amCharts and provides it with the details of the datastream:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt><strong>15</strong><tt>
</tt>16<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  &lt;div id=<span class="s"><span class="dl">&quot;</span><span class="k">population_chart</span><span class="dl">&quot;</span></span> <span class="r">class</span>=<span class="s"><span class="dl">'</span><span class="k">chart</span><span class="dl">'</span></span>&gt;<tt>
</tt>    &lt;strong&gt;<span class="co">Text</span> displayed <span class="r">when</span> the user doesn<span class="s"><span class="dl">'</span><span class="k">t have Flash. You might want to display a simple table with the population, search engines and visitor without flash would love that.&lt;/strong&gt;<tt>
</tt>    &lt;p&gt; To see this page properly, you need to upgrade your Flash Player, please visit the Adobe web site&lt;/p&gt;<tt>
</tt>  &lt;/div&gt;<tt>
</tt><tt>
</tt>  &lt;script type=&quot;text/javascript&quot;&gt;<tt>
</tt>    // &lt;![CDATA[    <tt>
</tt>    var so = new SWFObject(&quot;/amcolumn/amcolumn.swf&quot;, &quot;population_chart&quot;, &quot;800&quot;, &quot;380&quot;, &quot;8&quot;, &quot;#000000&quot;);<tt>
</tt>    so.addVariable(&quot;path&quot;, &quot;/amcolumn/&quot;);<tt>
</tt>    so.addVariable(&quot;settings_file&quot;, escape(&quot;/amcolumn/column_settings.xml&quot;));<tt>
</tt>    so.addVariable(&quot;data_file&quot;, escape(&quot;&lt;%= @population_data_link %&gt;&quot;));<tt>
</tt>    so.addVariable(&quot;additional_chart_settings&quot;, &quot;&lt;settings&gt;&lt;labels&gt;&lt;label&gt;&lt;x&gt;250&lt;/x&gt;&lt;y&gt;25&lt;/y&gt;&lt;text_size&gt;18&lt;/text_size&gt;&lt;text&gt;&lt;![CDATA[&lt;b&gt;California Population as of &lt;%= Time.now.to_s(:db) %&gt;&lt;/b&gt;]]&gt;&lt;/text&gt;&lt;/label&gt;&lt;/labels&gt;&lt;/settings&gt;&quot;);<tt>
</tt>    so.addVariable(&quot;preloader_color&quot;, &quot;#000000&quot;);<tt>
</tt>    so.write(&quot;population_chart&quot;);<tt>
</tt>    // ]]&gt;<tt>
</tt>  &lt;/script&gt;</span></span></pre>
</td>
</tr>
</table>
<p>As you can see, we have a div called population_chart. This div is replaced at load time by the Flash object if the visitor has Flash setup locally. Think about providing some data in case the user doesn&#8217;t have Flash.</p>
<p>The rest is simple Javascript. I unpacked the amchart column lib in mypublic/amcolumn folder and that&#8217;s why I setup the path as &#8220;amcolumn&#8221;</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  so.addVariable(<span class="s"><span class="dl">&quot;</span><span class="k">path</span><span class="dl">&quot;</span></span>, <span class="s"><span class="dl">&quot;</span><span class="k">/amcolumn/</span><span class="dl">&quot;</span></span>);</pre>
</td>
</tr>
</table>
<p>My settings file is called column_settings.xml :</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  so.addVariable(<span class="s"><span class="dl">&quot;</span><span class="k">settings_file</span><span class="dl">&quot;</span></span>, escape(<span class="s"><span class="dl">&quot;</span><span class="k">/amcolumn/column_settings.xml</span><span class="dl">&quot;</span></span>));</pre>
</td>
</tr>
</table>
<p>and the most important part:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  so.addVariable(<span class="s"><span class="dl">&quot;</span><span class="k">data_file</span><span class="dl">&quot;</span></span>, escape(<span class="s"><span class="dl">&quot;</span><span class="k">&lt;%= @population_data_link %&gt;</span><span class="dl">&quot;</span></span>));</pre>
</td>
</tr>
</table>
<p>Finally, I added some dynamic settings just to show you how easy it is:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  so.addVariable(<span class="s"><span class="dl">&quot;</span><span class="k">additional_chart_settings</span><span class="dl">&quot;</span></span>,<tt>
</tt>  <span class="s"><span class="dl">&quot;</span><span class="k">&lt;settings&gt;&lt;labels&gt;&lt;label&gt;&lt;x&gt;250&lt;/x&gt;&lt;y&gt;25&lt;/y&gt;&lt;text_size&gt;18&lt;/text_size&gt;&lt;text&gt;&lt;![CDATA[&lt;b&gt;California Population as of &lt;%= Time.now.to_s(:db) %&gt;&lt;/b&gt;]]&gt;&lt;/text&gt;&lt;/label&gt;&lt;/labels&gt;&lt;/settings&gt;</span><span class="dl">&quot;</span></span>);</pre>
</td>
</tr>
</table>
<p>Ok, let&#8217;s now create our XML view:</p>
<h2>population.xml.builder</h2>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt><strong>15</strong><tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt><strong>25</strong><tt>
</tt>26<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  xml.instruct! <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> ml</span>, <span class="sy">:version</span>=&gt;<span class="s"><span class="dl">&quot;</span><span class="k">1.0</span><span class="dl">&quot;</span></span>, <span class="sy">:encoding</span>=&gt;<span class="s"><span class="dl">&quot;</span><span class="k">UTF-8</span><span class="dl">&quot;</span></span><tt>
</tt>  xml.chart <span class="r">do</span><tt>
</tt>    <span class="c"># xml.message &quot;You can broadcast any message to chart from data XML file&quot;, :bg_color =&gt; &quot;#FFFFFF&quot;, :text_color =&gt; &quot;#000000&quot;</span><tt>
</tt>    xml.series <span class="r">do</span>    <tt>
</tt>      <span class="iv">@cities</span>.each_with_index <span class="r">do</span> |city, index|<tt>
</tt>        xml.value city.name, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index<tt>
</tt>      <span class="r">end</span><tt>
</tt>    <span class="r">end</span><tt>
</tt><tt>
</tt>    xml.graphs <span class="r">do</span><tt>
</tt>     <span class="c">#the gid is used in the settings file to set different settings just for this graph</span><tt>
</tt>      xml.graph <span class="sy">:gid</span> =&gt; <span class="s"><span class="dl">'</span><span class="k">population</span><span class="dl">'</span></span> <span class="r">do</span><tt>
</tt>        <span class="iv">@cities</span>.each_with_index <span class="r">do</span> |city, index|<tt>
</tt>          population = city.population<tt>
</tt>          <span class="r">case</span> population<tt>
</tt>            <span class="c"># When the population is &gt; 1 million, show the bar in red/pink</span><tt>
</tt>            <span class="r">when</span> &gt; <span class="i">100000</span><tt>
</tt>              xml.value value, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index, <span class="sy">:color</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#ff43a8</span><span class="dl">&quot;</span></span>, <span class="sy">:gradient_fill_colors</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#960040,#ff43a8</span><span class="dl">&quot;</span></span>, <span class="sy">:description</span> =&gt; level<tt>
</tt>            <span class="r">else</span><tt>
</tt>              xml.value value, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index, <span class="sy">:color</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#00C3C6</span><span class="dl">&quot;</span></span>, <span class="sy">:gradient_fill_colors</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#009c9d,#00C3C6</span><span class="dl">&quot;</span></span>, <span class="sy">:description</span> =&gt; level<tt>
</tt>            <span class="r">end</span><tt>
</tt>        <span class="r">end</span><tt>
</tt>      <span class="r">end</span><tt>
</tt>    <span class="r">end</span><tt>
</tt><tt>
</tt>  <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Nothing fancy, we first created a series with all the city names:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  xml.series <span class="r">do</span>    <tt>
</tt>    <span class="iv">@cities</span>.each_with_index <span class="r">do</span> |city, index|<tt>
</tt>      xml.value city.name, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index<tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Then we created another node with the values for each city.<br />
Since it would be cool to display some bars in a different color, we used a case-switch statement:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt><strong>5</strong><tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">    xml.graph <span class="sy">:gid</span> =&gt; <span class="s"><span class="dl">'</span><span class="k">population</span><span class="dl">'</span></span> <span class="r">do</span><tt>
</tt>      <span class="iv">@cities</span>.each_with_index <span class="r">do</span> |city, index|<tt>
</tt>        population = city.population<tt>
</tt>        <span class="r">case</span> population<tt>
</tt>          <span class="c"># When the population is &gt; 1 million, show the bar in red/pink</span><tt>
</tt>          <span class="r">when</span> &gt; <span class="i">100000</span><tt>
</tt>            xml.value value, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index, <span class="sy">:color</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#ff43a8</span><span class="dl">&quot;</span></span>, <span class="sy">:gradient_fill_colors</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#960040,#ff43a8</span><span class="dl">&quot;</span></span>, <span class="sy">:description</span> =&gt; level<tt>
</tt>          <span class="r">else</span><tt>
</tt>            xml.value value, <span class="sy"> <img src='http://railsontherun.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> id</span> =&gt; index, <span class="sy">:color</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#00C3C6</span><span class="dl">&quot;</span></span>, <span class="sy">:gradient_fill_colors</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">#009c9d,#00C3C6</span><span class="dl">&quot;</span></span>, <span class="sy">:description</span> =&gt; level<tt>
</tt>          <span class="r">end</span><tt>
</tt>      <span class="r">end</span><tt>
</tt>    <span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Depending on what you want to display, you might need to have different colors or a different tooltip text, or load an animation or image&#8230;  and as you can see, it&#8217;s <em>REALLY</em> easy.</p>
<p>Got to http://yoursite.com/reports/population to enjoy your new fancy graph.</p>
<h2>That&#8217;s it, you are done! </h2>
<p>Time to tweak your settings file to make your graph look <em>awesome</em>.<br />
Since you now have a lot of free time, you can start re-factoring your code and make sure you have a good test coverage.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://railsontherun.com/2007/10/04/sexy-charts-in-less-than-5-minutes/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
	</channel>
</rss>

