Sparklines Example

This example is inspired by Joe Gregorio's Sparkline Generator Web Application. For more about the concept of sparklines read here.

Contents

The example consists of one web resource implemented by the following:

com.sun.jersey.samples.sparklines.SparklinesResource
This Java class returns the a PNG image of a sparkline calculated for a given set of query parameters.

The example makes use of classes to parse query parameters, a message body writer to write an image in the PNG format, and makes use of entity tags and performs precondition checking to support not modified responses.

The mapping of the URI path space is presented in the following table:

URI path

Resource class

HTTP methods

Description

discrete

SparklinesResource

GET

Plot a sparkline as discrete vertical bars, one per data value.

smooth

SparklinesResource

GET

Plot a sparkline as continuous line.



For either URI path the following query parameters can be declared:

Query parameter

Default Value

Description

d


A comma separated list of integer values that represents the input data to plot as a sparkline.

limits

0,100

The valid range of the input data in “d”.



The “smooth” URI path can have following query parameters:

Query parameter

Default Value

Description

min-m

true

If true then a marker is placed at the smallest value in the input data.

max-m

true

If true then a marker is placed at the largest value in the input data.

last-m

true

If true then a marker is placed at the last value in the input data.

min-color

blue

The colour of the marker placed at the smallest value in the input data

max-color

green

The colour of the marker placed at the largest value in the input data.

last-color

red

The colour of the marker placed at the last value in the input data.

step

2

The horizontal distance, in pixels, between each plot of a data value.



The “discrete” URI path can have following query parameters:

Query parameter

Default Value

Description

width

2

The width, in pixels, of each plot of a virtual bar for each data value.

upper

50

Data values >= upper will be coloured in “upper-color” otherwse data values will be colured in “lower-color”

upper-color

red

The colour for data values >= “upper”

lower-color

gray

The colour for data values < upper



Running the Example

Run the example as follows:

run

mvn compile exec:java

From a web browser, visit:

http://localhost:9998/sparklines/discrete?d=88,84,82,92,82,86,66,82,44,64,66,88,96,80,24,26,14,0,0,26,8,6,6,24,52,66,36,6,10,14,30