Visualize.Shape.Area (Visualize v0.2.35)

Copy Markdown View Source

Area generator for creating filled areas between two lines.

Useful for area charts, stacked areas, and range visualizations.

Examples

data = [
  %{date: ~D[2024-01-01], value: 10},
  %{date: ~D[2024-01-02], value: 25},
  %{date: ~D[2024-01-03], value: 15}
]

area = Visualize.Shape.Area.new()
  |> Visualize.Shape.Area.x(fn d -> scale_x.(d.date) end)
  |> Visualize.Shape.Area.y0(fn _d -> height end)  # baseline
  |> Visualize.Shape.Area.y1(fn d -> scale_y.(d.value) end)
  |> Visualize.Shape.Area.curve(:monotone_x)

path_data = Visualize.Shape.Area.generate(area, data)

Summary

Functions

Sets the defined predicate: a falsy result breaks the area into separate rings

Generates the SVG path data string.

Generates a Path struct.

Creates a new area generator

Sets the x0 accessor (left edge)

Sets the x1 accessor (right edge)

Sets the x accessor (used for both x0 and x1)

Sets the y0 accessor (bottom edge / baseline)

Sets the y1 accessor (top edge)

Sets the y accessor (used for both y0 and y1)

Types

accessor()

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

t()

@type t() :: %Visualize.Shape.Area{
  curve: Visualize.Shape.Curve.curve_type(),
  curve_opts: keyword(),
  defined: (any() -> boolean()) | nil,
  x: accessor() | nil,
  x0: accessor() | nil,
  x1: accessor() | nil,
  y: accessor() | nil,
  y0: accessor() | nil,
  y1: accessor() | nil
}

Functions

curve(area, curve_type, opts \\ [])

@spec curve(t(), Visualize.Shape.Curve.curve_type(), keyword()) :: t()

Sets the curve type

defined(area, predicate)

@spec defined(t(), (any() -> boolean())) :: t()

Sets the defined predicate: a falsy result breaks the area into separate rings

generate(area, data)

@spec generate(t(), Visualize.Data.Table.source()) :: String.t()

Generates the SVG path data string.

Note: For backend-agnostic usage, prefer generate_path/2 and render with your chosen backend.

generate_path(area, data)

@spec generate_path(t(), Visualize.Data.Table.source()) :: Visualize.IR.Path.t()

Generates a Path struct.

Each run of consecutive defined data (see defined/2) becomes one closed ring: the top edge forward, a line to the bottom edge, the bottom edge reversed, and Z. A run of fewer than two points draws nothing.

new()

@spec new() :: t()

Creates a new area generator

x0(area, accessor)

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

Sets the x0 accessor (left edge)

x1(area, accessor)

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

Sets the x1 accessor (right edge)

x(area, accessor)

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

Sets the x accessor (used for both x0 and x1)

y0(area, accessor)

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

Sets the y0 accessor (bottom edge / baseline)

y1(area, accessor)

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

Sets the y1 accessor (top edge)

y(area, accessor)

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

Sets the y accessor (used for both y0 and y1)