Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
SPQuery Hacks Part 1: InfoPathUse SHIFT+ENTER to open the menu (new window).
Adventures in Excel Services 2010Use SHIFT+ENTER to open the menu (new window).
What Is The Easiest Way To Mess Up SharePoint? Use SHIFT+ENTER to open the menu (new window).
Redirect SharePoint Navigation - NYC SDUG Quick DipUse SHIFT+ENTER to open the menu (new window).
SPQuery Hacks Part 2: WorkflowsUse SHIFT+ENTER to open the menu (new window).
Upgrading SharePoint 2007 RTM to SharePoint 2010 Use SHIFT+ENTER to open the menu (new window).
Using JQuery to add charts to your Data ViewsUse SHIFT+ENTER to open the menu (new window).
Quick Rundown: Multi-line Text ColumnsUse SHIFT+ENTER to open the menu (new window).
Document Previews Won’t Open In FAST Search Using HTTPSUse SHIFT+ENTER to open the menu (new window).
Sleazy Reporting: SharePoint 2010 ListsUse SHIFT+ENTER to open the menu (new window).
Helpful Debugging with SharePoint C Sharp CodeUse SHIFT+ENTER to open the menu (new window).
Configuring Remote Blob Storage in SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Getting E-mail to Work in Your SharePoint 2010 Dev EnvironmentUse SHIFT+ENTER to open the menu (new window).
Sleazy Reporting: SharePoint 2010 External Content TypesUse SHIFT+ENTER to open the menu (new window).
Programmatically Dealing With Potential Multi-Select ColumnsUse SHIFT+ENTER to open the menu (new window).
Using JQuery to add charts to your Data Views
By: Omar Stewart

While at SPC 09, I attended an excellent session hosted by Dustin Miller on building composite apps using SharePoint Designer 2010. In one portion of the segment he used a JQuery charting library to add some visuals to a plain old Data View Web Part.

I'm going to show you how to quickly do this using SharePoint Designer 2007. This is very useful for quickly jazzing up a data-view, while providing all the benefits of graphing / visualizing your data.

We will go from this....

clip_image002

To This....

clip_image004 In just a few simple steps.

This first thing to do is create our list. This could be a task list, a bugs list, anything you want displayed in your data view.

For this example, I'll be logging our team members' interest in the iPad, based on the amount of web activity related to the iPad ..

clip_image006

Fill in some sample data….

clip_image008

Now we will open SharePoint Designer 2007, open our SharePoint site, and create a new .aspx page.

clip_image010

Now we can start building our data view.

From the Menu Bar click Data View," Insert Data View". This will expose the data source library tab..

clip_image012

I'll now scroll down to the "Gig Werks Team interest in iPad" List, click on the list, and then click "Show Data".

clip_image014

Towards the bottom of the screen, the fields of that list will be visible.

clip_image016

We now need to select the fields we will want in our data view. Hold down CONTROL and click to select the Name, all the other fields we want in our view, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, and FRIDAY.

clip_image018

Now that we have them selected, click the Insert Selected Fields as… button, then click "Multiple Item View" because we want all the users to show in our view.

clip_image020

We should now have a table displaying all the data from our list in our new data view web part. This is however, not the most fascinating thing to look at.

clip_image022

We can make simple changes that will make this much easier to read, and provide for an overall better user experience. With a simple chart, what used to take a user minutes to read, can now be parsed at a quick glance.

The first thing we need to do, is prepare our data view, so it can be parsed by the library we will be leveraging. With the data view selected, Click Data View, Data View Properties, then click the Layout Tab.

clip_image024

clip_image026

Scroll down to select the Comma Separated View Style.

clip_image028

Our User names are now displayed as headings, with our data displayed as comma separated values underneath.

clip_image030

We now need to wrap our comma separated values in a container, so our graphing library knows exactly what data to use to construct its graph

Use your cursor to Highlight a single row of values, making sure you don't select the user name, just the numbers.

clip_image032

Click EDIT >> Quick Tag Editor. We'll wrap the Tag in a SPAN and give it a class of "GraphThis".

clip_image034

<SPAN class="GraphThis">

clip_image036

The row of values should now be wrapped in a span with a class of "GraphThis".

clip_image038

We can now pass this along to JQuery and have it graph our data.

Switch to Code View, and scroll to the <head > section of the page. You can either download or link to the JQuery, and Sparkline libraries. For this example, we'll download them and drop them in to the root of our website.

clip_image040

Lets add the following Script references to our page:

You can download the Jquery Library Here:

http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js

You can download the Sparkline Library Here: http://omnipotent.net/jquery.sparkline/1.5.1/jquery.sparkline.min.js

<Script src="jquery-1.3.2.min.js"></Script>

<Script src="jquery.sparkline.min.js"></Script>

clip_image042

Now that we have our libraries referenced, we can begin to put them to work…

Lets open a new SCRIPT tag and begin our JQuery. We use "$(document).ready()" to tell JQuery we're ready to start querying our document

   1:  
   2: <SCRIPT>
   3: $(document).ready();
   4: </SCRIPT>

clip_image044

Inside the ready method, we'll add our function which is what we want JQuery to execute, once we've told it we're ready to use it.

<SCRIPT>

$(document).ready(

function() {

//Function for JQuery to execute will go here

}

);

</SCRIPT>

For our function, we want to tell JQuery to find everything with a class of "GraphThis" and then have Sparkline turn it into a Graph.

JQuery makes this extremely simple to do. Here's the syntax:

$(".GraphThis").sparkline();

..Thats It!! Our entire script should appear as follows:

   1:  
   2: <SCRIPT>
   3: $(document).ready(
   4: function()
   5: {
   6: //Function for JQuery to execute will go here
   7: $(".GraphThis").sparkline();
   8: }
   9: );
  10: </SCRIPT>

clip_image046

If you save, and preview your page in browser, you should now see a list of user names, along with a small line graph representing the values associated with them.

clip_image048

The Sparkline API makes it very simple to adjust these graphs, so lets make these the graphs a little bigger.

Lets go back to our code and add pass some parameters to Sparkline in order to set the width and height of our graphs.

$(".GraphThis").sparkline('html', {width:'300px', height:'50px'});

Our final script should look like this:

   1:  
   2: <SCRIPT>
   3: $(document).ready(
   4: function()
   5: {
   6: //Function for JQuery to execute will go here
   7: $(".GraphThis").sparkline('html', {width:'300px', height:'50px'});}
   8: );
   9: </SCRIPT>

clip_image050

Save and view your page in browser, you should now have a nice viewable graph to accompany your data.

clip_image052

As a final touch, I'll give the page a title and attach it to my SharePoint site's default master page. I'll also change the data view properties, so an edit button is automatically included in the data view.

clip_image054

Now just by taking a quick look at our data view, it now becomes obvious how extremely interested Kevin is in the iPad.

clip_image056

I hope this has been a useful introduction to JQuery in SharePoint. JQuery is an extremely powerful library for customizing your web sites, SharePoint is no exception.

-Omar Stewart

        

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  SharePoint Resources
  Business Intelligence Resources
  Upcoming Webinars



©2009 Gig Werks. All rights reserved. Privacy Policy