Skip navigation.

I'll think of a good title later

Attempts at charting with XML,XSL and SVG

, , , , , ,

Warning: boring, rambly, techy, geeky post with a remarkably high TLA density.

Edit: thanks to a lead from Jeff I've fixed the mime-type issue for FireFox, the chart is now viewable direct from the server (although positioning is still out). Still broken in Opera though :frown:

My pet project for the past few days has I have to say ended in disappointment.

The idea was simple: Take an XML document, transform it using XSL into an XHTML document that contains a block (or blocks) of SVG that calls a javascript function which further transforms the SVG via the DOM with values calculated using the Math object. Then we bounce the signal back into the aerosphere up to COMSAT 6, beam it back to SATCOM 2 transmitter number 137 and down on the dish on the back of Mr. Big's limo... It's almost too easy.

Well, it started off simple then mutated into the above. In more practical terms I wanted to take name+value pairs from an xml file & use XSL to create a pie chart drawn using SVG.

This guy did it back in 2003 but that solution uses java to calculate the coordinates for each slice which I thought could be done just as easily in javascript & I don't really like the look of the resulting chart. I wanted a much sharper diagram with proper labels. There are a number of other attempts at using svg as a charting tool on the web dating around 2003-2005 but they all seem to go so far, then stop. I found this odd, XML & SVG are ideally suited for the task surely?

You can take a look at my results here. What? you can't see anything? Well I did say I was disappointed, but I think I have found out why XML+SVG charting has only gone so far - browser inconsistancies.

Opera handles the whole thing fine when it is hosted on your local machine where the XML can be served as text/xml. Grab the xml, xsl, css & js files & save them all in the same directory (right click & save as to ensure you get the correct filenames - Opera & other browsers will change the .xsl extension to .xml (another bug?)). Load the xml file in Opera to behold my masterpiece. Load the XML file in your text editor of choice & change the values, add new data, change the mergevaluesunder value, save & refresh. The xsl & js files will sort, calculate & draw all the segments & reposition all the labels with reasonable intelligence. It also supports multiple pie charts one after the other by having multiple <bands> (although if you scroll down then refresh the page Opera draws the SVG & XHTML over the top of each other (scroll up & refresh to magically fix it), I'll try & build a simpler test case for a bug report later in the week).

Sadly though when the XML is served as application/xml (as it is from the MyOpera servers) it doesn't work & at the moment seems to be a likely candidate for another bug report.

Even on a local machine Firefox & IE throw back XSL processing errors because they can't handle XHTML as an output type.

Changing:

<xsl:output method="xhtml"/>


to:

<xsl:output method="html"/>


in the XSL file sort of solves the problem in that FireFox now displays the chart albeit with the positioning borked to the left somewhat (Edit: in FireFox getComputedTextLength() always returns 0). IE stops returning the XSLT processing error & returns the HTML but not the SVG. It unfortunately also breaks Opera which (correctly) won't display SVG inline inside an HTML document (because HTML isn't actually XML), the SVG Wiki & the Mozilla developer pages say that Moz should behave this way too, but evidently it doesn't.

Those in the know will be able to pick up from the source of the files that I was intending to add more templates for different & more useful charts. But I suppose XML, XSL & SVG are still in their infancy & implementation inconsistancies are only to be expected. Still, I've learnt a lot & I think there are some useful bits of code that others might find interesting which is why I throw it out into the interweb.

Looks like this is something I'll come back to.

I've been taggedAnother SVG map, how to cut your Inkscape files down & some more SVG links

Comments

stelt 29. March 2007, 15:37

Take a look at http://svg.startpagina.nl for many SVG (charting) examples and tools

jeff_schiller 29. March 2007, 17:35

Here's mine, orinoco: http://www.codedread.com/displayWebStats.php

I take my data as a bunch of raw XML files (generated by my site), parse them via PHP into JSON and then use JavaScript to turn that into an interactive SVG graph. Try grabbing those red/green grippies around ... Drag that legend out of the way ... use the arrow keys or click on the time graph. WARNING: There's a lot of data there now though (probably 600+ data points), so it's pretty slow in every browser I try it in (time for browsers to step it up, or for me to trim my data).

I haven't gone so far as to try XSLT but Rob did some back in the day: http://www.latenightpc.com/blog/archives/2004/12/16/xml-to-svg-with-xslt-part-i/

orinoco 30. March 2007, 19:12

Hi Stelt,

yep http://svg.startpagina.nl is already on my speed dial & has provided an enormous amount of help so far.

The thing is most charting examples rely on Python, Java, PHP, ruby etc. I wanted to see if I could come up with something truly standalone. It is much easier for newbies (me included!) to create an XML file & include a reference to a XSL stylesheet than it is for them to install whatever programming language is required (for those of us casual webmasters using free servers we don't have the choice to run any web programming languages even if we did have the expertise).

& the link to create a graph (on-line tool) is a Flash app!!!

Hi Jeff,

I agree that that chart is a thing of beauty, but it's not very easy to read. From a design point of view the colours are hard to distinguish & upside-down text is never a good idea when the text needs to be read.

I found the URL to this post:

http://ecmanaut.blogspot.com/2006/01/svg-challenge-taken.html

& I prefer your first effort before Johan got hold of it :smile:

I really like the way you can use the grippies to alter the timeframe of the line graph, I think that is extremely impressive! I hadn't really thought about any interactivity other than including a <URL> tag in the XML & using it to create drill down functionality. SVG offers a lot of possibilities on that front.

I also think that last link may contain a solution to one of my problems which I will investigate when I have the time.

Thanks for the inspiration!

jeff_schiller 30. March 2007, 22:06

orinico, thanks for the compliments. I like that grippy/scroll-bar thing too - btw, the blue solid line between the grippies is a scrollbar, so you shrink the timescale, then use the blue bar as a scrollbar too.

Anyway, it's too bad that the original blog page that had these raster graphics is now gone - the fatal flaw of the internet. But I (like Johan) really thought they were impressive and easy to read originally. I agree that my SVG versions are crappier - but I blame that on the font rendering in SVG viewers at the moment, hopefully that will improve...

stelt 17. July 2007, 04:25

orinoco, i turned off the link to the Flash thing (that could only save the end result as SVG) after i sent the people behind it some suggestions to improve their service, in order to have the link turned back on.

krolaw 12. October 2008, 10:09

Hi, I'm not sure if this is still of interest, but I've been working on www.xsltcharts.com

which is able to generate svg pie, bar, barstack, line graphs etc using only xslt 1.0, without the use of js, java or other extensions etc. I'm pretty new to all this stuff, was hoping for some kind of business model for the website, alas, it's just a bit of fun at the moment.

Thoughts?

Write a comment

You must be logged in to write a comment. If you're not a registered member, please sign up.

December 2009
M T W T F S S
November 2009January 2010
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31