NASDAQ: Loading.. 1322.85 (close 1/26-3/2)

DOW: Loading.. 6763.29 (close 1/27-3/2)

SF Giants: Loading.. Last game 9/28

jQuery Sparklines

About

25 Feb 2009
Version 1.4 Released

This jQuery plugin generates sparklines (small inline charts) directly in the browser using data supplied either inline in the HTML, or via javascript.

The plugin is compatible with most modern browsers and has been tested with Firefox 2+, Safari 3+, Opera 9 and Internet Explorer 6 & 7.

Each example displayed below takes just 1 line of HTML or javascript to generate.

The plugin was written by Gareth Watts for Splunk Inc and released under the New BSD License.

Examples

Pie charts 1,1,2 1,5 20,50,80

Bullet charts
10,12,12,9,7
14,12,12,9,7
10,12,14,9,7

Try It Out

Loading..

  • Chart type:
  • Values:
  • Values:
  • Values:
  • Values:
  • Values:
  • Values:
  • Values:
  • Height:
  • Width:
  • Line Colour:
  • Fill Colour:
  • Spot Colour:
  • Spot Size:
  • Bar Color:
  • Negative Bar Color:
  • Zero Color:
  • Bar Width:
  • Bar Spacing:
  • Positive Bar Color:
  • Negative Bar Color:
  • Zero Bar Color:
  • Bar Width:
  • Bar Spacing:
  • Line Height:
  • Threshold Value:
  • Threshold Color:
  • Target Color:
  • Target Width:
  • Performance Color:
  • Range Colors:
  • Slice Colors:
  • Box Line Color:
  • Box Fill Color:
  • Whisker Color:
  • Outlier Line Color:
  • Outlier Fill Color:
  • Median Color:
  • Show Outliers:
  • Outlier IQR:
  • Target:
  • Target Color:

Mouse speed Loading..

Inline 10,8,9,3,5,8,5 line graphs 8,4,0,0,0,0,1,4,4,10,10,10,10,0,0,0,4,6,5,9,10

Bar charts 10,8,9,3,5,8,5 negative values: -3,1,2,0,3,-1

Composite inline 8,4,0,0,0,0,1,4,4,10,10,10,10,0,0,0,4,6,5,9,10

Inline with normal range 8,4,0,0,0,0,1,4,4,10,10,10,10,0,0,0,4,6,5,9,10

Composite bar 4,6,7,7,4,3,2,1,4

Discrete 4,6,7,7,4,3,2,1,4,4,5,6,7,6,6,2,4,5
Discrete with threshold 4,6,7,7,4,3,2,1,4

Customize size and colours 10,8,9,3,5,8,5,7

Tristate charts 1,1,0,1,-1,-1,1,-1,0,0,1,1
(think games won, lost or drawn)

Tristate chart using a colour map: 1,2,0,2,-1,-2,1,-2,0,0,1,1

Box Plot: 4,27,34,52,54,59,61,68,78,82,85,87,91,93,100
Pre-computed box plot Loading..

News

25 February 2009 - Version 1.4 Released
This version adds box plots 4,27,34,52,54,59,61,68,78,82,85,87,91,93,100 suggested by Daniel Kadosh. What's a box plot?.

This release also includes a few bug fixes including one where lines would not always appear crisp in canvas based browsers such as Firefox.

Also at this time IE8 RC1 is out.. It looks like sparklines work fairly well in this release, although the small circles used in line charts, box plots, etc. don't show up.


25 January 2009 - Version 1.3 Released
This version fixes some positioning issues on all platforms and adds some support for Internet Explorer 8. IE 8 beta 2 (the latest beta release) seems to have some VML bugs though that I haven't been able to work around yet, so the results aren't perfect.

This version also adds a new function called $.sparkline_display_visible() - Call this anytime sparklines that were drawn into a non-displayed element may have just become visible so that they will be correctly rendered. For example, this site uses the function to make sure sparklines displayed in the various tabbed pages are drawn when the tab is selected.

Finally I updated the site and docs somewhat as the old layout was looking a little cramped.


21 November 2008 - Version 1.2.1 Released
Fixes a couple of pie chart bugs; no new features.


19 November 2008 - Version 1.2 Released
Some important bug fixes amongst some basic new features:

See the changelog for the complete list of changes.


14 August 2008 - Using Sparklines with Splunk
I gave a quick 5 minute demo at the Splunk Developers Boot Camp on using the Splunk PHP SDK with jquery.sparkline.js to chart the traffic this page received within the first few days of its launch.

Check out the short write up i posted to the new Splunk Wiki for the code and data I used to make it work (it was really easy) and if you download your own free copy of Splunk you can try it yourself ;-)


29 July 2008 - Version 1.1 Released
Thanks for the suggestions for improvements - This version fixes several bugs and adds some more features and chart types including

See the changelog for the complete list of changes.


17 July 2008 - Version 1.0 Released
Initial version released

Documentation

Quick Start

