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
@spec avg(SparklineSvg.Core.points()) :: SparklineSvg.Core.y()
Returns the average value of a list of points.
Examples
iex> SparklineSvg.ReferenceLine.avg([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
1.75
@spec max(SparklineSvg.Core.points()) :: SparklineSvg.Core.y()
Returns the maximum value of a list of points.
Examples
iex> SparklineSvg.ReferenceLine.max([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
3
@spec median(SparklineSvg.Core.points()) :: SparklineSvg.Core.y()
Returns the median value of a list of points.
Examples
iex> SparklineSvg.ReferenceLine.median([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
1.5
@spec min(SparklineSvg.Core.points()) :: SparklineSvg.Core.y()
Returns the minimum value of a list of points.
Examples
iex> SparklineSvg.ReferenceLine.min([{1, 1}, {2, 2}, {3, 3}, {4, 1}])
1
@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