Skip Ribbon Commands
Skip to main content

Laura Rogers @WonderLaura

:

Quick Launch

Laura Rogers @WonderLaura > Posts > Bar Graphs using SharePoint 2010 Data Views
Hi everyone,
My blog now has a new URL!
You can now find me at http://www.wonderlaura.com
July 25
Bar Graphs using SharePoint 2010 Data Views

This post is applicable to SharePoint 2010 Server, SharePoint Foundation, and SharePoint Online in Office 365.

The inspiration for this post comes from an old post from 2008 on MSDN called CSS Style Bar Graphs using Data Views.  I learned a lot from this old post and used it a couple of times back in SharePoint 2007.  When I decided to do the same thing in 2010, I discovered that the same syntax does not work anymore.  So I figured out a new way to do it.

The idea is that you can take a number field in a list that represents percentage, and create a bar graph.  It will show a bar across a cell, with the total width of the table cell being 100%, and the width of a blue bar being the percentage value from a field in your list.  This is what the end result will look like:

ch5[120]

Here are the steps to accomplish this, just based off of the percent complete field on a task list:

  1. Open your task list in SharePoint Designer 2010.
  2. Create a new view, or just open the All Items view, from the list of views on the right.
  3. In your view, put the cursor in the far right column.  In the Table tab of the ribbon, click the Insert Right button, to insert a blank cell to the right.
  4. Put the cursor in the cell (the second cell down, NOT the title row), and on the Insert tab in the ribbon, click the Picture button to insert a picture.  This is the one I used:
    blueline
  5. At the bottom of SharePoint designer, click the Split button, so you can look at the code behind the image.  Click to select the image, and you’ll see that the code behind it is automatically highlighted
    ch5[124]
  6. The width is what we want to change.  In the code, select the current width (the numbers 856), and as soon as you type a { open curly bracket, it will prompt you with the names of all the fields in the list.
    percent
  7. Pick the first @PercentComplete, and then type a }% after it, so it looks like this:
    ch5[126]

That’s it.  Save the page, and hit F12 to quickly preview it in the browser.  Now your bars are all the width of however much percent complete each task is.

Here’s a tricky one, though.  In my current project, I wanted to do the same thing, but I have no number field at all in my list.  I have a set of statuses that are just text, and I want to base this bar chart off of a text word that represents each item’s status.  For example, my statuses are Manager Approval, VP Approval, CEO Approval, Completed.  I want associate a percent with each, so that:

  • Manager Approval = 25%
  • VP Approval = 50%
  • CEO Approval = 75%
  • Completed = 100%

I don’t want to have to create a new column in my list just to put the percent in it, so instead, I wrote some XSLT code.  Yep.  This is how you create some variables in the code, so that the percent width can be based off of them.  This can all just go in the same cell with the bar graph.

<td class="ms-vb">
<xsl:variable name="stage">
<xsl:choose>
<xsl:when test="normalize-space(@Status) = 'Manager Approval'"><xsl:value-of select="25"/></xsl:when>
<xsl:when test="normalize-space(@Status) = 'VP Approval'"><xsl:value-of select="50"/></xsl:when>
<xsl:when test="normalize-space(@Status) = 'CEO Approval'"><xsl:value-of select="75"/></xsl:when>
<xsl:when test="normalize-space(@Status) = 'Completed'"><xsl:value-of select="100"/></xsl:when>


<xsl:otherwise>
<xsl:variable name="stage" select="100"/>
</xsl:otherwise>
</xsl:choose></xsl:variable>
<img alt="line" src="../Pictures/blueline.png" width="{$stage}%" height="43" />
</td>

 

Now, based on just some text in a text field called Status, your bar graph will show the proper widths accordingly.

 

 

Comments

Courtney Dulany

This technique is so simple and yet borderline genius!  I wish I had come up with it.  :-D
Shane YoungNo presence information on 7/26/2011 9:47 AM

Tecki

