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!
I differ with most people here; I found this post I couldn't stop until , even though it wasn't just what I had been searching for, was indeed a great read though. I will instantly take your feed to stay in touch of future updates.
Posted by: mover new York | 12/03/2010 at 11:11 AM
I am still waiting for your response you found after trying a new methods about publishing blogs.
Posted by: moving new York companies | 12/03/2010 at 11:12 AM
Wish I have the luxury of time to consider using the benefits these site can offer.....
Posted by: cheap new York moving | 12/03/2010 at 11:13 AM
Merry Christmas! I wish you a lot of gifts and luck in the new year.
Posted by: Antivirus_man | 12/05/2010 at 02:53 AM
You write well will be waiting for your new publications.
Posted by: Antivirus_man | 12/05/2010 at 02:54 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: skate shoes adidas | 12/11/2010 at 01:52 AM
I differ with most people here; I found this post I couldn't stop until , even though it wasn't just what I had been searching for, was indeed a great read though. I will instantly take your feed to stay in touch of future updates.
Posted by: skateboarding shoes adidas | 12/11/2010 at 01:53 AM
I'm glad I came across your blog today as it will now be part of my daily reading.I think ur blog is great for providing to everybody something unique to know.The information you provide is really helpful.
Posted by: adidas skate shoes review | 12/11/2010 at 01:54 AM
Michael Randolph was in his father's investment company, http://www.womenshoesbox.com/ ugg boots in fraud case has not been charged
but witnesses in cases. His lawyer yesterday confirmed thedeath, saying he was in the past two years due to false allegations and under great pressure. December 11, was arrested at Bernard Madoff ? 2 year anniversary, he was found hanging dead in New York residence, aged 46 years.
Posted by: ugg boots | 12/12/2010 at 07:56 PM
I differ with most people here; I found this post I couldn't stop until , even though it wasn't just what I had been searching for, was indeed a great read though. I will instantly take your feed to stay in touch of future updates.
Posted by: computer support los angeles | 12/16/2010 at 03:48 AM
I appreciate when I see well writen material. Your time isn't going to waste with your posts. Thanks so much and stick with it No doubt you will defintely reach your goals! have a great day!
Posted by: residential painting services | 12/18/2010 at 01:35 AM
I liked your site, you are very interesting to write. Merry Christmas and Happy New Year!
Posted by: JOBS_frend | 12/25/2010 at 03:52 AM
Great job!! it's really helpful and informative also.Although i don't understand it clearly but it may helpful for anyone who are interested.Continue your writing.I appreciate your article .
Posted by: Bianca | 01/13/2011 at 07:09 AM
Where should their loyalty lie and what should be the consequences of their actions ?
Posted by: Jordan 1 | 01/13/2011 at 06:54 PM
Hola,
Ha hecho un trabajo muy bueno. Hay muchas personas en busca de eso ahora van a encontrar suficientes fuentes por tus consejos.
espera para obtener más consejos acerca de que
Posted by: Generic Cialis | 02/14/2011 at 11:44 PM
Wow, nice post,there are many person searching about that now they will find enough resources by your post.Thank you for sharing to us.Please one more post about that..
Posted by: Generic Viagra | 03/08/2011 at 11:33 PM
I liked your site, you are very interesting to write.http://www.cnn.com/
Posted by: adwier | 04/01/2011 at 11:39 PM
There are may person searching about that now they will find enough resources by your post,
Posted by: Networking solutions | 04/02/2011 at 05:17 AM
Hey, Great post **
Posted by: オラセフ | 04/06/2011 at 01:44 AM
Please one more post about that..
I wonder how you got so good. This is really a fascinating blog, lots of stuff that I can get into. One thing I just want to say is that your Blog is so perfect!
Posted by: Generic Viagra | 04/09/2011 at 05:11 AM
Hola,Ha hecho un trabajo muy bueno. Hay muchas personas en busca de eso ahora van a encontrar suficientes fuentes por tus consejos.espera para obtener más consejos acerca de que
Posted by: Generic Vantin | 04/13/2011 at 03:52 AM
Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!....
Posted by: careprost | 04/16/2011 at 01:50 AM
There is an outstanding prospect you mood will adjust for the far better.
Posted by: ClubPenguinCheats | 05/04/2011 at 06:20 PM
Thank you for sharing. Very happy to see your article, I very much to like and agree with your point of view. Have a good time.
Posted by: baby products | 05/04/2011 at 11:32 PM
The General MEDION 40010871 Akku Administration of MEDION 40011354 Akku Customs (GAC) sai
Posted by: Laptop akkus | 05/10/2011 at 10:35 PM