Macaw.Draw.Shape.Pie (macaw_draw v0.1.0)

Copy Markdown View Source

The angles of a pie, as data (spec/07 §7), copied from visualize's Visualize.Shape.Pie. It draws nothing: generate/2 answers one arc datum per datum, in data order, that Macaw.Draw.Shape.Arc consumes directly.

iex> alias Macaw.Draw.Shape.Pie
iex> [a, b] = Pie.generate(Pie.new(), [1, 3])
iex> {a.end_angle, b.start_angle, b.value}
{1.5707963267948966, 1.5707963267948966, 3}

An arc datum is %{data: d, value: v, index: i, start_angle: a, end_angle: b, pad_angle: p}: v = max(0, value.(d)) (a value that is not a number read as 0), the span da = clamp(end − start, −2π, 2π) shared out as k = (da − n·pad) / Σv per unit (0 when the total is not positive), the pad after each positive value only. Sorting (sort/2 on the data, sort_values/2 on the values) changes the angles, never the order of the answer.

start_angle, end_angle and pad_angle are numbers or 1-arity functions of the whole data list, evaluated once per generate/2; one answering no number is {[key], {:type, :number}}.

Summary

Types

A refusal: faults with their paths.

t()

Functions

Sets end_angle: radians, or a 1-arity function of the whole data list.

The arc data (§7): [] for empty data, else one map per datum in data order.

A pie whose value is the datum itself when a number, its :value field (by spelling) when a map, 0 when that is absent or nil, and 0 otherwise.

Sets pad_angle: radians, or a 1-arity function of the whole data list.

Sets the comparator on data, a 2-arity function, and clears sort_values; nil unsets.

Sets the comparator on values, a 2-arity function, and clears sort; nil unsets.

Sets start_angle: radians, or a 1-arity function of the whole data list.

Sets the value accessor: a function, a field (0 when absent), or a number.

Types

angle()

@type angle() :: number() | ([term()] -> number())

arc_datum()

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

comparator()

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

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

t()

@type t() :: %Macaw.Draw.Shape.Pie{
  end_angle: angle(),
  pad_angle: angle(),
  sort: comparator() | nil,
  sort_values: comparator() | nil,
  start_angle: angle(),
  value: (term() -> term()) | nil
}

Functions

end_angle(pie, angle)

@spec end_angle(t(), angle()) :: t() | error()

Sets end_angle: radians, or a 1-arity function of the whole data list.

generate(pie, data)

@spec generate(t(), [term()]) :: [arc_datum()] | error()

The arc data (§7): [] for empty data, else one map per datum in data order.

new()

@spec new() :: t()

A pie whose value is the datum itself when a number, its :value field (by spelling) when a map, 0 when that is absent or nil, and 0 otherwise.

pad_angle(pie, angle)

@spec pad_angle(t(), angle()) :: t() | error()

Sets pad_angle: radians, or a 1-arity function of the whole data list.

sort(pie, comparator)

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

Sets the comparator on data, a 2-arity function, and clears sort_values; nil unsets.

sort_values(pie, comparator)

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

Sets the comparator on values, a 2-arity function, and clears sort; nil unsets.

start_angle(pie, angle)

@spec start_angle(t(), angle()) :: t() | error()

Sets start_angle: radians, or a 1-arity function of the whole data list.

value(pie, fun)

@spec value(t(), term()) :: t() | error()

Sets the value accessor: a function, a field (0 when absent), or a number.