View Source Contex.LinePlot (ContEx v0.5.0)

A simple point plot, plotting points showing y values against x values.

It is possible to specify multiple y columns with the same x column. It is not yet possible to specify multiple independent series.

Data are sorted by the x-value prior to plotting.

The x column can either be numeric or date time data. If numeric, a Contex.ContinuousLinearScale is used to scale the values to the plot, and if date time, a Contex.TimeScale is used.

Fill colours for each y column can be specified with colours/2.

A column in the dataset can optionally be used to control the colours. See colours/2 and set_colour_col_name/2

Link to this section Summary

Functions

Create a new point plot definition and apply defaults.

Link to this section Types

@type t() :: %Contex.LinePlot{
  colour_palette: term(),
  dataset: term(),
  legend_scale: term(),
  mapping: term(),
  options: term(),
  transforms: term(),
  x_scale: term(),
  y_scale: term()
}

Link to this section Functions

Link to this function

new(dataset, options \\ [])

View Source
@spec new(
  Contex.Dataset.t(),
  keyword()
) :: t()

Create a new point plot definition and apply defaults.

Options may be passed to control the settings for the barchart. Options available are:

  • :axis_label_rotation : :auto (default), 45 or 90

Specifies the label rotation value that will be applied to the bottom axis. Accepts integer values for degrees of rotation or :auto. Note that manually set rotation values other than 45 or 90 will be treated as zero. The default value is :auto, which sets the rotation to zero degrees if the number of items on the axis is greater than eight, 45 degrees otherwise.

  • :custom_x_scale : nil (default) or an instance of a suitable Contex.Scale.

The scale must be suitable for the data type and would typically be either Contex.ContinuousLinearScale or Contex.TimeScale. It is not necessary to set the range for the scale as the range is set as part of the chart layout process.

  • :custom_y_scale : nil (default) or an instance of a suitable Contex.Scale.

  • :custom_x_formatter : nil (default) or a function with arity 1

Allows the axis tick labels to be overridden. For example, if you have a numeric representation of money and you want to have the x axis show it as millions of dollars you might do something like:

  # Turns 1_234_567.67 into $1.23M
  defp money_formatter_millions(value) when is_number(value) do
    "$#{:erlang.float_to_binary(value/1_000_000.0, [decimals: 2])}M"
  end

  defp show_chart(data) do
    LinePlot.new(
      dataset,
      mapping: %{x_col: :column_a, y_cols: [:column_b, column_c]},
      custom_x_formatter: &money_formatter_millions/1
    )
  end
  • :custom_y_formatter : nil (default) or a function with arity 1.

  • :stroke_width : 2 (default) - stroke width of the line

  • :smoothed : true (default) or false - draw the lines smoothed

Note that the smoothing algorithm is a cardinal spline with tension = 0.3. You may get strange effects (e.g. loops / backtracks) in certain circumstances, e.g. if the x-value spacing is very uneven. This alogorithm forces the smoothed line through the points.

  • :colour_palette : :default (default) or colour palette - see colours/2

Overrides the default colours.

Where multiple y columns are defined for the plot, a different colour will be used for each column.

If a single y column is defined and a :fill_colcolumn is mapped, a different colour will be used for each unique value in the colour column.

If a single y column is defined and no :fill_colcolumn is mapped, the first colour in the supplied colour palette will be used to plot the points.

Colours can either be a named palette defined in Contex.CategoryColourScale or a list of strings representing hex code of the colour as per CSS colour hex codes, but without the #. For example:

  chart = LinePlot.new(
      dataset,
      mapping: %{x_col: :column_a, y_cols: [:column_b, column_c]},
      colour_palette: ["fbb4ae", "b3cde3", "ccebc5"]
    )

The colours will be applied to the data series in the same order as the columns are specified in set_val_col_names/2

  • :mapping : Maps attributes required to generate the barchart to columns in the dataset.

If the data in the dataset is stored as a map, the :mapping option is required. If the dataset is not stored as a map, :mapping may be left out, in which case the first column will be used for the x and the second column used as the y. This value must be a map of the plot's :x_col and :y_cols to keys in the map, such as %{x_col: :column_a, y_cols: [:column_b, column_c]}. The value for the :y_cols key must be a list.

If a single y column is specified an optional :fill_col mapping can be provided to control the point colour. This is ignored if there are multiple y columns.