View Source SparklineSvg.ReferenceLine (Sparkline SVG v0.5.0)

SparklineSvg.ReferenceLine is an internal struct used by SparklineSvg to represent a reference line.

There are five types of reference lines; four of them are easily accessible by using the corresponding atom:

  • :max - shows the maximum value of the chart.
  • :min - shows the minimum value of the chart.
  • :avg - shows the average value of the chart.
  • :median - shows the median value of the chart.
# Use a predefined reference line
sparkline =
  [1, 2, 3, 1]
  |> SparklineSvg.new()
  |> SparklineSvg.show_line()
  |> SparklineSvg.show_ref_line(:max)

The fifth type is a percentile reference line, which can be created by calling percentile/1 as the second argument of SparklineSvg.show_ref_line/3.

# Use the percentile reference line with custom options
percentile_99 = SparklineSvg.ReferenceLine.percentile(99)

sparkline =
  [1, 2, 3, 1]
  |> SparklineSvg.new()
  |> SparklineSvg.show_line()
  |> SparklineSvg.show_ref_line(percentile_99)

Finally, you can implement custom reference lines by passing a function that receives SparklineSvg.Core.points/0 and returns a unique SparklineSvg.Core.y/0 value at which to display the line.

# Use a custom reference line
fixed_ref_line = fn _ -> 7 end

custom_ref_line =
  fn points ->
    Enum.sum_by(points, fn {_x, y} -> y end) / 3
  end

sparkline =
  [1, 2, 3, 1]
  |> SparklineSvg.new()
  |> SparklineSvg.show_line()
  |> SparklineSvg.show_ref_line(fixed_ref_line, color: "blue")
  |> SparklineSvg.show_ref_line(custom_ref_line, color: "red")

Note that the custom function for a reference line will be called after the main checks and transformations of the data points. No further checks are performed on the returned value.

Summary

Types

A reference line function.

Functions

Returns the average value of a list of points.

Returns the maximum value of a list of points.

Returns the median value of a list of points.

Returns the minimum value of a list of points.

Returns, given a integer() nth a new function that can be used to calculates the nth percentile of a list of points.

Types

@type ref_line_function() :: (SparklineSvg.Core.points() -> SparklineSvg.Core.y())

A reference line function.

Functions

Link to this function

avg(datapoints)

View Source (since 0.4.0)

Returns the average value of a list of points.

Examples

iex> SparklineSvg.ReferenceLine.avg([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
1.75
Link to this function

max(datapoints)

View Source (since 0.4.0)

Returns the maximum value of a list of points.

Examples

iex> SparklineSvg.ReferenceLine.max([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
3
Link to this function

median(datapoints)

View Source (since 0.4.0)

Returns the median value of a list of points.

Examples

iex> SparklineSvg.ReferenceLine.median([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
1.5
Link to this function

min(datapoints)

View Source (since 0.4.0)

Returns the minimum value of a list of points.

Examples

iex> SparklineSvg.ReferenceLine.min([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
1
Link to this function

percentile(nth)

View Source (since 0.4.0)
@spec percentile(integer()) :: ref_line_function()

Returns, given a integer() nth a new function that can be used to calculates the nth percentile of a list of points.

Examples

iex> percentile_99 = SparklineSvg.ReferenceLine.percentile(99)
iex> percentile_99.([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
3