View Source Contex.PieChart (ContEx v0.5.0)

A Pie Chart that displays data in a circular graph.

The pieces of the graph are proportional to the fraction of the whole in each category. Each slice of the pie is relative to the size of that category in the group as a whole. The entire “pie” represents 100 percent of a whole, while the pie “slices” represent portions of the whole.

Fill colours for each slice can be specified with colour_palette parameter in chart options, or can be applied from a CategoryColourScale suppled in the colour_scale parameter. If neither option is supplied a default colour palette is used.

Link to this section Summary

Functions

Overrides the default colours.

Create a new PieChart struct from Dataset.

Renders the PieChart to svg, including the svg wrapper, as a string or improper string list that is marked safe.

Link to this section Types

@type t() :: %Contex.PieChart{
  colour_scale: term(),
  dataset: term(),
  mapping: term(),
  options: term()
}

Link to this section Functions

Link to this function

colours(chart, 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

get_categories(pie_chart)

View Source
Link to this function

new(dataset, options \\ [])

View Source

Create a new PieChart struct from Dataset.

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

  • :data_labels : true (default) or false - display labels for each slice value
  • :colour_palette : :default (default) or colour palette - see colours/2

An example:

  data = [
    ["Cat", 10.0],
    ["Dog", 20.0],
    ["Hamster", 5.0]
  ]

  dataset = Dataset.new(data, ["Pet", "Preference"])

  opts = [
    mapping: %{category_col: "Pet", value_col: "Preference"},
    colour_palette: ["fbb4ae", "b3cde3", "ccebc5"],
    legend_setting: :legend_right,
    data_labels: false,
    title: "Why dogs are better than cats"
  ]

  Contex.Plot.new(dataset, Contex.PieChart, 600, 400, opts)

Renders the PieChart to svg, including the svg wrapper, as a string or improper string list that is marked safe.