View Source Contex.BarChart (ContEx v0.5.0)

Draws a barchart from a Contex.Dataset.

Contex.BarChart will attempt to create reasonable output with minimal input. The defaults are as follows:

  • Bars will be drawn vertically (use orientation/2 to override - options are :horizontal and :vertical)
  • The first column of the dataset is used as the category column (i.e. the bar), and the second column is used as the value column (i.e. the bar height). These can be overridden with set_cat_col_name/2 and set_val_col_names/2
  • The barchart type defaults to :stacked. This doesn't really matter when you only have one series (one value column) but if you accept the defaults and then add another value column you will see stacked bars rather than grouped. You can override this with type/2
  • By default the chart will be annotated with data labels (i.e. the value of a bar will be printed on a bar). This can be overridden with data_labels/2. This override has no effect when there are 4 or more value columns specified.
  • By default, the padding between the data series is 2 (how this translates into pixels depends on the plot size you specify when adding the barchart to a Contex.Plot)

By default the BarChart figures out reasonable value axes. In the case of a :stacked bar chart it find the maximum of the sum of the values for each category and the value axis is set to {0, that_max}. For a :grouped bar chart the value axis minimum is set to the minimum value for any category and series, and likewise, the maximum is set to the maximum value for any category and series. This may not work. For example, in the situation where you want zero to be shown. You can force the range by setting the :custom_value_scale option or force_value_range/2 (deprecated)

Link to this section Summary

Functions

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.

Overrides the default colours.

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

Specifies whether data labels are shown on the bars

Optionally specify a LiveView event handler. This attaches a phx-click attribute to each bar element. You can specify the event_target for LiveComponents - a phx-target attribute will also be attached.

Forces the value scale to the given data range

Creates a new barchart from a dataset and sets defaults.

Specifies whether the bars are drawn horizontally or vertically.

Specifies the padding between the category groups. Defaults to 2. Specified relative to the plot size.

Highlights a selected value based on matching category and series.

Sets the category column name. This must exist in the dataset.

Sets the default scales for the plot based on its column mapping.

Sets the value column names. Each must exist in the dataset.

type(plot, type) deprecated

Specifies whether the bars are drawn stacked or grouped.

Link to this section Types

@type orientation() :: :vertical | :horizontal
@type plot_type() :: :stacked | :grouped
@type selected_item() :: %{category: any(), series: any()}
@type t() :: %Contex.BarChart{
  category_scale: term(),
  dataset: term(),
  mapping: term(),
  options: term(),
  phx_event_handler: term(),
  select_item: term(),
  series_fill_colours: term(),
  value_range: term(),
  value_scale: term()
}

Link to this section Functions

Link to this function

axis_label_rotation(plot, rotation)

View Source
This function is deprecated. Set in new/2 options.
@spec axis_label_rotation(t(), integer() | :auto) :: t()

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.

Link to this function

colours(plot, colour_palette)

View Source
This function is deprecated. Set in new/2 options.
@spec colours(t(), Contex.CategoryColourScale.colour_palette()) :: t()

Overrides the default colours.

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:

  barchart = BarChart.colours(barchart, ["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

Link to this function

custom_value_formatter(plot, custom_value_formatter)

View Source
This function is deprecated. Set in new/2 options.
@spec custom_value_formatter(t(), nil | (... -> any())) :: t()

Allows the axis tick labels to be overridden. For example, if you have a numeric representation of money and you want to have the value 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
    BarChart.new(data)
    |> BarChart.custom_value_formatter(&money_formatter_millions/1)
  end
Link to this function

data_labels(plot, data_labels)

View Source
This function is deprecated. Set in new/2 options.
@spec data_labels(t(), boolean()) :: t()

Specifies whether data labels are shown on the bars

Link to this function

event_handler(plot, event_handler, event_target \\ nil)

View Source
This function is deprecated. Set in new/2 options.

Optionally specify a LiveView event handler. This attaches a phx-click attribute to each bar element. You can specify the event_target for LiveComponents - a phx-target attribute will also be attached.

Note that it may not work with some browsers (e.g. Safari on iOS).

Link to this function

force_value_range(plot, value_range)

View Source
This function is deprecated. Use the `:custom_value_scale` option in `new/2`.
@spec force_value_range(
  t(),
  {number(), number()}
) :: t()

Forces the value scale to the given data range

Link to this function

new(dataset, options \\ [])

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

Creates a new barchart from a dataset and sets defaults.

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

  • :type : :stacked (default) or :grouped
  • :orientation : :vertical (default) or :horizontal
  • :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_value_scale : nil (default) or an instance of a suitable Contex.Scale.

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

  • :custom_value_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 value 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
    BarChart.new(
      dataset,
      mapping: %{category_col: :column_a, value_cols: [:column_b, column_c]},
      custom_value_formatter: &money_formatter_millions/1
    )
  end
  • :padding : integer (default 2) - Specifies the padding between the category groups. Defaults to 2. Specified relative to the plot size.
  • :data_labels : true (default) or false - display labels for each bar value
  • :colour_palette : :default (default) or colour palette - see colours/2

Overrides the default colours.

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:

  barchart = BarChart.new(
      dataset,
      mapping: %{category_col: :column_a, value_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

  • :phx_event_handler : nil (default) or string representing phx-click event handler
  • :phx_event_target : nil (default) or string representing phx-target for handler

Optionally specify a LiveView event handler. This attaches a phx-click attribute to each bar element. You can specify the event_target for LiveComponents - a phx-target attribute will also be attached.

Note that it may not work with some browsers (e.g. Safari on iOS).

  • :select_item - optional selected item to highlight. See select_item/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 category and the second column used as the value. This value must be a map of the plot's :category_col and :value_cols to keys in the map, such as %{category_col: :column_a, value_cols: [:column_b, column_c]}. The value for the :value_cols key must be a list.

Link to this function

orientation(plot, orientation)

View Source
This function is deprecated. Set in new/2 options.
@spec orientation(t(), orientation()) :: t()

Specifies whether the bars are drawn horizontally or vertically.

This function is deprecated. Set in new/2 options.
@spec padding(t(), number()) :: t()

Specifies the padding between the category groups. Defaults to 2. Specified relative to the plot size.

Link to this function

select_item(plot, select_item)

View Source
@spec select_item(t(), selected_item()) :: t()

Highlights a selected value based on matching category and series.

Link to this function

set_cat_col_name(plot, cat_col_name)

View Source
This function is deprecated. Use `:mapping` option in `new/2`.

Sets the category column name. This must exist in the dataset.

This provides the labels for each bar or group of bars

Link to this function

set_default_scales(plot)

View Source
This function is deprecated. Default scales are now silently applied.
@spec set_default_scales(t()) :: t()

Sets the default scales for the plot based on its column mapping.

Link to this function

set_val_col_names(plot, val_col_names)

View Source
This function is deprecated. Use `:mapping` option in `new/2`.

Sets the value column names. Each must exist in the dataset.

This provides the value for each bar.

This function is deprecated. Set in new/2 options.
@spec type(t(), plot_type()) :: t()

Specifies whether the bars are drawn stacked or grouped.