Thanks Laura for Wonderful Article..
Shane YoungNo presence information on 7/27/2011 12:17 PM

Mark

Hi Laura - I added the new cell to the right on my list and was able to add the image. However, there was no default width or height. I was able to add a value for height but when trying to do so for width, the value for "%PercentComplete" was not present. Nor were the other list field values (i.e. Priority, Title…etc). Is there some other config that I have to do? Thanks, Mark - 
Shane YoungNo presence information on 8/9/2011 1:19 PM

Amy Gibbs

I don't see a Percent Complete either - I can fumble around through the XPath Expression Builder to get one - but the bar doesn't display right.  I'm using Enterprise edition, does that make a difference?
Shane YoungNo presence information on 8/19/2011 1:56 PM

Laura

I just happened to use a sharepoint task list in this demo, which happens to have a field called percent complete.  This solution can be used in any type of list or library, though, with any number field at all. 
Laura RogersNo presence information on 8/19/2011 4:24 PM

Another Amy

Hi Laura - I am having the same problem as Mark and Amy Gibbs above.  I add the column and insert the image, but there's no default width or height.  When I add a width and height, "%PercentComplete" is not an option.  Thoughts?
Shane YoungNo presence information on 9/7/2011 6:23 PM

Laura

I don't understand you guys' questions about PercentComplete.  Your list has to have a column called Percent Complete, and if it doesn't, then you won't see that as an option.
Laura RogersNo presence information on 9/21/2011 9:35 PM

Jon

I also didn't have the @PercentComplete as an option from the popup after typing "{", although I was to go into the XPath Expression Builder and select that formula manually.  Unfortunately that didn't work for me either. I was left with a blue sliver toward the absolute right of the row no matter what the % complete of the item was.  Here is what did work: If you replace the width="{PercentComplete}%" with width="{$thisNode/@*[name()=current()/@Name]}%" you should be all set.  Also, I had a hard time inserting the blue bar image properly in the first step; it should look like Laura's post where the numerical value for the % is still visible and the blue bar is directly to the right.  I hope this helps.
Shane YoungNo presence information on 10/14/2011 7:40 AM

John

I have the same issue, the @ shows only some fields and not all i also tried $thisnode and didn't work. please advice
Shane YoungNo presence information on 10/30/2011 7:06 AM

Laura

Jon, Cool, thanks for the further troubleshooting.  I can't get mine to not work correctly, so I can't see what some people are seeing as far as it not working.
Laura RogersNo presence information on 11/11/2011 7:52 PM

Carolyn

I think I've discovered why the %Complete field isn't available for selection.  Make sure that when you insert the image, your cursor is in the first DATA row cell, NOT the Title bar cell. I think you'll find you get a different selection of fields depending on which cell the image is placed in.  Even after that, the width/height parameters did not appear for me. I had to add them. And when I neglected to add the height parameter it seemed to take on the size percentage of the width.  I added it with a fixed size and everything worked fine.    Thanks Laura!
Shane YoungNo presence information on 11/14/2011 1:43 PM

Laura

Carolyn, THANKS, I've added that tidbit to the original post, so hopefully no one else will use the incorrect cell.
Laura RogersNo presence information on 11/14/2011 11:28 PM

Haitham Rabie

 Laura Rogers   you are Genius :D keep it up .
 on 2/28/2012 7:59 AM

Cynthia

I am trying to add the bar chart to a Status List.  I successfully added it in SharePoint Designer but when I view the page in the Browser, the bar chart does not show.  I tried both editing the default list view aspx page and creating a new view but neither method works.  Any suggestions?
 on 6/11/2012 1:17 PM

Laura

Cynthia,
Status lists are stuctured really weird, and behave differently than all other types of lists... so I really don't have an answer for that one.
lauraNo presence information on 6/13/2012 11:04 PM
​I have disabled any new comments on this, because my blog has moved to a new site.  Please go here: http://www.wonderlaura.com
RSS FeedFacebookYouTube
 

 Our Books for End Users