<?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>But what if... &#187; flot</title>
	<atom:link href="http://eremiya.net/category/flot/feed/" rel="self" type="application/rss+xml" />
	<link>http://eremiya.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 04 Nov 2010 22:52:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Jquery, Flot Tutorial (Part 2 revisited)</title>
		<link>http://eremiya.net/2010/06/30/jquery-flot-tutorial-part-2-revisited/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-flot-tutorial-part-2-revisited</link>
		<comments>http://eremiya.net/2010/06/30/jquery-flot-tutorial-part-2-revisited/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 01:59:13 +0000</pubDate>
		<dc:creator>Jérémie</dc:creator>
				<category><![CDATA[Coldfusion]]></category>
		<category><![CDATA[Jquery]]></category>
		<category><![CDATA[charting]]></category>
		<category><![CDATA[flot]]></category>

		<guid isPermaLink="false">http://eremiya.net/?p=149</guid>
		<description><![CDATA[Woaw, I&#8217;ve never blogged that much in a row. In my previous post, I showed you how to create a pod, and having ajax request to get the data. Actually, this is not the best way to use flot. Flot can redraw your chart &#8220;on the fly&#8220;, if I can say, if your code is [...]]]></description>
			<content:encoded><![CDATA[<p>Woaw, I&#8217;ve never blogged that much in a row. In my <a href="http://eremiya.net/2010/06/27/jquery-flot-tutorial-part-2/">previous post</a>, I showed you how to create a pod, and having ajax request to get the data. Actually, this is not the best way to use flot. Flot can redraw your chart &#8220;<em>on the fly</em>&#8220;, if I can say, if your code is written appropriately. <span id="more-149"></span></p>
<p>Let try to do that. As there is no great change on the  HTML file, let&#8217;s concentrate on the JavaScript. First, we define some global variables. When the page has loaded, we call a getData function, that takes 2 parameters, year and yearNumber.</p>
<pre class="brush: js">
var $j = jQuery.noConflict();

var plotarea = $j(&quot;#placeholder&quot;);

var months = [&#039;jan&#039;, &#039;feb&#039;, &#039;mars&#039;, &#039;apr&#039;, &#039;may&#039;, &#039;jun&#039;, &#039;jul&#039;, &#039;aug&#039;, &#039;sep&#039;, &#039;oct&#039;, &#039;nov&#039;, &#039;dec&#039;];

var &lt;cfoutput&gt;#toScript(url.year, &quot;year&quot;)#&lt;/cfoutput&gt;
var &lt;cfoutput&gt;#toScript(url.yearNumber,  &quot;yearNumber&quot;)#&lt;/cfoutput&gt;

$j(document).ready(function(){

//Draw the chart
getData(year,yearNumber);

//Put an event listener on every select of the form
$j(&quot;select&quot;).change(function() {

//Get the selects values
var selectedYear = $j(&quot;#year option:selected&quot;).text();
if (selectedYear == &#039;&#039;) {
selectedYear = year;
}
var selectedYearNumber = $j(&quot;#yearNumber option:selected&quot;).text();
if (selectedYearNumber == &#039;&#039;) {
selectedYearNumber = yearNumber;
}

getData(selectedYear,selectedYearNumber);

})

});
</pre>
<p>We also add event listeners on the selects of the control form. On Change, we get the value of both selects and call the getData function again. Let&#8217;s see that function.</p>
<pre class="brush: js">
function getData(year,yearNumber) {

var dataObject = [];

$j.getJSON(
&#039;http://dev.eremiya/flot/jquery_flot_1.5/charting.cfc?wsdl&#039;,
{ method : &#039;getStructForChart&#039;, returnformat : &#039;json&#039;, queryformat : &#039;column&#039;, year : year, yearNumber : yearNumber },
function(data) {

for (var a=0; a&lt;data.length; a++) {

eval(&quot;data&quot; + a + &quot; = []&quot;);

for (var i=0; i&lt;data[a].ROWCOUNT; i++) {

var myDate = new Date(data[a].DATA.ORDERDATE[i]+&#039;/01&#039;);
eval(&quot;data&quot; + a).push([myDate.getMonth(),data[a].DATA.TOTAL[i]]);
var dataYear = myDate.getFullYear();

}

dataObject.push({label:dataYear,data:eval(&quot;data&quot; + a)})

}

drawChart({data:dataObject});

}
)

}
</pre>
<p>The getData function call the CFC and get a JSON response. We format the data, and then we pass the populated data array to the drawChart function (as an option, if you noticed). The drawChart function takes options (you can overwrite them as you wish) and then, guess what&#8230;, draw the chart!</p>
<pre class="brush: js">
function drawChart(options) {

//Some option (edit for your own)
var defaults = {
data : [],
legend: {
show: true,
margin: 10,
backgroundOpacity: 0.5
},
points: {
show: true,
radius: 6
},
lines: {
show: true
},
grid: {
borderWidth:0,
labelMargin:15,
clickable:true,
hoverable:true,
autoHighlight:true
},
xaxis: {
tickSize: 1,
tickFormatter: function(val,axis) {
return months[val];
}
}
};

var options = $j.extend(defaults, options);

$j.plot(plotarea, options.data, options);

plotarea.bind(&quot;plotclick&quot;, function (event, pos, item) {

if(item != null) {
alert(&#039;In &#039;+item.series.label+&#039;/&#039;+item.datapoint[0]+&#039;, the order amout is &#039;+item.datapoint[1]);
}

});

}
</pre>
<p>As you can see, we are going to pass data as an option, and the drawChart function will redraw the chart without any refresh. That&#8217;s the magic of jQuery, Flot and ColdFusion. Here is the <a href="http://eremiya.net/wp-content/uploads/2010/06/jquery_flot_1.5_3.zip" target="_blank">source code</a> of this post (chart2.cfm). Change it to meet your needs. Again, this is by a newbie for newbies, so let me know if you have a better approach. We&#8217;ll progress together.</p>
]]></content:encoded>
			<wfw:commentRss>http://eremiya.net/2010/06/30/jquery-flot-tutorial-part-2-revisited/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jquery, Flot Tutorial (Part 2)</title>
		<link>http://eremiya.net/2010/06/27/jquery-flot-tutorial-part-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-flot-tutorial-part-2</link>
		<comments>http://eremiya.net/2010/06/27/jquery-flot-tutorial-part-2/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 04:29:13 +0000</pubDate>
		<dc:creator>Jérémie</dc:creator>
				<category><![CDATA[Coldfusion]]></category>
		<category><![CDATA[Jquery]]></category>
		<category><![CDATA[charting]]></category>
		<category><![CDATA[flot]]></category>

		<guid isPermaLink="false">http://eremiya.net/?p=93</guid>
		<description><![CDATA[In my previous post, we created a chart that represent the increase of a given year by month (from january to december). In this post, we&#8217;ll see how to create a pod, for a dashboard for example, that uses the previously created page and gives the possibility to query for a special year, and also [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://eremiya.net/2009/10/16/jquery-flot-tutorial-part-1-5/" target="_blank">previous post</a>, we created a chart that represent the increase of a given year by month (from january to december). In this post, we&#8217;ll see how to create a pod, for a dashboard for example, that uses the previously created page and gives the possibility to query for a special year, and also to display several years. Once again, we&#8217;ll use CFCs to query the database and get the data to our chart.</p>
<p><span id="more-93"></span></p>
<p style="text-align: center;">Here is a preview of what we want to achieve.</p>
<p><a href="http://eremiya.net/wp-content/uploads/2010/06/Screen-shot-2010-06-27-at-12.38.52-PM.png"><img class="aligncenter size-full wp-image-146" title="Pod" src="http://eremiya.net/wp-content/uploads/2010/06/Screen-shot-2010-06-27-at-12.38.52-PM.png" alt="" width="500" height="259" /></a></p>
<p>Let&#8217;s get started! First, a function to load the chart when our page has loaded:</p>
<pre class="brush: html">

&lt;div id=&quot;pod&quot;&gt;
&lt;!-- A div that will contain the cart --&gt;
&lt;div id=&quot;chartholder&quot; class=&quot;binding&quot; url=&quot;chart.cfm&quot;&gt;

&lt;/div&gt;

&lt;!-- Controller for our pod --&gt;
&lt;div id=&quot;dataCollector&quot;&gt;
&lt;cfoutput&gt;
&lt;form&gt;
&lt;label for=&quot;year&quot;&gt; Year :
&lt;select id=&quot;year&quot;&gt;
&lt;option&gt;&lt;/option&gt;
&lt;cfloop from=&quot;2000&quot; to=&quot;2004&quot; index=&quot;y&quot;&gt;
&lt;option value=&quot;#y#&quot;&gt;#y#&lt;/option&gt;
&lt;/cfloop&gt;
&lt;/select&gt;
&lt;/label&gt;

&lt;label for=&quot;yearNumber&quot;&gt; Number of year to display :
&lt;select id=&quot;yearNumber&quot;&gt;
&lt;option&gt;&lt;/option&gt;
&lt;cfloop from=&quot;1&quot; to=&quot;5&quot; index=&quot;n&quot;&gt;
&lt;option value=&quot;#n#&quot;&gt;#n#&lt;/option&gt;
&lt;/cfloop&gt;
&lt;/select&gt;
&lt;/label&gt;
&lt;/form&gt;
&lt;/cfoutput&gt;
&lt;/div&gt;

&lt;/div&gt;
</pre>
<p>Then some JavaScript. First, a function to call our chart.</p>
<pre class="brush: js">
if ( $j(&#039;.binding&#039;) != null ) {

$j.each($j(&#039;.binding&#039;),function(){
//console.log($j(this));
divid = $j(this).attr(&#039;id&#039;);
event = $j(this).attr(&#039;url&#039;);
//console.log(divid);
doBind({div:divid,url:event});
});

}
</pre>
<p>When the page has loaded, if there is any element with a class named &#8216;binding&#8217;, get some info (the div ID and the url to call), and call the function doBind:</p>
<pre class="brush: js">
function doBind(options) {
var $j = jQuery.noConflict();

//Some option (edit for your own)
var defaults = {
div: &#039;&#039;,
url: &#039;&#039;,
injectResult: &#039;&#039;
};
var options = $j.extend(defaults, options);

//Get the div that we want to update
var updatediv = $j(&#039;#&#039; + options.div);

//Show a loader on request
updatediv.html(&#039;&lt;div&gt;&lt;img src=&quot;ajax-loader.gif&quot;&gt;&lt;/div&gt;&#039;);

//The ajax request
$j.ajax({
type: &#039;GET&#039;,
url: options.url,
timeout: 2000, //You can edit that value to be able to extend the timeout time
success: function(data) {

//empty the container div
updatediv.html(&#039;&#039;);

//Hide the container
updatediv.css(&#039;visibility&#039;,&#039;hidden&#039;);

//Put the data
updatediv.html(data);

//Show the updated div
updatediv.css(&#039;visibility&#039;,&#039;visible&#039;);

},
//On error
error: function (XMLHttpRequest, textStatus, errorThrown) {
updatediv.html(&#039;Timeout contacting server.. &#039;+errorThrown);
}
});
}
</pre>
<p>The function send an ajax request to the url defined in the div, and update the content of the div. The function is well documented I think, but if you have any question, please drop me a comment. We finally need to put some event listener on our form so that at each change, we update the chart.</p>
<pre class="brush: js">
//Put an event listener on every select of the form
$j(&quot;select&quot;).change(function() {

//Get the selects values
var year = $j(&quot;#year option:selected&quot;).text();
var yearNumber = $j(&quot;#yearNumber option:selected&quot;).text();

//Create the url
url = &#039;chart.cfm&#039;;
if (year != &#039;&#039;) url = url +  &#039;?year=&#039; + year;
if (yearNumber != &#039;&#039; &amp;&amp; year != &#039;&#039;) {
url = url +  &#039;&amp;yearNumber=&#039; + yearNumber;
} else if(yearNumber != &#039;&#039; &amp;&amp; year == &#039;&#039;) {
url = url +  &#039;?yearNumber=&#039; + yearNumber;
}

//Call the doBind function and then the options
doBind({
div: &#039;chartholder&#039;,
url: url,

});

})
</pre>
<p>And there you have it! The final JS should look like this.</p>
<pre class="brush: js">
var $j = jQuery.noConflict();

$j(document).ready(function(){

//Put an event listener on every select of the form
$j(&quot;select&quot;).change(function() {

//Get the selects values
var year = $j(&quot;#year option:selected&quot;).text();
var yearNumber = $j(&quot;#yearNumber option:selected&quot;).text();

//Create the url
url = &#039;chart.cfm&#039;;
if (year != &#039;&#039;) url = url +  &#039;?year=&#039; + year;
if (yearNumber != &#039;&#039; &amp;&amp; year != &#039;&#039;) {
url = url +  &#039;&amp;yearNumber=&#039; + yearNumber;
} else if(yearNumber != &#039;&#039; &amp;&amp; year == &#039;&#039;) {
url = url +  &#039;?yearNumber=&#039; + yearNumber;
}

//Call the doBind function and then the options
doBind({
div: &#039;chartholder&#039;,
url: url,

});

})

if ( $j(&#039;.binding&#039;) != null ) {

$j.each($j(&#039;.binding&#039;),function(){
//console.log($j(this));
divid = $j(this).attr(&#039;id&#039;);
event = $j(this).attr(&#039;url&#039;);
//console.log(divid);
doBind({div:divid,url:event});
});

}

});

function updateChart() {

}

function doBind(options) {
var $j = jQuery.noConflict();

//Some option (edit for your own)
var defaults = {
div: &#039;&#039;,
url: &#039;&#039;,
injectResult: &#039;&#039;
};
var options = $j.extend(defaults, options);

//Get the div that we want to update
var updatediv = $j(&#039;#&#039; + options.div);

//Show a loader on request
updatediv.html(&#039;&amp;lt;div&amp;gt;&amp;lt;img src=&quot;ajax-loader.gif&quot;&amp;gt;&amp;lt;/div&amp;gt;&#039;);

//The ajax request
$j.ajax({
type: &#039;GET&#039;,
url: options.url,
timeout: 2000, //You can edit that value to be able to extend the timeout time
success: function(data) {

//empty the container div
updatediv.html(&#039;&#039;);

//Hide the container
updatediv.css(&#039;visibility&#039;,&#039;hidden&#039;);

//console.log(data);

//Put the data
updatediv.html(data);

//Show the updated div
updatediv.css(&#039;visibility&#039;,&#039;visible&#039;);

},
//On error
error: function (XMLHttpRequest, textStatus, errorThrown) {
updatediv.html(&#039;Timeout contacting server.. &#039;+errorThrown);
}
});
}
</pre>
<p>Here is the <a href="http://eremiya.net/wp-content/uploads/2010/06/jquery_flot_1.5_2.zip" target="_self">source code</a> if you want to try it by yourself. This method is a simple way to have a pod being updated by user input, using ajax. But what if I want to interact with the chart directly? I&#8217;ll be back&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://eremiya.net/2010/06/27/jquery-flot-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Jquery, Flot Tutorial (Part 1.5)</title>
		<link>http://eremiya.net/2009/10/16/jquery-flot-tutorial-part-1-5/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-flot-tutorial-part-1-5</link>
		<comments>http://eremiya.net/2009/10/16/jquery-flot-tutorial-part-1-5/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 16:37:35 +0000</pubDate>
		<dc:creator>Jérémie</dc:creator>
				<category><![CDATA[Coldfusion]]></category>
		<category><![CDATA[Jquery]]></category>
		<category><![CDATA[charting]]></category>
		<category><![CDATA[flot]]></category>

		<guid isPermaLink="false">http://eremiya.net/?p=47</guid>
		<description><![CDATA[Sorry for not posting more often. thanks for all the comments. I&#8217;ll work on the frequency of my posts! Flot is indeed a great plugin. There is a lot of things that can be done using it. Let&#8217;s say, I want to create a chart that represent the increase of a given year by month [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for not posting more often. thanks for all the comments. I&#8217;ll work on the frequency of my posts!</p>
<p>Flot is indeed a great plugin. There is a lot of things that can be done using it.<br />
Let&#8217;s say, I want to create a chart that represent the increase of a given year by month (from january to december). There is several approach but I will use:</p>
<ul>
<li>cfc to get the data from the DB</li>
<li>custom axis renderer, for reasons I will explain later</li>
</ul>
<p><span id="more-47"></span><br />
Here is a preview of what we want to achieve.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-59" title="cfc driven flot charting" src="http://eremiya.net/wp-content/uploads/2009/10/Picture-11.png" alt="cfc driven flot charting" width="488" height="191" /></p>
<p>All the data are pulled from the database. For you all to try, I used a dsn that is from the default install of CF. And to make it more &#8220;sexy&#8221;, we&#8217;ll add a zest of ajax!<br />
Let&#8217;s get started. First, the cfc. Of course you can get the data with whatever server technology you are using.<br />
We are using the cfartgallery datasourse. In that datasource, you have a table name APP.ORDERS. Basically, in this table, there is the orders for the art gallery application. What we want to do is to call a function that will return a query with the monthly order amount for a special year.</p>
<pre class="brush: coldfusion">&lt;cffunction name=&quot;getByDate&quot; output=&quot;false&quot; access=&quot;public&quot; returntype=&quot;query&quot;&gt;
&lt;cfargument name=&quot;year&quot; type=&quot;Numeric&quot; required=&quot;false&quot;&gt;

&lt;cfset nq = QueryNew(&quot;orderdate, total&quot;, &quot;VarChar, Integer&quot;)&gt;

&lt;cfloop from=&quot;1&quot; to=&quot;12&quot; index=&quot;i&quot;&gt;

&lt;cfquery name=&quot;q&quot; datasource=&quot;cfartgallery&quot;&gt;
SELECT Year(ORDERDATE) AS ORDERYEAR, TOTAL
FROM APP.ORDERS
WHERE Month(ORDERDATE) = #i#
AND Year(ORDERDATE) = #year#
&lt;/cfquery&gt;

&lt;cfset temp = QueryAddRow(nq)&gt;
&lt;cfset Temp = QuerySetCell(nq, &quot;orderdate&quot;, year &amp; &#039;/0&#039; &amp; i)&gt;
&lt;cfset Temp = QuerySetCell(nq, &quot;total&quot;, Val(q.total))&gt;

&lt;/cfloop&gt;

&lt;cfreturn nq&gt;
&lt;/cffunction&gt;</pre>
<p>In this quite simple function, we ccreate a new query named nq then loop for 1 to 12 (which are months of the year, from january to december) the we query the database for each one of those months then we append the new query nq with the total and the date with the format &#8220;YYYY/M&#8221; (no leading 0). Notice that the only argument we are passing to that function is the year. We&#8217;ll come to that later. Notice also that the data type for the orderdate is VarChar. We don&#8217;t want to receive a date from the cfc, because it might cause some problems later.</p>
<p>Now, we are going to add a new function to our cfc that we are going to call from our cfm page.</p>
<pre class="brush: coldfusion">&lt;cffunction name=&quot;getStructForChart&quot; output=&quot;false&quot; access=&quot;remote&quot; returntype=&quot;any&quot;&gt;

&lt;cfargument name=&quot;year&quot; type=&quot;Numeric&quot; required=&quot;false&quot; default=&quot;2004&quot;&gt;
&lt;cfargument name=&quot;yearNumber&quot; type=&quot;Numeric&quot; required=&quot;false&quot; hint=&quot;Number of preceding years&quot;&gt;

&lt;cfset myStruct = arraynew(1)&gt;

&lt;cfset data = getByDate(arguments.year)&gt;
&lt;cfset ArrayAppend(myStruct,data)&gt;

&lt;cfif arguments.yearNumber gt 0&gt;
&lt;cfset arguments.yearNumber = arguments.yearNumber - 1&gt;
&lt;cfloop from=&quot;1&quot; to=&quot;#arguments.yearNumber#&quot; index=&quot;y&quot;&gt;
&lt;cfset data = getByDate(arguments.year-y)&gt;
&lt;cfset ArrayAppend(myStruct,data)&gt;
&lt;/cfloop&gt;
&lt;/cfif&gt;

&lt;cfreturn myStruct&gt;

&lt;/cffunction&gt;</pre>
<p>In this function, we retrieve the year argument and this time, it has a default value. Because of the data in the table, we set it 2004, but of course in your application, you can set it to whatever is fine, for exaple #Year(Now())# for the current year. We also introduce a new argument &#8220;yearNumber&#8221; which is the number of preeceding year we want to display.</p>
<p>Here we create an array that will be transform to json when we call the cfc using jquery. In this array, we will put the query returned from the getByDate function. If the yearNumber argument is greater than 0, we loop from 1 to the argument yearNumber and we call the function again by passing a new value for the year argument. Finally, we will append the returned query to the array. That&#8217;s it for the cfc.</p>
<p>Now, let&#8217;s create the javascript function.</p>
<pre class="brush: js">var $j = jQuery.noConflict();

$j(document).ready(function(){

//getData();
drawChart();

});</pre>
<p>Just to remind you, as I use different javascript libraries, I defined var $j = jQuery.noConflict(); to avoid conflict.</p>
<p>In my <a href="http://www.eremiya.net/?p=7" target="_blank">previous blog</a>, I specified an array that will be the data store. Let&#8217;s define it again.</p>
<pre class="brush: js">
var dataObject = [];
var plotarea = $j(&quot;#placeholder&quot;);
</pre>
<p>Now let&#8217;s defined too important variables and a handy array that we will use for our custom axis rendering:</p>
<pre class="brush: js">
var &lt;cfoutput&gt;#toScript(url.year, &quot;year&quot;)#&lt;/cfoutput&gt;
var &lt;cfoutput&gt;#toScript(url.yearNumber, &quot;yearNumber&quot;)#&lt;/cfoutput&gt;

var months = [&#039;jan&#039;, &#039;feb&#039;, &#039;mars&#039;, &#039;apr&#039;, &#039;may&#039;, &#039;jun&#039;, &#039;jul&#039;, &#039;aug&#039;, &#039;sep&#039;, &#039;oct&#039;, &#039;nov&#039;, &#039;dec&#039;];
</pre>
<p>The months array is basically an array of strings, but you could put variables if for example your site is multilingual and using a resource bundle. the too variables get the url var of the year and the yearNumber and pass them to the following ajax call to the cfc:</p>
<pre class="brush: js">
$j.getJSON(
&#039;http://192.168.0.100:81/model/cfc/service/charting.cfc?wsdl&#039;,
{ method : &#039;getStructForChart&#039;, returnformat : &#039;json&#039;, queryformat : &#039;column&#039;, year : year, yearNumber : yearNumber },
function(data) {
</pre>
<p>A great thing with cfc is that you can call it directly and specify that you want a Json result!!! That is a great gain of time!! You can change the url of your cfc but do not forget the  ?wsdl at the end. That makes the magic! Once we receive the response, let&#8217;s populate the dataObject array.</p>
<pre class="brush: js">
for (var a=0; a&lt;data.length; a++) {

eval(&quot;data&quot; + a + &quot; = []&quot;);

for (var i=0; i&lt;data[a].ROWCOUNT; i++) {

var myDate = new Date(data[a].DATA.orderdate[i]+&#039;/01&#039;);
eval(&quot;data&quot; + a).push([myDate.getMonth(),data[a].DATA.total[i]]);
var dataYear = myDate.getFullYear();

}

dataObject.push({label:dataYear,data:eval(&quot;data&quot; + a)})

}
</pre>
<p>Here, we loop through the response (which is an array of objects) and we create a dynamic array (eval(&#8220;data&#8221; + a + &#8221; = []&#8220;);) to push our formated data in. then we push the month of our newly formated date (formated for js using the new Date() function) and the according order amount. And finally we push that dynamic array to our dataObject array. Now the rest is just formating.</p>
<pre class="brush: js">
var options = {
legend: {
show: true,
margin: 10,
backgroundOpacity: 0.5
},
points: {
show: true,
radius: 6
},
lines: {
show: true
},
grid: {
borderWidth:0,
labelMargin:15,
clickable:true,
hoverable:true,
autoHighlight:true
},
xaxis: {
tickSize: 1,
tickFormatter: function(val,axis) {
return months[val];
}
}
};
</pre>
<p>There is some changes from my <a href="http://www.eremiya.net/?p=7" target="_blank">previous post</a>, the radius of the point to make them bigger, no formatting on the vertical axis (yaxis), the points are clickable now, and I have a custom renderer on my horizontal axis (xaxis). That function return the month name defined in the months array for a given value. We&#8217;re almost done. Let&#8217;s draw the chart and add a function to handle the click on a point:</p>
<pre class="brush: js">
$j.plot(plotarea, dataObject, options);

$j(&quot;#placeholder&quot;).bind(&quot;plotclick&quot;, function (event, pos, item) {
alert(&#039;In &#039;+item.series.label+&#039;/&#039;+item.datapoint[0]+&#039;, the order amout is &#039;+item.datapoint[1]);
});</pre>
<p>In that function, when a button is clicked, three variables are passed to the function. the item variable is, as its name tells, the item values (the horizontal and vertical data).  The item.series.label return the label (here the year of the series). Here is the final code:</p>
<pre class="brush: coldfusion">&amp;lt;cfparam name=&quot;url.year&quot; type=&quot;numeric&quot; default=&quot;2004&quot;&amp;gt;
&amp;lt;cfparam name=&quot;url.yearNumber&quot; type=&quot;numeric&quot; default=&quot;0&quot;&amp;gt;
&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&amp;gt;
&amp;lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&amp;gt;

&amp;lt;title&amp;gt;CFC Driven Chart&amp;lt;/title&amp;gt;
&amp;lt;meta name=&quot;description&quot; content=&quot;eremiya&quot; /&amp;gt;

&amp;lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;jquery/jquery-1.3.2.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;js/jquery/flot/jquery.flot.js&quot;&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;div id=&quot;placeholder&quot; style=&quot;width:800px;height:300px&quot;&amp;gt;&amp;lt;/div&amp;gt;

&amp;lt;script id=&quot;source&quot; language=&quot;javascript&quot; type=&quot;text/javascript&quot;&amp;gt;

var $j = jQuery.noConflict();

$j(document).ready(function(){

//getData();
drawChart();

});

function drawChart() {

var dataObject = [];
var plotarea = $j(&quot;#placeholder&quot;);

var &lt;cfoutput&gt;#toScript(url.year, &quot;year&quot;)#&lt;/cfoutput&gt;
var &lt;cfoutput&gt;#toScript(url.yearNumber, &quot;yearNumber&quot;)#&lt;/cfoutput&gt;

var months = [&#039;jan&#039;, &#039;feb&#039;, &#039;mars&#039;, &#039;apr&#039;, &#039;may&#039;, &#039;jun&#039;, &#039;jul&#039;, &#039;aug&#039;, &#039;sep&#039;, &#039;oct&#039;, &#039;nov&#039;, &#039;dec&#039;];

$j.getJSON(
&#039;http://192.168.0.100:81/model/cfc/service/charting.cfc?wsdl&#039;,
{ method : &#039;getStructForChart&#039;, returnformat : &#039;json&#039;, queryformat : &#039;column&#039;, year : year, yearNumber : yearNumber },
function(data) {
for (var a=0; a&lt;data.length; a++) {

eval(&quot;data&quot; + a + &quot; = []&quot;);

for (var i=0; i&lt;data[a].ROWCOUNT; i++) {

var myDate = new Date(data[a].DATA.orderdate[i]+&#039;/01&#039;);
eval(&quot;data&quot; + a).push([myDate.getMonth(),data[a].DATA.total[i]]);
var dataYear = myDate.getFullYear();

}

dataObject.push({label:dataYear,data:eval(&quot;data&quot; + a)})

}

var options = {
legend: {
show: true,
margin: 10,
backgroundOpacity: 0.5
},
points: {
show: true,
radius: 6
},
lines: {
show: true
},
grid: {
borderWidth:0,
labelMargin:15,
clickable:true,
hoverable:true,
autoHighlight:true
},
xaxis: {
tickSize: 1,
tickFormatter: function(val,axis) {
return months[val];
}
}
};

$j.plot(plotarea, dataObject, options);

$j(&quot;#placeholder&quot;).bind(&quot;plotclick&quot;, function (event, pos, item) {
alert(&#039;In &#039;+item.series.label+&#039;/&#039;+item.datapoint[0]+&#039;, the order amout is &#039;+item.datapoint[1]);
});
}
);

}
&amp;lt;/script&amp;gt;

&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;</pre>
<p>Quite a long post, sorry about that! Please tell me what you think about it! And <a href="http://eremiya.net/wp-content/uploads/2009/10/jquery_flot_1.5.zip">here are the download files</a>!<br />
Hey, but what if we want to change the number of year using ajax?&#8230; &#8220;I&#8217;ll be back&#8230; sooner&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://eremiya.net/2009/10/16/jquery-flot-tutorial-part-1-5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Jquery, Flot Tutorial (Part 1)</title>
		<link>http://eremiya.net/2009/07/08/jquery-flot-tutorial-part-1/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-flot-tutorial-part-1</link>
		<comments>http://eremiya.net/2009/07/08/jquery-flot-tutorial-part-1/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 10:13:35 +0000</pubDate>
		<dc:creator>Jérémie</dc:creator>
				<category><![CDATA[Jquery]]></category>
		<category><![CDATA[charting]]></category>
		<category><![CDATA[flot]]></category>

		<guid isPermaLink="false">http://eremiya.net/?p=7</guid>
		<description><![CDATA[Flot is a great plugin for JQuery! I decided to make a simple tutorial in this part and then try to have something more complex (ajax request pulling out data from the database).  Let&#8217;s make things first: let&#8217;s invoke the scripts. &#60;script src=&#34;../jquery/jquery-1.3.2.js&#34; type=&#34;text/javascript&#34;&#62;&#60;/script&#62; &#60;script src=&#34;../js/jquery/flot/jquery.flot.js&#34; type=&#34;text/javascript&#34;&#62;&#60;/script&#62; &#60;!--[if IE]&#62;&#60;script language=&#34;javascript&#34; type=&#34;text/javascript&#34; src=&#34;../excanvas.pack.js&#34;&#62;&#60;/script&#62;&#60;![endif]--&#62; Then, let&#8217;s place [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Flot" href="http://code.google.com/p/flot/" target="_blank">Flot</a> is a great plugin for JQuery! I decided to make a simple tutorial in this part and then try to have something more complex (ajax request pulling out data from the database). <span id="more-7"></span></p>
<p>Let&#8217;s make things first: let&#8217;s invoke the scripts.</p>
<pre class="brush: js">&lt;script src=&quot;../jquery/jquery-1.3.2.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;../js/jquery/flot/jquery.flot.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;!--[if IE]&gt;&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;../excanvas.pack.js&quot;&gt;&lt;/script&gt;&lt;![endif]--&gt;</pre>
<p>Then, let&#8217;s place a div that will contain the chart.</p>
<pre class="brush: html">&lt;div id=&quot;placeholder&quot; style=&quot;width:600px;height:300px&quot;&gt;&lt;/div&gt;</pre>
<p>Now, let&#8217;s put our script to initialize the chart:</p>
<pre class="brush: js">&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;

var $j = jQuery.noConflict();

$j(function () {

var dataArray = [];

for (var i=1; i&lt;13; i++) {
dataArray.push([2000+i,Math.random()*11]);
}

//console.log(dataArray);

var data = [
{
label: &quot;Yearly increase&quot;,
data: dataArray
}
];

var options = {
legend: {
show: true,
margin: 10,
backgroundOpacity: 0.5
},
points: {
show: true,
radius: 3
},
lines: {
show: true
},
grid: {
borderWidth:0
},
xaxis: {
tickSize:1
},
yaxis: {
tickSize:1,
tickDecimals: 0
}
};

var plotarea = $j(&quot;#placeholder&quot;);

$j.plot( plotarea , data, options );
});
&lt;/script&gt;</pre>
<p>Let me explain: as I am using other js libraries, I did put the first line to avoid conflict</p>
<pre class="brush: js">var $j = jQuery.noConflict();</pre>
<p>then i specified an array that will be my data store. Math.random()*11 will generate a number randomly between 0 and 10.</p>
<pre class="brush: js">

var dataArray = [];

for (var i=1; i&lt;13; i++) {
dataArray.push([2000+i,Math.random()*11]);
}
</pre>
<p>As I would like to have a label on my chart, I can specify that in the data format and pass my dataArray (If I have multiple dataset, that is there I should set them)</p>
<pre class="brush: js">

var data = [
{
label: &quot;Yearly increase&quot;,
data: dataArray
}
];
</pre>
<p>And I set the options for my grid, x and y axes, the legend and so on&#8230;</p>
<pre class="brush: js">

var options = {
legend: {
show: true,
margin: 10,
backgroundOpacity: 0.5
},
points: {
show: true,
radius: 3
},
lines: {
show: true
},
grid: {
borderWidth:0
},
xaxis: {
tickSize:1
},
yaxis: {
tickSize:1,
tickDecimals: 0
}
};
</pre>
<p>in more details:</p>
<ul>
<li><span style="color: #800000;">legend</span> will let you customize the &#8230; legend</li>
<li><span style="color: #800000;">points</span> &amp; <span style="color: #800000;">lines</span> are the type I chose for my series (you could choose bars or the newly pie type)</li>
<li>the <span style="color: #800000;">grid</span> is the background of your chart</li>
<li><span style="color: #800000;">xaxis</span> is the &#8220;horizontal&#8221; line and the <span style="color: #800000;">yaxis</span>, the &#8230; vertical one (you&#8217;re maybe not that newbie, sorry!)</li>
</ul>
<p>for more details, you can check the <a title="API text" href="http://people.iola.dk/olau/flot/API.txt" target="_blank">api text document</a>.</p>
<p>Finally get the placeholder and call flot to draw the chart by passing the variables to the plot function:</p>
<pre class="brush: js">

var plotarea = $j(&quot;#placeholder&quot;);

$j.plot( plotarea , data, options );
</pre>
<p>Here is the final code:</p>
<pre class="brush: xml">&amp;lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&amp;gt;
&lt;html&gt;
&lt;head&gt; &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt; &lt;title&gt;Flot Examples&lt;/title&gt; &lt;link href=&quot;layout.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;&lt;/link&gt; &lt;!--[if IE]&gt;&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;../excanvas.pack.js&quot;&gt;&lt;/script&gt;&lt;![endif]--&gt; &lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;../jquery/jquery-1.3.2.js&quot;&gt;&lt;/script&gt; &lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;../js/jquery/flot/jquery.flot.js&quot;&gt;&lt;/script&gt; &lt;/head&gt;
&lt;body&gt; &lt;h2&gt;Chart&lt;/h2&gt;
&lt;div id=&quot;placeholder&quot; style=&quot;width:600px;height:300px&quot;&gt;&lt;/div&gt;
&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;

var $j = jQuery.noConflict();

$j(function () {

var dataArray = [];

for (var i=1; i&lt;13; i++) {
dataArray.push([2000+i,Math.random()*11]);
}

//console.log(dataArray);

var data = [
{
label: &quot;Yearly increase&quot;,
data: dataArray
}
];

var options = {
legend: {
show: true,
margin: 10,
backgroundOpacity: 0.5
},
points: {
show: true,
radius: 3
},
lines: {
show: true
},
grid: {
borderWidth:0
},
xaxis: {
tickSize:1
},
yaxis: {
tickSize:1,
tickDecimals: 0
}
};

var plotarea = $j(&quot;#placeholder&quot;);

$j.plot( plotarea , data, options );
});
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>And here is the final result in your browser:</p>
<p style="text-align: center;"><img class="size-full wp-image-36 aligncenter" title="chart" src="http://eremiya.net/wp-content/uploads/2009/07/chart.png" alt="line chart using jquery flot" width="563" height="319" /></p>
<p style="text-align: left;">What if now, we want to pull the data from the database using cf&#8230; &#8220;I&#8217;ll be back&#8221;!</p>
]]></content:encoded>
			<wfw:commentRss>http://eremiya.net/2009/07/08/jquery-flot-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

