Contex.OrdinalScale (ContEx v0.4.0) View Source

An ordinal scale to map discrete values (text or numeric) to a plotting coordinate system.

An ordinal scale is commonly used for the category axis in barcharts. It has to be able to generate the centre-point of the bar (e.g. for tick annotations) as well as the available width the bar or bar-group has to fill.

In order to do that the ordinal scale requires a 'padding' option to be set (defaults to 0.5 in the scale) that defines the gaps between the bars / categories. The ordinal scale has two mapping functions for the data domain to the plotting range. One returns the centre point (range_to_domain_fn) and one returns the "band" the category can occupy (domain_to_range_band_fn).

An OrdinalScale is initialised with a list of values which represent the categories. The scale generates a tick for each value in that list.

Typical usage of this scale would be as follows:

iex> category_scale
...> = Contex.OrdinalScale.new(["Hippo", "Turtle", "Rabbit"])
...> |> Contex.Scale.set_range(0.0, 9.0)
...> |> Contex.OrdinalScale.padding(2)
...> category_scale.domain_to_range_fn.("Turtle")
4.5
iex> category_scale.domain_to_range_band_fn.("Hippo")
{1.0, 2.0}
iex> category_scale.domain_to_range_band_fn.("Turtle")
{4.0, 5.0}

Link to this section Summary

Functions

Updates the domain data for the scale.

Returns the band for the nominated category in terms of plotting coordinate system.

Creates a new ordinal scale.

Sets the padding between the categories for the scale.

Link to this section Types

Specs

t() :: %Contex.OrdinalScale{
  domain: term(),
  domain_to_range_band_fn: term(),
  domain_to_range_fn: term(),
  padding: term(),
  range: term(),
  range_to_domain_fn: term()
}

Link to this section Functions

Link to this function

domain(ordinal_scale, data)

View Source

Specs

domain(t(), list()) :: t()

Updates the domain data for the scale.

Link to this function

get_band(ordinal_scale, domain_value)

View Source

Specs

get_band(t(), any()) :: {number(), number()}

Returns the band for the nominated category in terms of plotting coordinate system.

If the category isn't found, the start of the plotting range is returned.

Specs

new(list()) :: t()

Creates a new ordinal scale.

Sets the padding between the categories for the scale.

Defaults to 0.5.

Defined in terms of plotting coordinates.

Note that if the padding is greater than the calculated width of each category you might get strange effects (e.g. the end of a band being before the beginning)