Feature
Post

Category
Code

XML Data Driven CSS Charts

XML Data Driven CSS Charts? Whaaa!!!

No its not crazy talk. CSS Charts are possible nowadays and they can even be semantically correct. Let's say you already have started using CSS based charts on your site. Wouldn't it be nice if the CSS charts were update able via an XML file. You are in the right place! This technique is going to show you how to make accessible charts that are scalable and easy to update.

The Basic Structure Markup

Below you will find the basic structure for the chart. I used a definition list since it does a great job of showing relation between values. You could also use list items, but I didn't like the way it looked semantically.

HTML:
  1.     <dd>Chart Title</dd>
  2.       <dd>
  3.       <dl>
  4.             <dd><strong>Unit 1</strong><span><em>95&#37;</em></span></dd>
  5.             <dd><strong>Unit 2</strong><span><em>75&#37;</em></span></dd>
  6.             <dd><strong>Unit 3</strong><span><em>50&#37;</em></span><dd>
  7.             <dd><strong>Unit 4</strong><span><em>25&#37;</em></span></dd>
  8.             <dd><strong>Unit 5</strong><span><em>10&#37;</em></span></dd>
  9.             <dd><strong>Unit 6</strong><span><em>35&#37;</em></span></dd>
  10.       </dl>
  11.       </dd>
  12.     <dd>Various Units</dd>
  13. </dl>

Style that Chart!

I am not going to go into a lot of detail about each part of the CSS. The basic structure is pretty straight forward and doesn't require you be a brain surgeon. Please see the CSS file located in the source zip at the end of the post. I am however going to talk about some of the CSS that controls the height and color.

Heights

The height of the bars in this tutorial are controlled by CSS so it is required that you create classes for the various heights so the XML parser can associate. I create the heights in 5% increments, you can changed to whatever you want.

Here is a snippet of the heights:

CSS:
  1. dl.xmlGraph.vertical dd dl dd span.h5 { height:5%; }
  2. dl.xmlGraph.vertical dd dl dd span.h95 { height:95%; }
  3. dl.xmlGraph.vertical dd dl dd span.h100 { height:100%; }

Colors

We are going to create a few generic colors that we will offer as options for the bar colors.

