Create Charts and Graphs for SharePoint Dashboards
Claudio Cabaleyro wrote a series of articles for us on using the Google Charts API to expose charts and graphs in SharePoint without recourse deployment or server installation. We have put together a live, online workshop, SharePoint Charts and Graphs without Excel Services, that utilizes some of Claudio’s solutions.
In this three minute screencast, I show how data from a simple SharePoint list can be pulled into a Content Editor Web Part to dynamically generate a bar chart. The cut-and-paste-code for the solution is below the screencast.
To implement the solution, create a view that exposes sums at the top of the columns. Put a CEWP on the page and paste the code. We are still tweaking the code a bit to make it more flexible, but this should work as is.
If you like this solution, we’ll be showing much more during the workshop. Hope to see you there.
<div id="jLoadMe" class="content"></div> <script type="text/javascript"> if(typeof jQuery=="undefined"){ var jQPath="/javascripts/"; document.write("<script src='",jQPath,"jquery.min.js' type='text/javascript'><\/script>"); } </script> <script type="text/javascript"> /* * Google Chart for list column totals * By Claudio Cabaleyro (2009) */ $("document").ready(function ($) { Array.prototype.max = function() { // function to get the maximum value of Y axis var max = this[0]; var len = this.length; for (var i = 1; i < len; i++) if (this[i] > max) max = this[i]; return max; } function BuildChartURL(Data, Names) // Adjust Chart Properties below - See Google Charts API for reference { MaxData= Math.round(1.1*Data.max()).toString(); var DataPoints= "&chd=t:"+Data.join(","); var DataTitles= "&chl="+Names.join("|"); var ChType= "cht=bvs"; // Vertical bars type var ChSize = "&chs=300x200"; //HeighxWidth in px var ChScale = "&chds=0,"+MaxData; //Vertical scale: 0 through 10% over max series value var ChLabels ="&chm=N,000000,0,-1,11,-1"; return ("<p><IMG src='http://chart.apis.google.com/chart?"+ChType+ChSize+DataPoints+DataTitles+ChScale+ChLabels+"'/></p>"); } var ArrayTitle= $(".ms-unselectedtitle .ms-vb",".ms-viewheadertr "); //find all columns in titles row var TitleTxt = new (Array); $.each(ArrayTitle, function(i,e) { TitleTxt[i] = $(e).text(); // Store ALL column titles in TitleTxt }); var ArrayTotal=$('#aggr tr > td:not(.ms-vb)') //find all columns in totals row var TotalTxt = new (Array); $.each(ArrayTotal, function(i,e) { TotalTxt[i] = $(e).text().substr($(e).text().indexOf("= ")+2); // Store ALL column totals in TotalTxt }); var Titles = new (Array); var Totals = new (Array); $.each(TitleTxt, function(i,e) // clear empty elements in totals row { if (TotalTxt[i] != "" && TotalTxt[i] != null) { Titles.push(TitleTxt[i]); Totals.push(parseFloat(TotalTxt[i].replace(',',''))); } }); $("#jLoadMe").append(BuildChartURL(Totals, Titles)) // alert(BuildChartURL(Totals, Titles)); // debug line })(jQuery); </script>
Awesome Chicago photo by the John Hancock tower Mark! I hope you had a good time in the Windy City for #SPSaturday back in June.
Jeff – I didn’t think anyone would notice! Orion took that picture after a great time at the top. He wants to bring the whole family next time. — Mark
Claudio’s code and the solutions presented are brilliant! This is precisely what we needed, especially the multiple graphs on a single page. We are not coders or developers by any means and since we are a non-profit, we have no budget for SharePoint add-ons like EPM Live, nor do we have budget for developer time.
A couple of quick(?) questions:
1) Let’s say we have a list of projects that includes budget information. We want to create a line chart that shows the projects across the x-axis and the budget amounts as data points with the dollar amounts along the y-axis. Using the sum method proposed by Mark Miller doesn’t help here. Neither does using the grouping method that Claudio outlines on his pages. How would we create such a chart?
2) Let’s say we have the same list of projects from #1. We would again like to do a line chart with the budget information but do a second line on the same graph with Actual Cost for each project; i.e. the underlying question is how do we create multiple lines on a single chart for comparison?
Best regards-
Daniel
This amy be a dumb question, but as i understood this, you copy and past the script into a CEWP. But where in script, do you tell it what list to use?
That’s one of the short-comings I have found with this script. Don’t get me wrong, I think it is a huge step in the right direction for creating dashboards in SP. However, it loops through and picks up the results for each list you have in the webpart. Thus, if you want 4 graphs, you need 4 copies of the list, grouped by whatever metric you are trying to graph.
You can mix and match lists too.
What I would like to do is chart things like budget for a project where I have a list with updates throughout the lifecycle of the project. The Budget at Completion field would be one line on the graph, the Earned Value would be another, etc.
I do not see the code that is supposed to be available below the screencast.
Ken – It should be available now. — Mark