Phoenix LiveView function components for the common chart types (spec/10 §2).
Each component is a preset design of Visualize.Chart.Presets plus an assign mapping
(spec/14 §15, D-64): its assigns become a design and the rows the design's source binds
to, Visualize.Chart.Frame realises and draws the design, and the component prints the
elements the layer returns — the axes, every path, rectangle, circle and label — in the
markup spec/10 declares. Its template is the <svg> shell and the attribute order of
each mark, nothing more.
Usage
use Phoenix.Component
import Visualize.Components
<.line_chart data={@data} x={& &1.date} y={& &1.value} width={600} height={400} />Components
line_chart/1andarea_chart/1- one series over a time, linear or band x axisbar_chart/1,horizontal_bar_chart/1andstacked_bar_chart/1- bars on a band scalescatter_plot/1- one circle per datum on two linear axespie_chart/1- a pie or donut with optional labels
Conventions
- Empty
datarenders the frame - the<svg>and, where the chart has them, its axes over a[0, 1]domain - with no marks. A domain whose ends coincide (one datum, or every value zero) stays collapsed and maps to the middle of the axis (D-49, D-60). colorsis a scheme atom ofVisualize.Scale.Color.schemes/0, whose colours are taken by index and cycled, or an explicit list of colours used as given. An unknown scheme or an empty list raisesArgumentError.animate={true}putsstyle="transition: all 0.3s ease-in-out;"on every mark, so LiveView's DOM patches animate through CSS.themeis aVisualize.Theme(defaultVisualize.Theme.default/0); every colour, font and size that is not an explicit assign is a slot of it, rendered as a CSS reference with the literal as fallback so a stylesheet overrides it (D-55). A colour assign leftniltakes the slot its documentation names.marginis merged over the component's default, so%{left: 60}moves one side.- The defaults below are
Visualize.Chart.Presets.defaults/1.
This module compiles only when Phoenix.Component is loaded: phoenix_live_view is an
optional dependency, present in a host that has LiveView and never fetched otherwise.
Summary
Functions
Renders an area chart: the area from the baseline up to the series, then the series line on top of it.
Renders a vertical bar chart: a band x scale over the categories in data order and a linear y scale from zero.
Renders a horizontal bar chart: bar_chart/1 with the roles swapped - y is the
category accessor on a band scale down the left, x the numeric accessor on a linear
scale from zero along the bottom.
Renders a line chart: one series with bottom and left axes.
Renders a pie or donut chart.
Renders a scatter plot: one circle per datum on two linear scales over the extents of the values.
Renders a stacked bar chart: one bar per category, one segment per key, stacked by
the :stack transform of spec/14 §5.4.
Functions
Renders an area chart: the area from the baseline up to the series, then the series line on top of it.
Assigns
data- the data points (required); empty renders the frame alonex- asline_chart/1: time, linear or band by the first datum (required)y- accessor to a non-negative number; the y domain is[0, max](required)width,height- pixels (default 600 by 400)margin- merged over%{top: 20, right: 20, bottom: 30, left: 40}fill,fill_opacity- the area (defaultnil, the theme's:series_1, at 0.3)stroke,stroke_width- the line (defaultnil, the theme's:series_1, 2)curve- applied to both the area and the line (default:linear)animate,class,responsive,theme,title,description- per the module conventions
Attributes
data(:any) (required)x(:any) (required)y(:any) (required)width(:integer) - Defaults to600.height(:integer) - Defaults to400.margin(:map) - Defaults to%{left: 40, right: 20, top: 20, bottom: 30}.fill(:string) - Defaults tonil.fill_opacity(:float) - Defaults to0.3.stroke(:string) - Defaults tonil.stroke_width(:integer) - Defaults to2.curve(:atom) - Defaults to:linear.animate(:boolean) - Defaults tofalse.class(:string) - Defaults tonil.responsive(:boolean) - Defaults tofalse.theme(:any) - Defaults tonil.title(:string) - Defaults tonil.description(:string) - Defaults tonil.
Renders a vertical bar chart: a band x scale over the categories in data order and a linear y scale from zero.
Assigns
data- the data points (required); empty renders the frame alonex- accessor to the category (required)y- accessor to a non-negative number; the y domain is[0, max](required)width,height- pixels (default 600 by 400)margin- merged over%{top: 20, right: 20, bottom: 30, left: 40}fill- bar colour (defaultnil: the theme's:series_1)padding- band padding, 0 to 1 (default 0.1)animate,class,responsive,theme,title,description- per the module conventions
Attributes
data(:any) (required)x(:any) (required)y(:any) (required)width(:integer) - Defaults to600.height(:integer) - Defaults to400.margin(:map) - Defaults to%{left: 40, right: 20, top: 20, bottom: 30}.fill(:string) - Defaults tonil.animate(:boolean) - Defaults tofalse.class(:string) - Defaults tonil.responsive(:boolean) - Defaults tofalse.theme(:any) - Defaults tonil.title(:string) - Defaults tonil.description(:string) - Defaults tonil.padding(:float) - Defaults to0.1.
Renders a horizontal bar chart: bar_chart/1 with the roles swapped - y is the
category accessor on a band scale down the left, x the numeric accessor on a linear
scale from zero along the bottom.
Assigns
As bar_chart/1, with margin merged over %{top: 20, right: 20, bottom: 30, left: 100}
to leave room for the category labels.
Attributes
data(:any) (required)x(:any) (required)y(:any) (required)width(:integer) - Defaults to600.height(:integer) - Defaults to400.margin(:map) - Defaults to%{left: 100, right: 20, top: 20, bottom: 30}.fill(:string) - Defaults tonil.animate(:boolean) - Defaults tofalse.class(:string) - Defaults tonil.responsive(:boolean) - Defaults tofalse.theme(:any) - Defaults tonil.title(:string) - Defaults tonil.description(:string) - Defaults tonil.padding(:float) - Defaults to0.1.
Renders a line chart: one series with bottom and left axes.
Assigns
data- the data points (required); empty renders the frame alonex- accessor to the x value: aDateTime,NaiveDateTimeorDategives a time scale, a number a linear scale, anything else a band scale, decided by the first datum (required)y- accessor to a numeric y value; the y domain is the extent of the values (required)width,height- pixels (default 600 by 400)margin-%{top:, right:, bottom:, left:}, merged over%{top: 20, right: 20, bottom: 30, left: 40}curve- any curve type ofVisualize.Shape.curve/3(default:linear)stroke- line colour, also the point fill (defaultnil: the theme's:series_1)stroke_width- (default 2)show_points- draw anr="4"circle at each datum (defaultfalse)x_label,y_label- axis titles as<text class="axis-label">animate,class,responsive,theme,title,description- per the module conventions
Example
<.line_chart data={@sales} x={& &1.date} y={& &1.revenue} curve={:monotone_x} />Attributes
data(:any) (required)x(:any) (required)y(:any) (required)width(:integer) - Defaults to600.height(:integer) - Defaults to400.margin(:map) - Defaults to%{left: 40, right: 20, top: 20, bottom: 30}.curve(:atom) - Defaults to:linear.stroke(:string) - Defaults tonil.stroke_width(:integer) - Defaults to2.animate(:boolean) - Defaults tofalse.class(:string) - Defaults tonil.responsive(:boolean) - Defaults tofalse.theme(:any) - Defaults tonil.title(:string) - Defaults tonil.description(:string) - Defaults tonil.x_label(:string) - Defaults tonil.y_label(:string) - Defaults tonil.show_points(:boolean) - Defaults tofalse.
Renders a pie or donut chart.
Assigns
data- the slices (required); empty renders an empty<svg>value- accessor to the slice value (required)label- accessor to the label text; drawn only whenshow_labelsis also truewidth,height- pixels (default 400 by 400)inner_radius- greater than 0 makes a donut (default 0)outer_radius-nilmeansmin(width, height) / 2 - 10pad_angle- radians between slices (default 0.02)colors- scheme atom or colour list; the i-th slice takes the i-th colour (default:category10)show_labels- (defaulttrue)animate,class,responsive,theme,title,description- per the module conventions
A label sits at the centroid of the slice's mid-radius, as Visualize.Shape.Arc.centroid/2
computes it, inked the theme's text or background, whichever reads on the slice; a
slice too narrow for its whole label draws none.
Attributes
data(:any) (required)value(:any) (required)label(:any) - Defaults tonil.width(:integer) - Defaults to400.height(:integer) - Defaults to400.inner_radius(:integer) - Defaults to0.outer_radius(:integer) - Defaults tonil.pad_angle(:float) - Defaults to0.02.colors(:any) - Defaults to:category10.animate(:boolean) - Defaults tofalse.class(:string) - Defaults tonil.responsive(:boolean) - Defaults tofalse.theme(:any) - Defaults tonil.title(:string) - Defaults tonil.description(:string) - Defaults tonil.show_labels(:boolean) - Defaults totrue.
Renders a scatter plot: one circle per datum on two linear scales over the extents of the values.
Assigns
data- the data points (required); empty renders the frame alonex,y- numeric accessors (required)width,height- pixels (default 600 by 400)margin- merged over%{top: 20, right: 20, bottom: 30, left: 40}fill- point colour (defaultnil: the theme's:series_1)size- the radius, or a one-argument function of the datum returning it (default 5)animate,class,responsive,theme,title,description- per the module conventions
Attributes
data(:any) (required)x(:any) (required)y(:any) (required)width(:integer) - Defaults to600.height(:integer) - Defaults to400.margin(:map) - Defaults to%{left: 40, right: 20, top: 20, bottom: 30}.fill(:string) - Defaults tonil.size(:any) - Defaults to5.animate(:boolean) - Defaults tofalse.class(:string) - Defaults tonil.responsive(:boolean) - Defaults tofalse.theme(:any) - Defaults tonil.title(:string) - Defaults tonil.description(:string) - Defaults tonil.
Renders a stacked bar chart: one bar per category, one segment per key, stacked by
the :stack transform of spec/14 §5.4.
Assigns
data- one map per category (required); empty renders the frame alonex- accessor to the category, applied to each point'sdata(required)keys- the series keys read from each datum (required)width,height- pixels (default 600 by 400)margin- merged over%{top: 20, right: 20, bottom: 30, left: 40}colors- scheme atom or colour list, one colour per key in order (default:category10)padding- band padding (default 0.1)animate,class,responsive,theme,title,description- per the module conventions
The y domain is [0, max y1] over every point of every series.
Attributes
data(:any) (required)x(:any) (required)keys(:list) (required)width(:integer) - Defaults to600.height(:integer) - Defaults to400.margin(:map) - Defaults to%{left: 40, right: 20, top: 20, bottom: 30}.colors(:any) - Defaults to:category10.animate(:boolean) - Defaults tofalse.class(:string) - Defaults tonil.responsive(:boolean) - Defaults tofalse.theme(:any) - Defaults tonil.title(:string) - Defaults tonil.description(:string) - Defaults tonil.padding(:float) - Defaults to0.1.