I built this for one small project then found myself using it all over the place. It's basically a generic JavaScript 'class'* that allows you to easily build YUI charts with very little JavaScript code.
If you use YUI charts as suggested in many examples you'll need configuration code for each individual chart which, if you have more than 2 or 3 charts on a page, can make your code appear cluttered. With the ChartsAjax class, all the chart options can be set with JSON data built dynamically on the server and retrieved from one AJAX call. This includes not only data for the datastore but also styling and column definitions.
Below is a breakdown with code examples. Feel free to offer suggestions on how to make it better!
- The very first thing you need to do is go to the YUI developer page and select the components you want to use then include the CSS/JavaScript per their instructions.
- Include the ChartsAjax and AjaxObject code in your page.
function ChartsAjax(){ var statType=''; var chartType=''; this.buildChart = buildChart; this.useData = useData; this.handleFailure = handleFailure; function buildChart(type, chart){ statType = type; chartType = chart; var sourceUrl = '/stats/get_chart/'+statType+'/'; var callback = {success:this.useData,failure:this.handleFailure}; AjaxObject.makeRequest(sourceUrl, callback); }; function useData(o){ try {var json = YAHOO.lang.JSON.parse(o.responseText);} catch (e) {return false;} YAHOO.widget.Chart.SWFURL = "http://yui.yahooapis.com/2.7.0/build/charts/assets/charts.swf"; var msDs = new YAHOO.util.DataSource( json.Response.Results ); msDs.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; msDs.responseSchema = { fields:json.Response.Fields }; var targetDiv = statType; var chartDefinition = { series: json.Response.Series, xField: json.Response.XField, yField: json.Response.YField, style: json.Response.Style } switch(chartType){ case "line": var chart = new YAHOO.widget.LineChart(targetDiv, msDs, chartDefinition); break; case "column": var chart = new YAHOO.widget.ColumnChart(targetDiv, msDs, chartDefinition); break; } }; function handleFailure(o){ //do something with o.responseText } }; var AjaxObject = { makeRequest:function(url, callback){var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);} }; - Create an empty container div. Remember that the chart takes on the dimensions of it's parent container so you could either apply a class to this div or a style attribute (ex: style="width:500px;height:200px").
<div id="products_sold" >Loading...</div>
- On page load, create a ChartsAjax instantiation. Notice the second argument in the buildChart method. You can choose to display the chart as 'line' or 'column'. You can also add options to the ChartsAjax class as you wish. ex:
function load_charts(){ var products_sold = new ChartsAjax(); products_sold.buildChart('products_sold', 'line'); } YAHOO.util.Event.onDOMReady(load_charts); - Return data from server. The class will use the sourceUrl variable to request data from your server. You can configure the url to fit your needs. The JSON can be built using any server side language. My favorite is python because the list and dictionary data structures are virtually identical to the resulting JSON. Example json:
{"Response": { "YField": "", "Style": { "legend": { "display": "bottom"}, "xAxis": {"labelRotation": "-90"}, "yAxis": {"titleRotation": "-90"}}, "Fields": ["date", "total"], "Results": [ {"date": "05-27", "total": 147}, {"date": "05-28", "total": 105}, {"date": "05-29", "total": 160}, {"date": "05-30", "total": 40}, {"date": "05-31", "total": 66}, {"date": "06-01", "total": 136}, {"date": "06-02", "total": 243}], "Series": [{ "yField": "total", "displayName": "Total Products Sold"}], "XField": "date"}} - Presto!
Not sure if you've checked it out:
http://omnipotent.net/jquery.sparkline/ nice lib if you are on the jquery side of the fence.
Posted by: Hardy | 06/12/2009 at 07:05 AM
looks like a good lib. I definitely prefer the cleanliness of jQuery and most of it's plugins, especially compared to YUI's verbose syntax. However, I do like having EVERYTHING hosted and ALL the documentation centralized. I only use it for admin consoles though, since it's so freakin bloated. Even though it's frowned upon, I love mixing the two together, especially since they're both available through the google API (http://code.google.com/apis/ajaxlibs/)
Posted by: Aaron | 06/12/2009 at 07:24 AM
Beautiful! For a while now I've been puzzling over how to make the complete charts component much more dynamic rather than having to write a function for each chart I need.
Using the response from the ajax to get all of the fields and chart settings is simply genius!
Thanks a lot!
Posted by: Alex Hall | 12/10/2009 at 03:39 AM
Don't know what is wrong what is rite but i know that every one has there own point of view and same goes to this one..
Posted by: SEO Company | 07/02/2010 at 12:27 PM
I believe that that more study we are, much more knowledge we possess. As long as we believe ourself, our dream will come true. I know this principle from your blog. Thank you so much.
Posted by: Nike Shox R4 | 07/06/2010 at 08:14 PM
Congratulations! You have so much useful information, write more.
Posted by: RamonGustav | 08/23/2010 at 03:00 AM
I think you are not quite right and you should still studying the matter.
Posted by: RamonGustav | 08/23/2010 at 03:03 AM
I in reality take pleasure in all that you just fill your weblog with. Your recommendations are especially painless to know! Thank you for exhibiting us so superb document.
Posted by: AJF 12 | 09/13/2010 at 06:53 PM
The fate of the heart is romantic dreams, thoughts on heart is fragrant memories, friendship is the sweet heart rhythm, the music you record in the heart is sweet beauty, a salute alcohol or, eternal friendship remains and permanent wish everlasting friendship! Wish you happy every day!
Posted by: New Balance Shoes | 09/24/2010 at 06:02 PM
i don't understand Completely....
Posted by: tibia gold | 09/25/2010 at 02:13 AM
Subsequent time once you are feeling down, strive placing on a smile. There is an outstanding prospect you mood will adjust for the far better.
Posted by: New Jordans | 10/09/2010 at 03:15 AM
The secret of being miserable is to have leisure to bother about whether you are happy or not .
Posted by: new balance | 10/17/2010 at 08:26 PM
I think you are not quite right and you should still studying the matter.
Posted by: Vidos_viagros | 10/26/2010 at 02:00 PM
I think you are not quite right and you should still studying the matter.
Posted by: Vidos_viagros | 10/26/2010 at 02:00 PM
Have you been turned down by other lenders?
Posted by: Vidos_viagros | 10/26/2010 at 02:01 PM
There should be a better way to start a day than waking up every morning.
Posted by: cheap chanel handbags | 11/07/2010 at 11:58 PM
Yes, I agree this comment. When I opened this site, I feel very comfortable to learn this knowledge. Next time I will face any problem and any found details than I will go to on this site.
Posted by: nike shox | 11/09/2010 at 12:39 AM
The great pleasure in life is doing what people say you can not do
Posted by: Jordan 12 | 11/09/2010 at 12:49 AM
You're fantastic.Continue to work hard. I really like this comment,
Posted by: Supra Shoes | 11/10/2010 at 02:12 AM
oh so funny!
Posted by: jordan france | 11/11/2010 at 12:21 AM
Life is a struggle, accept it.
Posted by: Air Jordans | 11/11/2010 at 04:21 PM
I can are living for two months' on the terrific compliment. (Mark Twain, us writer)
Posted by: shox shoes | 11/11/2010 at 04:23 PM
No neighborhood could build a perpetual constitution, as well just like a perpetual law.(Thomas Jefferson, the states president)
Posted by: shox shoes | 11/12/2010 at 06:41 PM
oh I hope you all have a blessed days!
Posted by: shop taobao china | 11/15/2010 at 11:52 PM
A drawback may be that a lot of people don't know sponsored results from regular ones. So asking people whether they click on them or not might not give accurate feedback.*-*
Posted by: air max LTD | 12/02/2010 at 07:26 PM