PrawnEx.Chart (prawn_ex v0.4.0)

Copy Markdown View Source

Bar and line charts using drawing primitives (rectangles, lines, text).

Used by PrawnEx.bar_chart/3, PrawnEx.line_chart/3, and PrawnEx.multi_line_chart/3. Data is scaled to fit the chart box; optional axis and labels.

Colors: every :bar_color / :stroke_color (and a series :color) accepts either a gray level (0..1) or an {r, g, b} tuple (0..1 each), so charts can carry a brand color instead of grayscale.

Summary

Functions

Draws a vertical bar chart. Data: list of {label, value} or [label, value]. Bars grow upward from the baseline; labels go below.

Draws a line chart. Data: list of y-values (x = index) or list of {x, y} points. Points are scaled to fit the chart box.

Draws several lines in ONE chart box on a SHARED scale — the thing a single line_chart/3 per series cannot do, since each would normalize to its own min/max and the comparison would lie.

Functions

bar_chart(doc, data, opts)

@spec bar_chart(PrawnEx.Document.t(), [{String.t(), number()} | [term()]], keyword()) ::
  PrawnEx.Document.t()

Draws a vertical bar chart. Data: list of {label, value} or [label, value]. Bars grow upward from the baseline; labels go below.

Options (beyond :at, :width, :height, :axis, :labels, :label_font_size, :padding):

  • :bar_color — gray level or {r, g, b} tuple
  • :value_labels — draw each bar's value above it (default false)
  • :value_formatter — 1-arity fun turning the value into the label text (default &to_string/1)
  • :value_font_size — default 8

line_chart(doc, data, opts)

@spec line_chart(PrawnEx.Document.t(), [number()] | [{number(), number()}], keyword()) ::
  PrawnEx.Document.t()

Draws a line chart. Data: list of y-values (x = index) or list of {x, y} points. Points are scaled to fit the chart box.

multi_line_chart(doc, series, opts)

@spec multi_line_chart(PrawnEx.Document.t(), [map() | keyword()], keyword()) ::
  PrawnEx.Document.t()

Draws several lines in ONE chart box on a SHARED scale — the thing a single line_chart/3 per series cannot do, since each would normalize to its own min/max and the comparison would lie.

Series: list of maps (or keywords) with :data (y-values or {x, y} points), optional :color (gray or {r, g, b}, defaults cycle a small gray ramp), and optional :label for the legend.

Options: :at, :width, :height, :padding, :axis, :from_zero (scale the y-axis from zero instead of the data minimum, default false), :legend (draw color-swatch labels above the box when any series has a :label, default true), :legend_font_size (default 8).