View Source Contex.ContinuousLinearScale (ContEx v0.5.0)

A linear scale to map continuous numeric data to a plotting coordinate system.

Implements the general aspects of scale setup and use defined in the Contex.Scale protocol

The ContinuousLinearScale is responsible for mapping to and from values in the data to a visual scale. The two key concepts are "domain" and "range".

The "domain" represents values in the dataset to be plotted.

The "range" represents the plotting coordinate system use to plot values in the "domain".

Important Note - When you set domain and range, the scale code makes a few adjustments based on the desired number of tick intervals so that the ticks look "nice" - i.e. on round numbers. So if you have a data range of 0.0 → 8.7 and you want 10 intervals the scale won't display ticks at 0.0, 0.87, 1.74 etc, it will round up the domain to 10 so you have nice tick intervals of 0, 1, 2, 3 etc.

By default the scale creates 10 tick intervals.

When domain and range are both set, the scale makes transform functions available to map each way between the domain and range that are then available to the various plots to map data to plotting coordinate systems, and potentially vice-versa.

The typical setup of the scale looks like this:

y_scale
  = ContinuousLinearScale.new()
    |> ContinuousLinearScale.domain(min_value, max_value)
    |> Scale.set_range(start_of_y_plotting_coord, end_of_y_plotting_coord)

Translating a value to plotting coordinates would then look like this:

plot_y = Scale.domain_to_range(y_scale, y_value)

ContinuousLinearScale implements the Contex.Scale protocol that provides a nicer way to access the transform functions. Calculation of plotting coordinates is typically done in tight loops so you are more likely to do something like than translating a single value as per the above example:

  x_tx_fn = Scale.domain_to_range_fn(x_scale)
  y_tx_fn = Scale.domain_to_range_fn(y_scale)

  points_to_plot = Enum.map(big_load_of_data, fn %{x: x, y: y}=_row ->
          {x_tx_fn.(x), y_tx_fn.(y)}
        end)

Link to this section Summary

Functions

Sets the extents of the value domain for the scale by specifying a list of values to be displayed.

Sets the extents of the value domain for the scale.

Defines the number of intervals between ticks.

Creates a new scale with defaults

Link to this section Types

@type t() :: %Contex.ContinuousLinearScale{
  custom_tick_formatter: term(),
  display_decimals: term(),
  domain: term(),
  interval_count: term(),
  interval_size: term(),
  nice_domain: term(),
  range: term()
}

Link to this section Functions

@spec domain(t(), [number()]) :: t()

Sets the extents of the value domain for the scale by specifying a list of values to be displayed.

The scale will determine the extents of the data.

@spec domain(t(), number(), number()) :: t()

Sets the extents of the value domain for the scale.

Link to this function

interval_count(scale, interval_count)

View Source
@spec interval_count(t(), integer()) :: t()

Defines the number of intervals between ticks.

Defaults to 10.

Tick-rendering is the responsibility of Contex.Axis, but calculating tick intervals is the responsibility of the scale.

@spec new() :: t()

Creates a new scale with defaults