Sparklines are small, data-dense charts popularized and named by Edward Tufte. The charts are useful for quickly conveying directional trends or frequencies of positive and negative events rather than specific values.
Sparklines are typically compact enough to be displayed inline with text.
They are also useful when presented in groups to compare two or more data sets:
Sparklines are easily created with d3. There’s nothing really special about them compared to other d3 charts except sizing and some concerns about resolution.
Simple bar chart
First we’ll create a simple bar chart:
Line 10 The width for bars. Leave room for 1 pixel of padding between data points.
Line 12 Create a random data array with values in the domain [0, 1].
Lines 13-14 Create the x and y scales. The x scale domain corresponds to indexes in the data set; the y scale domain corresponds to the extent of values in the data set.
Line 16 Attach the main SVG element to an existing #sparklines element in the DOM.
Bar chart with negative values
Creating a bar chart with positive and negative values is slightly more involved:
Lines 9-10 This time we’ll create data in the domain [-50, 50].
Line 16 Set the y domain to match our data.
Line 28 Set the y value to the top of the bar.
Line 30 Set the height to the absolute value of the data point.
Line 31 Set the color according to whether a positive or negative value.
Lastly we have a line chart:
Again a straightforward d3 implementation, this time using a line generator as seen on lines 25-27. The circles are a little fluff to help frame the chart but could probably be left out.