CSS:
  1. .green { background-color:#009900; }
  2. .lghtGreen { background-color:#8fad53; }
  3. .lghtOrange { background-color:#CC9900; }

The Data Structure

Now lets get into the meat of this technique. The data! As you already know the data is going to be stored in an XML file. Below is the generic structure.

HTML:
  1. <graphData>
  2.       <graph>
  3.             <params>
  4.                    <title>Chart Title</title>
  5.                    <xAxis>Various Units</xAxis>
  6.                    <yAxis>Complete</yAxis>
  7.             </params>
  8.             <values>
  9.                   <set name=Unit 1 value=95 color=green />
  10.                   <set name=Unit 2 value=75 color=lghtGreen />
  11.                   <set name=Unit 3 value=50 color=lghtBlue />
  12.                   <set name=Unit 4 value=25 color=blue/>
  13.                   <set name=Unit 5 value=10 color=purple/>
  14.                   <set name=Unit 6 value=35 color=lghtGrey/>
  15.             </values>
  16.       </graph>
  17. </graphData>

The Data Parser

This is where the technique really starts to come together. We now start utilizing both the XML file and CSS selectors to achieve our goal...data driven charts! Below is a snippet of the parser so you can get an idea of what were trying to do and get the basic jist of things. The full files will be available at the end of this article so you can dissect.

CODE:
  1. &lt;%
  2.  
  3.       If graphTitle  "" Then
  4.       stringBuffer = "<dt>Chart Title</dt>"
  5.       stringBuffer = stringBuffer &amp; "<dd>" &amp; vbCrLf
  6.       If graphTitle  "" Then stringBuffer = stringBuffer &amp; "<h3>" &amp; graphTitle &amp; "</h3>" &amp; vbCrLf
  7.       stringBuffer = stringBuffer &amp; "</dd>" &amp; vbCrLf
  8.       End If
  9.       stringBuffer = stringBuffer &amp; "<dd>" &amp; vbCrLf &amp; "<dl>" &amp; vbCrLf
  10.       stringBuffer = stringBuffer &amp; "<dd></dd>" &amp; vbCrLf
  11.  
  12.       Dim params
  13.       Set params = graphXML.selectNodes("//params")
  14.       Set sets = graphXML.selectNodes("//set")
  15.  
  16.       x = 0
  17.  
  18.       For each valset in sets
  19.       If x = 0 Then
  20.             classAttribute = " class=""first"""
  21.       ElseIf x = (sets.length - 1) Then
  22.             classAttribute = " class=""last"""
  23.       Else
  24.             classAttribute = ""
  25.       End If
  26.  
  27.       x = x + 1
  28.  
  29.       Set attribs = valset.attributes
  30.             setName = ""
  31.             setValue = ""
  32.             setColor = ""
  33.       For each attrib in attribs
  34.             If attrib.name = "name" Then setName = attrib.text
  35.             If attrib.name = "value" Then setValue = attrib.text
  36.             If attrib.name = "color" Then setColor = attrib.text
  37.       Next
  38.       stringBuffer = stringBuffer &amp; "<dd>" &amp; vbCrLf
  39.  
  40.       If setName  ""
  41.       Then stringBuffer = stringBuffer &amp; vbTab &amp; "<strong>" &amp; setName &amp; "</strong>" &amp; vbCrLf
  42.  
  43.       If setValue  ""
  44.       Then stringBuffer = stringBuffer &amp; vbTab &amp; "<span><em>" &amp; setValue &amp; "&#37;</em></span>" &amp; vbCrLf
  45.       stringBuffer = stringBuffer &amp; "</dd>" &amp; vbCrLf
  46.       onFirst = false
  47.  
  48.       Next
  49.       stringBuffer = stringBuffer &amp; "</dl>" &amp; vbCrLf &amp; "</dd>" &amp; vbCrLf
  50.       response.Write(stringBuffer) 
  51.  
  52.    %&gt;

Keep in mind that it's always a good idea to have error handling in place to handle any kind of exception that can occur. I didn't include in code snippet due to space constraints, plus I didn't want to bore you guys.

The Final Product

This is how you would structure your ASP page to finally bring all the elements together. Just by calling the ASP file in as a file include then reference the below code in the body.

HTML:
  1. <!-- #include file="xmlChartParser.asp" -->

HTML:
  1. <% Call CreateGraph("xmlData.xml ") %>

Below I created an example page utilizing the techniques discussed in this article. I have also attached the source file so you can rummage through the code. I am always open to new methods...drop me line and well discuss.

Happy charting!!!

DEMO: view page

Source Files: download

  1. By jeffrey posted on October 23, 2008 at 4:06 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Great article thanks for the insight!!!

  2. By Karen Ruiz posted on October 23, 2008 at 4:12 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Thank you Mr.Palacios for your very informative info on CSS charts. You were right to express how we feel about them WHAAA!!! I followed your advice and technique and am on my way to successful charting. I hope we can continue to see articles from you in other areas.
    Thanks again,
    Karen

  3. By Barack Obama posted on October 27, 2008 at 8:43 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Great article, I am updating my charts as we speak… It is time for a change and with these changes I will rule the world!

  4. TrackbackUseful Links (24/10/2008) | ApramanaCSS ile Tablo oluşturmanın 8 yolu GB | Gencbilgin.com - Bilgiyi Eğlendirerek Öğreten Alem…

    Your words are your own, so be nice and helpful if you can. If this is the first time you're posting a comment, it might go into moderation. Don't worry, it's not lost, so there's no need to repost it! We accept clean XHTML in comments, but don't overdo it please.