View Source Contex.Sparkline (ContEx v0.5.0)

Generates a simple sparkline from an array of numbers.

Note that this does not follow the pattern for other types of plot. It is not designed to be embedded within a Contex.Plot and, because it only relies on a single list of numbers, does not use data wrapped in a Contex.Dataset.

Usage is exceptionally simple:

  data = [0, 5, 10, 15, 12, 12, 15, 14, 20, 14, 10, 15, 15]
  Sparkline.new(data) |> Sparkline.draw() # Emits svg sparkline

The colour defaults to a green line with a faded green fill, but can be overridden with colours/3. Unlike other colours in Contex, these colours are how you would specify them in CSS - e.g.

  Sparkline.new(data)
  |> Sparkline.colours("#fad48e", "#ff9838")
  |> Sparkline.draw()

The size defaults to 20 pixels high and 100 wide. You can override by updating :height and :width directly in the Sparkline struct before call draw/1.

Link to this section Summary

Functions

Override line and fill colours for the sparkline.

Renders the sparkline to svg, including the svg wrapper, as a string or improper string list that is marked safe.

Create a new sparkline struct from some data.

Link to this section Types

@type t() :: %Contex.Sparkline{
  data: term(),
  extents: term(),
  fill_colour: term(),
  height: term(),
  length: term(),
  line_colour: term(),
  line_width: term(),
  spot_colour: term(),
  spot_radius: term(),
  width: term(),
  y_transform: term()
}

Link to this section Functions

Link to this function

colours(sparkline, fill, line)

View Source
@spec colours(t(), String.t(), String.t()) :: t()

Override line and fill colours for the sparkline.

Note that colours should be specified as you would in CSS - they are passed through directly into the SVG. For example:

  Sparkline.new(data)
  |> Sparkline.colours("#fad48e", "#ff9838")
  |> Sparkline.draw()

Renders the sparkline to svg, including the svg wrapper, as a string or improper string list that is marked safe.

@spec new([number()]) :: t()

Create a new sparkline struct from some data.