ContEx v0.2.0 Contex.BarChart View Source
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/2to override - options are:horizontaland: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/2andset_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 withtype/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 overriden 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 using force_value_range/2
Link to this section Summary
Functions
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
Re-applies default settings.
Optionally specify a LiveView event handler. This attaches a phx-click attribute to each bar element. Note that it may
not work with some browsers (e.g. Safari on iOS).
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 value column names. Each must exist in the dataset.
Specifies whether the bars are drawn stacked or grouped.
Link to this section Types
Link to this section Functions
colours(plot, colour_palette)
View Sourcecolours(Contex.BarChart.t(), Contex.CategoryColourScale.colour_palette()) :: Contex.BarChart.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
custom_value_formatter(plot, custom_value_formatter)
View Sourcecustom_value_formatter(Contex.BarChart.t(), nil | (... -> any())) :: Contex.BarChart.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
data_labels(plot, data_labels)
View Sourcedata_labels(Contex.BarChart.t(), boolean()) :: Contex.BarChart.t()
Specifies whether data labels are shown on the bars
Re-applies default settings.
Optionally specify a LiveView event handler. This attaches a phx-click attribute to each bar element. Note that it may
not work with some browsers (e.g. Safari on iOS).
force_value_range(plot, value_range)
View Sourceforce_value_range(Contex.BarChart.t(), {number(), number()}) :: Contex.BarChart.t()
Forces the value scale to the given data range
new(dataset, orientation \\ :vertical)
View Sourcenew(Contex.Dataset.t(), orientation()) :: Contex.BarChart.t()
Creates a new barchart from a dataset and sets defaults
orientation(plot, orientation)
View Sourceorientation(Contex.BarChart.t(), orientation()) :: Contex.BarChart.t()
Specifies whether the bars are drawn horizontally or vertically.
padding(plot, padding)
View Sourcepadding(Contex.BarChart.t(), number()) :: Contex.BarChart.t()
Specifies the padding between the category groups. Defaults to 2. Specified relative to the plot size.
select_item(plot, select_item)
View Sourceselect_item(Contex.BarChart.t(), selected_item()) :: Contex.BarChart.t()
Highlights a selected value based on matching category and series.
Sets the category column name. This must exist in the dataset.
This provides the labels for each bar or group of bars
Sets the value column names. Each must exist in the dataset.
This provides the value for each bar.
type(plot, type)
View Sourcetype(Contex.BarChart.t(), plot_type()) :: Contex.BarChart.t()
Specifies whether the bars are drawn stacked or grouped.