Visualize.Shape.Pie (Visualize v0.2.25)

Copy Markdown View Source

Pie layout generator that computes angles for pie and donut charts.

This doesn't generate paths directly - it computes start and end angles that can be passed to the Arc generator.

Examples

data = [
  %{name: "A", value: 30},
  %{name: "B", value: 50},
  %{name: "C", value: 20}
]

pie = Visualize.Shape.Pie.new()
  |> Visualize.Shape.Pie.value(fn d -> d.value end)

arcs = Visualize.Shape.Pie.generate(pie, data)
# Returns list of maps with :data, :value, :index, :start_angle, :end_angle, :pad_angle

Summary

Functions

Sets the end angle (in radians)

Generates arc data from the input data.

Creates a new pie generator

Sets the pad angle between arcs (in radians)

Sets a comparator to sort the data before computing angles.

Sets a comparator to sort by computed values.

Sets the start angle (in radians)

Sets the value accessor

Types

accessor()

@type accessor() :: (any() -> number()) | atom()

arc_data()

@type arc_data() :: %{
  data: any(),
  value: number(),
  index: non_neg_integer(),
  start_angle: number(),
  end_angle: number(),
  pad_angle: number()
}

comparator()

@type comparator() :: (any(), any() -> boolean())

t()

@type t() :: %Visualize.Shape.Pie{
  end_angle: number() | (any() -> number()),
  pad_angle: number() | (any() -> number()),
  sort: comparator() | nil,
  sort_values: comparator() | nil,
  start_angle: number() | (any() -> number()),
  value: accessor() | nil
}

Functions

end_angle(pie, angle)

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

Sets the end angle (in radians)

generate(pie, data)

@spec generate(t(), Visualize.Data.Table.source()) :: [arc_data()]

Generates arc data from the input data.

Returns a list of maps, each containing:

  • :data - the original data element
  • :value - the computed value
  • :index - the original index
  • :start_angle - start angle in radians
  • :end_angle - end angle in radians
  • :pad_angle - padding angle in radians

new()

@spec new() :: t()

Creates a new pie generator

pad_angle(pie, angle)

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

Sets the pad angle between arcs (in radians)

sort(pie, comparator)

@spec sort(t(), comparator() | nil) :: t()

Sets a comparator to sort the data before computing angles.

Takes a function that receives two data elements and returns true if the first should come before the second.

sort_values(pie, comparator)

@spec sort_values(t(), comparator() | nil) :: t()

Sets a comparator to sort by computed values.

Takes a function that receives two values and returns true if the first should come before the second.

start_angle(pie, angle)

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

Sets the start angle (in radians)

value(pie, accessor)

@spec value(t(), accessor()) :: t()

Sets the value accessor