Fude.Live (Fude v0.1.0)

Copy Markdown View Source

New points for a chart on the page, without sending its data again.

A chart follows its assigns: when the data of <.chart> changes, the chart draws again in place, its marks moving to their new places. For a chart that grows a point at a time (a reading every second, the orders of the day), append/5 sends only the new rows: the chart takes them in, keeps the last max of them, and moves.

def mount(_params, _session, socket) do
  if connected?(socket), do: Phoenix.PubSub.subscribe(MyApp.PubSub, "readings")
  {:ok, assign(socket, chart: Fude.line(x: :at, y: :load), readings: Readings.last(60))}
end

def handle_info({:reading, reading}, socket) do
  {:noreply, Fude.Live.append(socket, "load", socket.assigns.chart, [reading], max: 60)}
end

<.chart id="load" chart={@chart} data={@readings} />

The new rows go through the chart's rules as its data does (types, units of time, formats), and are added to its marks: a chart whose marks are its rows (lines, areas and points with no aggregate). A chart that sums or counts its rows draws again from its assigns instead.

The page's table of the data is the one of the last render.

Summary

Functions

Sends new rows to the chart of an id, made with the chart they belong to (without its data). Options

Has the browser save the chart of an id as a file: :svg, or :png at twice its size. The file has the chart's title, legend and source, as on the page.

Functions

append(socket, id, chart, rows, opts \\ [])

Sends new rows to the chart of an id, made with the chart they belong to (without its data). Options:

  • :max - the most rows the chart keeps: the oldest go first. All of them when not given.

export(socket, id, format)

@spec export(Phoenix.LiveView.Socket.t(), String.t(), :svg | :png) ::
  Phoenix.LiveView.Socket.t()

Has the browser save the chart of an id as a file: :svg, or :png at twice its size. The file has the chart's title, legend and source, as on the page.