Thursday 14 October 2010

Creating piecharts in SharePoint

Having recently discovered how to produce bar charts from SharePoint data we have been getting requests for Pie Charts.  Bar charts are no great shakes, just an XSLT stylesheet controlling the relative width of two cells in a table, one with a coloured background and one with a transparent background, pie charts are altogether more complex (unless you have MOSS and so Excel services, but we don't).

Initially we found an article on EndUserSharePoint on how to do this using a service from Google.  This works well but has the disadvantage that the data has to be sent to Google's servers which then return the piechart so the user has to be connected to the internet and there's a chance of data leakage.

We then found a solutions for Secure Pie Charts on SharePoint which gets around this by using a solution from Yahoo!, storing the files locally and callugn them from some Javascript.  Well worth a look, I think.

We implemented it, I got it working on Monday and one of my colleagues struggled to get it implemented on Thuresday.  We eventually got it working but only when I remembered something else we had to edit to get it working, which isn't mentioned in the original blog entry (or if it is I missed it) and is easy to miss in the Javascript.

The below assumes that you have read the  blog entry linked above, downloaded the files, unpacked them as recommended and have the Javascript that calls the files open and ready to paste into the page in SharePoint Designer.  We tested this on Sharepoint 2007/WSS3.0 using Sharepoint Designer 2007 no warranty is given or implied, try it first on a test/sandpit server and if you screw up your production or any other server following these suggestions then it's your problem not ours. 

If you paste just the Javascript into a text editor before pasting it into the page the lines you need to edit are 27 (line 27 starts "<link rel="stylesheet" type="text/css" href=") through 39 to point to the local copies of the various files.  The original blog entry recommends putting the files in a document library (create one that isn't shown on the Quick list for this purpose) then using the "Copy Shortcut" on the Right-Click menu to get the URL to paste, I would recommend following that advice.  Then do the same for line 47 (setting the jQPath variable) with the path to jquery.js. but delete the filename leaving the final forward slash (so if the full shortcut is "https://myserver.org/resources/jquery.js" the line should read "var jQPath="https://myserver.org/resources/";).  The penultimate step is to edit line 123 to change the URL for charts.swf to the one for that file on your local server.

What caught us out the second time around is line 71 which sets the value of the variable chartkeyword.  Set this to the name of the field you are grouping by in the view.

Paste the resulting code into the Content Editor Web Part (use Code view) the original blog told you to put on the page, making sure to put it between the <![CDATA and ]] tags.  You should be ready to go and able to view the pie chart in IE.

If you can't see the chart first try pasting in the Javascript unedited then just changing line 71 to the name of the grouping field.  If that doesn't work check where you've pasted the code and you've used the correct field name.  If it does work recheck the various URLs ands change them one by one to the marching ones for your own server, retesting each time.  When it stops working that's the one you got wrong.

We found that initially after pasting in the code Sharepoint Designer 2007 crashed when we clicked back in the Display pane.  If this happens to you see if Microsoft Office Diagnostics starts up and if it does let it run and fix your installation.  This happened on 3 different machines so we figure there was some patch or fix that was missing (or possibly it was because we had Sharepoint Designer 2007 installed along side Office 2003 an there was some library mismatch).  If Microsoft Office Diagnostics doesn't start up make sure you're got the latest patchsets for SharePoint Designer 2007.

The question we're looking at now is if we can control the colours of the pie chart segments.  A number of the lists we're looking to have pie chart views on are for things that have colour coded statuses and that's what we want to display (how many red, how many amber and how many green).


This could be really cool and useful.

No comments:

Post a Comment