To add some sparklines to your web page you need four things:

  1. The jQuery javascript library loaded into the page - At least version 1.2 or higher
  2. A copy of jquery.sparkline.js loaded into the page which you can download from this site
  3. An inline tag on the page within which to display the sparkline (eg. <span>)
  4. A call to the sparkline() function to actually display the sparkline.

Here's a simple web page that will display a couple of sparklines:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>

    <script type="text/javascript" src="jquery-1.2.6.js"></script>
    <script type="text/javascript" src="jquery.sparkline.js"></script>

    <script type="text/javascript">
    $(function() {
        $('.inlinesparkline').sparkline(); 
        $('.dynamicsparkline').sparkline([10,8,5,7,4,4,1]); 
    });
    </script>
</head>
<body>

<p>
Inline Sparkline: <span class="inlinesparkline">1,4,4,7,5,9,10</span>.
</p>
<p>
Sparkline with dynamic data: <span class="dynamicsparkline">Loading..</span>
</p>

</body>
</html>
        

Click here to open this example in a new window.

As the example shows, the values to be used in a sparkline can either be supplied inline, inside the tag to be used as the target or can be passed as the first parameter to the sparkline() function.

To draw different types of charts, or overlay one on top of another, or display charts into hidden layers, read on.

Syntax

$(selector).sparkline(values, options);
        

Values can either be an array of numbers or "html" which causes the values to be read from from the selected tag:

<span class="sparklines">1,2,3,4,5,4,3,2,1</span>
<span id="ticker"">Loading..</span>

$('.sparklines').sparkline('html');
$('#ticker').sparkline([1,2,3,4,5,4,3,2,1]);
        

Options is an object that specifies the type of sparkline to draw, colours to use, etc

$('#barchart').sparkline(myvalues, { type:'bar', barColor:'green' });
        

Common Options

Hidden Sparklines

If you try to display a sparkline in a tag that's currently not visible (ie. the tag or one of its parents are set to display:none) then you'll find that the sparkline hasn't been rendered when you finally make the tag visisble. This is because a hidden tag has no size so we can't detect the area to draw on.

The solution is to call the $.sparkline_display_visible() function anytime a sparkline may have become visible so that it can be correctly rendered. This is the technique this site uses to handle the sparklines that are hidden in the different tabbed sections.

Line Chart Options

8,4,0,0,0,0,1,4,4,10,10,10,10,0,0,0,4,6,5,9,10

Line charts are the default chart type, but to specify the type explicitly set an option called "type" to "line".

By default the values supplied to line charts are presumed to be y values mapping on to sequential (integer) x values. If you need to specify both the x and y values for your chart, you have a few options:

  1. Inline: x and y values are separated by a colon: x:y,x:y,x:y - eg. <span class="linechart">1:3,2.7:4,4.8:3</span>
  2. Array of arrays: An array of [x,y] arrays: $('#linechart').sparkline([ [1,3], [2.7,4], [4.8,3] ]);
  3. Separate arrays: Pass x values separately: $('#linechart').sparkline([3,4,3], { xvalues: [1,2.7,4.8]});

Bar Chart Options

10,8,9,3,5,8,5
-3,1,2,0,3,-1

Set the "type" option to "bar" to generate bar charts.

Tristate Chart Options

1,1,0,1,-1,-1,1,-1,0,0,1,1

Set the "type" option to "tristate" to generate tristate charts.

Discrete Chart Options

4,6,7,7,4,3,2,1,4,4,5,6,7,6,6,2,4,5

Set the "type" option to "discrete" to generate discrete charts.

Bullet Graph Options

10,12,12,9,7

Set the "type" option to "bullet" to generate bullet graphs.

See the wikipedia page for more information on Bullet graphs.
Supplied values must be in this order: target, performance, range1, range2, range3, ...

Pie Chart Options

1,1,2
1,5
20,50,80

Set the "type" option to "pie" to generate pie charts.

These little pie charts tend only to be useful with 2 or 3 values at most

Box Plot Options

4,27,34,52,54,59,61,68,78,82,85,87,91,93,100
Loading..

See the wikipedia page for more information on Box plots

Set the "type" option to "box" to generate box plots.

As noted in the options above, by default "raw" is set to false. This means that you can just pass an arbitrarily long list of values to the sparkline function and the corresponding box plot will be calculated from those values. This is probably the behaviour you want most of the time.

If, on the other hand, you have thousands of values to deal with you may want to pre-compute the points needed for the box plot. In that case, set raw=true and pass in the computed values. If showing outliers, supplied values of:
low_outlier, low_whisker, q1, median, q3, high_whisker, high_outlier
Omit the outliers and set showOutliers to false to omit outlier display.

License

This plugin is copyright Splunk Inc and licensed using the New BSD License

Download

Note: jquery.sparkline.js is a plugin for jQuery - You need to include both on your page to generate sparklines.

Version 1.4:

Version 1.3:

Version 1.2.1:

Who's Using It

Are you using jquery.sparkline on your project or site? Send me a quick email and I'll add a link here!

Current users include:

Feedback

Feedback and patches are welcome - Please direct email to Gareth Watts - [email protected]