Visualize

Visualize is a data-visualization library for Elixir and Phoenix LiveView. You describe a chart as plain data, apply your rows on the server, and get SVG or canvas output.

The problem it solves

Putting a chart in a LiveView app usually means leaving Elixir: a JavaScript charting library, a build step to bundle it, and glue to keep its state in step with the server's. The chart itself ends up as code in the browser, which you can't store, check or reuse from Elixir.

Visualize keeps the whole chart on the server:

Features

Installation

Visualize is on Hex:

def deps do
  [
    {:visualize, "~> 0.2"},
    {:nx, "~> 0.9"}      # optional: binary canvas encoding and Nx-accelerated paths
  ]
end

Then mix deps.get. The API reference and these guides are on HexDocs; read the changelog before moving to a new minor version.

Example

A design composed from fragments, applied to rows, and rendered:

import Visualize.Chart.Build

{:ok, design} =
  Visualize.Chart.compose([
    chart(meta: %{name: "Daily total"}),
    source(:days, [:date, :value]),
    cartesian(margin: %{top: 20, right: 20, bottom: 30, left: 40}),
    time_scale(:x),
    linear_scale(:y, domain: [0, :auto], nice: true),
    axis(:x, :bottom),
    axis(:y, :left, grid: true),
    line(:days, %{x: :date, y: :value}, style: %{curve: :step_after})
  ])

rows = [%{date: ~D[2024-01-01], value: 10}, %{date: ~D[2024-01-02], value: 25}]
{:ok, applied} = Visualize.Chart.apply(design, sources: %{days: rows}, size: {500, 300})

Visualize.Chart.render(applied, root: true)         # an <svg> document
Visualize.Chart.render(applied, backend: :canvas)   # Canvas 2D commands

The design records no size; the host gives one when the chart is applied.

Running the examples

examples/ is a Phoenix LiveView app over the library:

cd examples
./run.sh                  # http://127.0.0.1:4080

./run.sh 4020 picks another port and ./run.sh 0.0.0.0:4020 another address. An address other than loopback exposes an unauthenticated development server. See examples/README.md for the rest.

Documentation

mix docs builds the API reference with the guides and the changelog. Three more documents ship in the package and are read on GitHub:

Status and feedback

Visualize is pre-1.0, and its API can still change between tags. The chart builder is a work in progress, not a finished product: try it, but expect gaps.

Feedback is welcome and encouraged. Bug reports, rough edges, charts you couldn't make and ideas are all useful: open an issue on GitHub.

MIT licensed.