<?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>SAPessi &#187; Charts</title>
	<atom:link href="http://sapessi.com/tag/charts/feed/" rel="self" type="application/rss+xml" />
	<link>http://sapessi.com</link>
	<description>Perfection of means and confusion of aims...</description>
	<lastBuildDate>Fri, 02 Mar 2012 09:26:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SVG graphics with JavaScript</title>
		<link>http://sapessi.com/2009/11/svg-graphics-with-javascript/</link>
		<comments>http://sapessi.com/2009/11/svg-graphics-with-javascript/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 13:47:03 +0000</pubDate>
		<dc:creator>Stefano Buliani</dc:creator>
				<category><![CDATA[My Works]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[WWW]]></category>
		<category><![CDATA[Charts]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[RaphaelJS]]></category>
		<category><![CDATA[SVG]]></category>
		<category><![CDATA[TweetSentiment]]></category>

		<guid isPermaLink="false">http://sapessi.com/?p=327</guid>
		<description><![CDATA[When I started developing TweetSentiment I decided that the interface should have as little text as possible. Most of the information I was interested in could be displayed graphically, with a chart. So I looked at all the options available for chart generation. Backend code to generate a static image (JFreeChart or PHP) Flash object [...]<!-- Easy AdSense V2.82 -->
<!-- Post[count: 2] -->
<div class="ezAdsense adsense adsense-leadout" style="text-align:center;margin:12px;"><script type="text/javascript"><!--
google_ad_client = "pub-8456780651289352";
/* 468x60, created 11/24/09 */
google_ad_slot = "7140896000";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<!-- Easy AdSense V2.82 -->

]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;source=sapessi&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>When I started developing <a href="http://tweetsentiment.info" target="_blank">TweetSentiment</a> I decided that the interface should have as little text as possible. Most of the information I was interested in could be displayed graphically, with a chart.</p>
<p>So I looked at all the options available for chart generation.</p>
<ol>
<li>Backend code to generate a static image (<a href="http://www.jfree.org/jfreechart/" target="_blank">JFreeChart</a> or PHP)</li>
<li>Flash object to draw a chart retrieving the data from a URL</li>
<li>Draw charts in JavaScript directly on the client&#8217;s browser</li>
</ol>
<p>I&#8217;m not a huge fan of option one. Primarily because TweetSentiment is hosted on a tiny linux box which would not be able to handle the load for the traffic the site gets, also because it&#8217;s a static image &#8211; it&#8217;s just not very funky &#8211; no interaction possible.</p>
<p>Option two would certainly create spectacular looking charts but I have almost no experience with flash and I wasn&#8217;t about to start learning a new language/technology. Plus I&#8217;m not into browser plugins if I can avoid them.</p>
<p>JavaScript is a language I&#8217;m familiar with and I remember seeing some cool-looking charts generated with <a href="http://www.dojotoolkit.org/" target="_blank">Dojo</a>. Unfortunately for TweetSentiment I have used jquery since the most important thing for me there was DOM manipulation (and jquery is just better for that).<br />
I then started shopping around for <a href="http://jquery.com/" target="_blank">jQuery</a> plugins to generate charts. There are a few around but none of them impressed me. They just weren&#8217;t as good looking as I&#8217;d hoped nor they were interactive.</p>
<p>By coincidence I stumbled on <a href="http://raphaeljs.com/" target="_blank">RaphaelJS</a>. A JavaScript library to draw <a href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics" target="_blank">Scalable Vector Graphics</a> directly from JavaScript based on jQuery. I tested the samples on the website with a few browsers and I was happy to discover it worked just fine with all of them.</p>
<blockquote>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;"><strong>Scalable Vector Graphics</strong> (<strong>SVG</strong>) is a family of specifications of an <a style="text-decoration: none; color: #002bb8; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial;" title="XML" href="/wiki/XML">XML</a>-based <a style="text-decoration: none; color: #002bb8; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial;" title="File format" href="/wiki/File_format">file format</a> for describing two-dimensional <a style="text-decoration: none; color: #002bb8; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial;" title="Vector graphics" href="/wiki/Vector_graphics">vector graphics</a>, both static and dynamic (i.e. interactive or <a style="text-decoration: none; color: #002bb8; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial;" title="SVG animation" href="/wiki/SVG_animation">animated</a>).</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">The SVG specification is an <a style="text-decoration: none; color: #002bb8; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial;" title="Open standard" href="/wiki/Open_standard">open standard</a> that has been under development by the <a style="text-decoration: none; color: #002bb8; background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; background-position: initial initial; background-repeat: initial initial;" title="World Wide Web Consortium" href="/wiki/World_Wide_Web_Consortium">World Wide Web Consortium</a> (W3C) since 1999.</p>
</blockquote>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">I also discovered that there is a <a href="http://g.raphaeljs.com/" target="_blank">charting library built on top of RaphaelJS</a>, which is exactly what I was looking for. However, being a geek, I decided to go ahead and try to develop something on my own. You know, just for kicks.</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">As I delved deeper into RaphaelJS I found the library to be incredibly powerful. It&#8217;s a shame that the <a href="http://raphaeljs.com/reference.html" target="_blank">documentation provided on the website</a> lets it down a bit.<br />
The most powerful bit is the ability to extend objects and attach new functions to them. Something scarcely mentioned in the available documentation.</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">For example if you need to use curved lines (paths as SVG calls them) you can just defined a default function you can then call from your code simply by adding it to the <strong>el </strong>&#8220;object&#8221; in RaphaelJS</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;">Raphael.<span class="me1">el</span>.<span class="me1">curveTo</span> <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="kw2">var</span> args <span class="sy0">=</span> Array.<span class="me1">prototype</span>.<span class="me1">splice</span>.<span class="me1">call</span><span class="br0">&#40;</span>arguments<span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> arguments.<span class="me1">length</span><span class="br0">&#41;</span><span class="sy0">,</span><br />
&nbsp; d <span class="sy0">=</span> <span class="br0">&#91;</span><span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="st0">&quot;s&quot;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="st0">&quot;c&quot;</span><span class="br0">&#93;</span><span class="br0">&#91;</span>args.<span class="me1">length</span><span class="br0">&#93;</span> <span class="sy0">||</span> <span class="st0">&quot;&quot;</span><span class="sy0">;</span><br />
&nbsp; <span class="kw1">this</span>.<span class="me1">isAbsolute</span> <span class="sy0">&amp;</span>amp<span class="sy0">;&amp;</span>amp<span class="sy0">;</span> <span class="br0">&#40;</span>d <span class="sy0">=</span> d.<span class="me1">toUpperCase</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="kw1">this</span>._last <span class="sy0">=</span> <span class="br0">&#123;</span>x<span class="sy0">:</span> args<span class="br0">&#91;</span>args.<span class="me1">length</span> <span class="sy0">-</span> 2<span class="br0">&#93;</span><span class="sy0">,</span> y<span class="sy0">:</span> args<span class="br0">&#91;</span>args.<span class="me1">length</span> <span class="sy0">-</span> 1<span class="br0">&#93;</span><span class="br0">&#125;</span><span class="sy0">;</span><br />
&nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="br0">&#123;</span>path<span class="sy0">:</span> <span class="kw1">this</span>.<span class="me1">attrs</span>.<span class="me1">path</span> <span class="sy0">+</span> d <span class="sy0">+</span> args<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><span class="sy0">;</span></div>
</div>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">Another very useful function I found in one of their samples is the <strong>andClose() </strong>This is used to close a polygon you have started drawing with paths. No matter where you got to it will reconnect to the initial point.</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;">Raphael.<span class="me1">el</span>.<span class="me1">andClose</span> <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">attr</span><span class="br0">&#40;</span><span class="br0">&#123;</span>path<span class="sy0">:</span> <span class="kw1">this</span>.<span class="me1">attrs</span>.<span class="me1">path</span> <span class="sy0">+</span> <span class="st0">&quot;z&quot;</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><span class="sy0">;</span></div>
</div>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">This can then be used this way using <a href="http://ejohn.org/blog/ultra-chaining-with-jquery/" target="_blank">chaining</a>.</p>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;">RaphaelJSElement.<span class="me1">lineTo</span><span class="br0">&#40;</span>x<span class="sy0">,</span> opts.<span class="me1">height</span> <span class="sy0">-</span> bottomgutter<span class="br0">&#41;</span>.<span class="me1">andClose</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p style="margin-top: 0.4em; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; line-height: 1.5em;">I&#8217;m still developing the chart library I used in TweetSentiment and I&#8217;m planning to publish it here with some documentation under MIT licence.</p>
<!-- Easy AdSense V2.82 -->
<!-- Post[count: 3] -->
<div class="ezAdsense adsense adsense-leadout" style="text-align:center;margin:12px;"><script type="text/javascript"><!--
google_ad_client = "pub-8456780651289352";
/* 468x60, created 11/24/09 */
google_ad_slot = "7140896000";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<!-- Easy AdSense V2.82 -->

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;submitHeadline=SVG+graphics+with+JavaScript&amp;submitSummary=" rel="nofollow" title="Add to&nbsp;Buzz"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/buzz.png" title="Add to&nbsp;Buzz" alt="Add to&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;title=SVG+graphics+with+JavaScript" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;title=SVG+graphics+with+JavaScript" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;title=SVG+graphics+with+JavaScript" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;bm_description=SVG+graphics+with+JavaScript" rel="nofollow" title="Add to&nbsp;Mister Wong"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Add to&nbsp;Mister Wong" alt="Add to&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;T=SVG+graphics+with+JavaScript" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;title=SVG+graphics+with+JavaScript" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;title=SVG+graphics+with+JavaScript" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F" rel="nofollow" title="Add to&nbsp;Tip'd"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/tipd.png" title="Add to&nbsp;Tip'd" alt="Add to&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+SVG+graphics+with+JavaScript+@+http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fsapessi.com%2F2009%2F11%2Fsvg-graphics-with-javascript%2F&amp;t=SVG+graphics+with+JavaScript" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://sapessi.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://sapessi.com/2009/11/svg-graphics-with-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

