Friday, September 16, 2005

CSS-based sparklines v1.0

This is my first try to sparklines without images using only CSS/XHTML. The example illustrates an hypothetical medical report for readings of glucose in blod.

The glucose of patien X is normal. Please, check a sparkline with the last 50 readings and current levels



































































Glu 80.2
                 . Normal values of glucose in blod are within the dashed red and green lines.

The sparkline contains five types of blocks (or divs). One for regular points, one for maximal point, one for minial point, one for current point, and finally one for the normal levels. The position of a point has a resolution of a pixel and may be positioned by using the CSS position property in conjunction with left and top. See example:

id="point" style="top: 5px; left:47px;"

Style, layout and resolution of the sparkline can be easily changed by modifying the CSS :


#graph {
position: absolute;
display: inline;
margin-top: 2px;
}

#average {
position: absolute;
width: 100%;
border-top: 1px dotted #F79F9F;
border-bottom: 1px dotted #7AD47A;
overflow; hidden;
}

#point {
position: absolute;
width: 1px;
height: 1px;
background: #878B86;
overflow; hidden;
}

#max {
position: absolute;
width: 1px;
height: 1px;
background: #8B0F0B;
overflow; hidden;
}

#min {
position: absolute;
width: 1px;
height: 1px;
background: #008B1A;
overflow; hidden;
}

#current {
position: absolute;
width: 1px;
height: 1px;
background: #091BA5;
overflow; hidden;
}


8 Comments:

Anonymous Eric W. Bachtal said...

Nice work! Interestingly, in the one real-world application of my sparklines service, I actually abandoned my own barlines implementation for styled divs, too, as the server-side call for graphic production was just too heavy.

I considered doing something like what you've done based on the pixel-level curves done by Alessandro Fulciniti:

http://pro.html.it/articoli/id_599/idcat_31/pag_1/pag.html

But couldn't find the time to finish it. Glad to see it in action. Now it just needs a parameterized server-side interface... ;)

Good luck!

4:12 PM  
Blogger marciusmr said...

Hi Eric,

Thanks for your comment. I actually did that mostly for fun. I think your implementation is very good. I just wanted to do it without images ;-)

I am planning (when I get the time) to generate a parametrized server-side. Also, keep using CSS and XHTML to generate all sort of data visualization.

Thanks again!
m

9:57 AM  
Anonymous Eric W. Bachtal said...

Speaking of client-side browser-based data visualization, have you seen Walter Zorn's High Performance JavaScript Vector Graphics Library:

http://www.walterzorn.com/jsgraphics/jsgraphics_e.htm

Really shows how much can be done with HTML, CSS, and JavaScript. Amazing.

3:46 PM  
Blogger marciusmr said...

That sounds like a good library! I may use it in the future for my other projects for data visualization.

THANKS!
m

11:26 PM  
Anonymous Gary said...

Hey marciusmr. Just dropping a line to let you know that I wrote a Python script that will autogenerate the sparkline code that you so graciously shared with all of us.

http://neurobashing.com/monkey/archives/002282.html

3:48 PM  
Blogger marciusmr said...

Hey gary!

Thanks for your code! That is nice!!! I actually had an intention of doing a similar script in PHP but my day job takes also my nights! ;-)

Hope to see more code out there now that you strated the trend!

THANKS!
m

10:27 AM  
Anonymous Anonymous said...

Check out my PHP implementation at http://www.webofdc.com/offsite/sparklines_interval.php. Soon should have up the interval lines as well.

Jeremy

5:37 PM  
Anonymous Anonymous said...

BTW, the problem mentioned on other sites about this code is that it doesn't render correctly in IE. That's because IE forces you to include SOMETHING in each DIV, which this does not. I had the same problem and solved it by inserting a 1x1 transparent spacer in the DIVs.

5:40 PM  

Post a Comment

<< Home