<?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>unitstep.net &#187; graphs</title>
	<atom:link href="http://unitstep.net/blog/category/graphs/feed/" rel="self" type="application/rss+xml" />
	<link>http://unitstep.net</link>
	<description>the home of peter chng</description>
	<lastBuildDate>Mon, 19 Mar 2012 01:49:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Properly setting Axis Ranges and Data Scaling using the Google Chart API</title>
		<link>http://unitstep.net/blog/2008/11/06/properly-setting-axis-ranges-and-data-scaling-using-the-google-chart-api/</link>
		<comments>http://unitstep.net/blog/2008/11/06/properly-setting-axis-ranges-and-data-scaling-using-the-google-chart-api/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 01:00:08 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
				<category><![CDATA[charts]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[tutorials]]></category>
		<category><![CDATA[api]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=531</guid>
		<description><![CDATA[The Google Chart API is a great way to dynamically create chart and graph images for any non-static data you might want to display to your visitors, such as stats for a runner&#8217;s training log. Although you can use your own server-side solution for generating charts, using PHP&#8217;s GD Image Processing Library or even a [...]]]></description>
			<content:encoded><![CDATA[<p class="image align-right"><a href="http://code.google.com/apis/chart/"><img src="http://unitstep.net/wordpress/wp-content/uploads/2008/11/google-chart-api.png" alt="" title="google-chart-api" /></a></p>
<p>The <a href="http://code.google.com/apis/chart/">Google Chart API</a> is a great way to dynamically create chart and graph images for any non-static data you might want to display to your visitors, such as <a href="http://runtrackr.uservoice.com/pages/general/suggestions/39633">stats for a runner&#8217;s training log</a>.  </p>
<p>Although you can use your own server-side solution for generating charts, using <a href="http://php.net/gd"><acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s GD Image Processing Library</a> or even a fancy chart library like <a href="http://pchart.sourceforge.net/">pChart</a>, this can increase the drain on your server&#8217;s resources.  Client-side solutions that utilize <a href="http://www.filamentgroup.com/lab/creating_accessible_charts_using_canvas_and_jquery/">the <code>canvas</code> element and jQuery</a> or <a href="http://www.maani.us/xml_charts/">Flash</a> can be very nice and easy to use, but may slow down the browser if there are a lot of charts.</p>
<p>Google Charts offers a nice trade-off; by using their service you offload the processing and bandwidth and get back a simple <a href="http://www.w3.org/Graphics/PNG/" class="ubernym uttInitialism"><acronym class="uttInitialism" title="Portable Network Graphics">PNG</acronym></a> image, but you must learn to use the API.  Additionally, there&#8217;s also the risk that the service may throttle you, even if there&#8217;s no current usage limit.</p>
<h3>Problems specifying Axis Ranges &#8211; Data points are not changed!</h3>
<p>The API for Google Charts essentially consists of passing different query string parameters and their values and getting back a <a href="http://www.w3.org/Graphics/PNG/" class="ubernym uttInitialism"><acronym class="uttInitialism" title="Portable Network Graphics">PNG</acronym></a>-format image.  As a result of this, passing in parameters is a bit quirky, and information in the API guide is a bit sketchy in this respect.</p>
<p>One such example with quirky behaviour is properly setting the <a href="http://code.google.com/apis/chart/labels.html#axis_range">axis&#8217; ranges</a>, or scales, of a chart.  By default, all charts are set up on a 100&#215;100 grid.  It would seem that by changing the axis range, these values will be adjusted accordingly, affecting the position of any data points you&#8217;ve plotted.</p>
<p>But this isn&#8217;t the case; instead, changing the axis range with the <code>chxr</code> parameter <strong>only</strong> adjusts the position of any <em>axis labels</em> you might have, and <strong>does not</strong> affect the position of data points. Instead, data points will continue to be plotted as if they were on a 100&#215;100 grid.</p>
<h3>Illustrating the problem</h3>
<p>The following examples illustrate this point.  We plot a set of points, <code>{(10,40),(30,60),(50,70),(70,20),(90,40)}</code> using an <a href="http://code.google.com/apis/chart/types.html#line_charts">X-Y line chart</a>.  The first chart illustrates the points on the default grid of 100&#215;100. Because axis labels/positions have not been specified, the chart has been filled with default axis labels, which are values in the respective positions.</p>
<p class="image"><a href="http://chart.apis.google.com/chart?chs=500x500&amp;cht=lxy&amp;chd=t:10,30,50,70,90%7C40,60,70,20,40&amp;chxt=x,y&amp;chg=10,10&amp;chm=o,0000FF,0,-1,5,0"><img src="http://unitstep.net/wordpress/wp-content/uploads/2008/11/chart1.png" alt="chart on default 100x100 grid" /></a></p>
<pre><code>URL: http://chart.apis.google.com/chart?chs=200x200&amp;cht=lxy&amp;chd=t:10,30,50,70,90|40,60,70,20,40&amp;chxt=x,y&amp;chg=10,10&amp;chm=o,0000FF,0,-1,5,0</code></pre>
<p>In this case, the points are plotted in the correct positions.  (Points have been highlighted with <a href="http://code.google.com/apis/chart/styles.html#shape_markers">markers</a> for clarity and grid lines spaced 10 units apart)</p>
<p>However, if we simply change the axis ranges by specifying the <code>chxr</code> parameter to be <code>chxr=0,0,200|1,0,200</code>, the axis labels change, but the data points are not properly scaled:</p>
<p class="image"><a href="http://chart.apis.google.com/chart?chs=500x500&amp;cht=lxy&amp;chd=t:10,30,50,70,90%7C40,60,70,20,40&amp;chxt=x,y&amp;chg=10,10&amp;chm=o,0000FF,0,-1,5,0&amp;chxr=0,0,200%7C1,0,200"><img src="http://unitstep.net/wordpress/wp-content/uploads/2008/11/chart2.png" alt="chart without data scaling"></a></p>
<pre><code>URL: http://chart.apis.google.com/chart?chs=200x200&amp;cht=lxy&amp;chd=t:10,30,50,70,90|40,60,70,20,40&amp;chxt=x,y&amp;chg=10,10&amp;chm=o,0000FF,0,-1,5,0&amp;chxr=0,0,200|1,0,200</code></pre>
<p>This is further evidenced by the grid line spacing; though it&#8217;s still set to 10 units, the lines would appear to be 20 units apart in the above graph.  This means that internally, the chart is still using a 100&#215;100 grid.  So, how do we fix that?</p>
<h3>Plotting data properly with axis ranges</h3>
<p>The <a href="http://code.google.com/apis/chart/labels.html#multiple_axes_labels">documentation</a> would benefit from being clearer in this respect.  </p>
<p>In order to properly scale data, <strong>you must use <a href="http://code.google.com/apis/chart/formats.html#data_scaling">data scaling</a> whenever you adjust the axis ranges to non-default values.</strong></p>
<p>For the above example, since we have set both axis ranges to 0-200, we must set the data scale of both data sets (X and Y values) to 0-200 as well.  We accomplish this by setting the <code>chds</code> like so:</p>
<pre><code>chds=0,200,0,200</code></pre>
<p>The resultant graph is now as expected for the given data points:</p>
<p class="image"><a href="http://chart.apis.google.com/chart?chs=500x500&amp;cht=lxy&amp;chd=t:10,30,50,70,90%7C40,60,70,20,40&amp;chxt=x,y&amp;chg=10,10&amp;chm=o,0000FF,0,-1,5,0&amp;chxr=0,0,200%7C1,0,200&amp;chds=0,200,0,200"><img src="http://unitstep.net/wordpress/wp-content/uploads/2008/11/chart3.png"></a></p>
<pre><code>URL: http://chart.apis.google.com/chart?chs=200x200&amp;cht=lxy&amp;chd=t:10,30,50,70,90|40,60,70,20,40&amp;chxt=x,y&amp;chg=10,10&amp;chm=o,0000FF,0,-1,5,0&amp;chxr=0,0,200|1,0,200&amp;chds=0,200,0,200</code></pre>
<p>(Curiously, grid lines are still placed 10 units apart according to an &#8220;internal&#8221; 100&#215;100 grid.)</p>
<p>I couldn&#8217;t find anything clear in the documentation about this, but instead found some <a href="http://groups.google.com/group/google-chart-api/browse_thread/thread/4c3783b3a0dabe8/02842878d286f0c8?lnk=gst&#038;q=axis+range#02842878d286f0c8">helpful threads</a> on the Google Groups for their Charts API that aided me in figuring out exactly how this all works.</p>
<h3>Clear as mud?</h3>
<p>There are still some things I&#8217;m not clear about, such as what <em><a href="http://code.google.com/apis/chart/formats.html#data_scaling">data scaling</a></em> is actually supposed to mean, and why simply changing axis ranges does not properly scale data in the first place.  Additionally, the <a href="http://code.google.com/apis/chart/formats.html">data formats page</a> specifies numerous other ways of specifying data in more compact, encoded forms, but these formats do not seem to support data scaling.  Does that mean these formats are confined to a 100&#215;100 grid?  </p>
<p>I haven&#8217;t had the time to fully experiment with the API, but hopefully will have time to learn.  In the meantime, I hope you found this information useful.  If you have some information I haven&#8217;t presented here, please don&#8217;t hesitate to comment below!</p>
<p class="note less">Note: The information and graph images here reflect the state of the Google Chart API as of the date of publication. (2008-11-06)</p>
<hr/>Copyright &copy; 2012 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/11/06/properly-setting-axis-ranges-and-data-scaling-using-the-google-chart-api/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

