After my first attempt at providing some way for people to style graphs in Graphication, which ended up being a rather ugly system with an odd set of nested dictionaries, a thought struck me; we already have a language for specifying presentation, and which has inheritance and other nifty time-saving shortcuts: CSS.
I could only find one python CSS library, cssutils, and while that seemed to have very decent CSS2 support for parsing into a document tree, I couldn’t see any immediate way of using it for retrieving the applicable values for, say, a grid object with class “minor” inside a wavegraph object.
First, I wrote a very lightweight CSS parser and rule matcher. Code examples always show off these things best; first you do something like
css_string = """wavegraph {color: #369; font-size: 12; }
grid.minor { color: #eee; } """
import css
stylesheet = css.CssStylesheet.from_css(css_string)
If you’re feeling like using stylesheets a lot, you can make them external (e.g. a file “default_css.css”) and use the import hook:
import css
css.install_hook()
import graphication.default_css as stylesheet
Then, querying properties is pretty simple:
>>> stylesheet.props("wavegraph")
{"color": "#369", "font-size": "12"}
>>> stylesheet.props("wavegraph").get("color")
"#369"
>>> stylesheet.props("wavegraph").get_int("font-size")
12
>>> stylesheet.props("wavegraph grid.minor line")
{"color": "#eee", "font-size": "12"}
This means all the styling crud previously used can be replaced with these simple css-selector-ish queries, and different graph styles can simply ship as different css files.
So, hopefully, graph styling will be a lot more accessible once I roll this fully into the graph system, as well as a lot nicer to deal with for most uses. In the meantime, if you want to look at this CSS parser code, have a look at the current subversion copy.
The start of the LastGraph rewrite is well underway; the backend has been moved to Django (probably out of familiarity, since I’ve been using it quite a bit recently), and is already fetching week lists and track lists, much to my delight.
This time around, I’m grabbing the weekly track charts, not the artist charts, and decoding and storing all of the data in a database, which is then queried to get artist statistics. This way, I’ll have all that data I bothered fetching around for future use, such as the Flash-based graph viewer I am contemplating (it does exist, but has only got as far as rendering predefined graphs…).
In more interesting (visual) news, the new rendering backend has been somewhat spun off into its own project, called ‘Graphication’. It’s built on top of Cairo, and the idea is to eventually provide a nice API for snazzy graph creation (LastGraph will have a few more graph types in future, which is one of the reasons for doing this. Radial graphs of genres/artists/tracks, anyone?).

It already has a working wavegraph rendering module, an example output of which is above (the data is randomly generated). I’m now going to move on to the text-on-curves part, which is quite simple to do, but I’m going to try and find a way of making it less brute-force-ish, in the hope it might run a little faster.
In the vain hope of making this slightly more blog-like, you people get a dose of my daily life for a change.
This morning I was quick enough to sign up to BarCamp Brighton, which sounds like great fun, and also happens to be on reasonable dates. I am somewhat filled with anticipation, but also a slight trepidation, since I feel I should make the effort to give a proper talk and I find myself somewhat lacking in ideas for them at the moment.
It’s almost inevitable that I’ll end up talking about LastGraph, especially since I’m redoing both the interface (to be somewhat nicer and more efficient), as well as changing the renderer (the new version uses Cairo, so it’s possible to do direct-to-pdf output now, rather than the inkscape hack).
Even more interesting perhaps is that I’m making the graphing library nice and modular and will probably add more graph types to both it and the LastGraph interface (I think a nice radial chart or two would look nice next). I’ll release it at some point, and hopefully people can start using less bar charts in future when there’s far nicer ways of presenting the data (although, in some cases, bar charts still do very well).