Sparklines for Java

Introduction

Sparklines are "intense, simple, wordlike graphics." They are detailed in Edward Tufte's latest book, Beautiful Evidence and also in his message board.

Here's one showing my unique visitor history on my Javadoc search site for the past several days , or maybe you prefer a line graph: .

There's a PHP library for producing sparklines, and even a sparkline web service, but I wanted something I could use to generate sparklines in both Java and JSTL (and I thought it would be a fun project). (It's also worth checking out Why's minimalist python sparklines, which encode the image data directly in the HTML as a data: URI [this library allows you to encode the images as using either data: format or javascript: format in JSTL])

JSTL examples

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib prefix="spark" uri="http://www.representqueens.com/taglibs/spark" %>
    <c:set var="test" value="5, 35, 22, 19, 16, 10, 4, 33, 9, 12, 27, 36, 22" />
    <img src="<spark:bar name="test" color="red" />" />
  

will produce:

(I think the default size might be a little large).

The "name" attribute specifies the key to find the data (Uses JspContext.findAttribute). It expects the value to be either a List<Number>, a Number[], or a String of comma separated int values. It's the only manditory attribute.

The other attributes are width, height, spacing, color, highColor, lastColor, output. You can replace "bar" with "line" to get a linegraph (note: highColor and lastColor are currently not supported for line graphs).

The color attributes can either be a color name (a field name from java.awt.Color), or a HTML hex color (e.g. #ff0000 for red).

Java Examples

final Number[] data = new Integer[] { 5, 22, 16, 8, 30, 9, 12, 27, 19, 22 };

// width, height, spacing
final SizeParams params = new SizeParams(50, 12, 1);

final BufferedImage i =
  BarGraph.createGraph(data, params,
                       Color.blue.brighter(),
                       Color.blue.darker(),
                       Color.magenta);

ImageIO.write(i, "png", new File(args[0]));

Would produce something like:

Line graphs are similar, although we currently only support a single color:

final BufferedImage i = LineGraph.createGraph(data, params, Color.blue.darker());

produces:

Download

Binary package – spark.jar (initial revision, last updated 7/10/06).

Source package – spark-1.0.tar.gz (initial revision, last updated 7/17/06). Released under the Apache License.

Coming soon

more docs, javadoc, jstl docs, etc.

Contact

You can reach me at spark [at] representqueens [dot] com for any comments, suggestions, bugs, etc.