ECharts.Kino (echarts v0.1.1)

Copy Markdown View Source

Livebook integration for ECharts.

The kino renders the chart in place and keeps a small server process, so the chart can be updated without re-evaluating the cell and browser events can be delivered to Elixir.

Rendering

ECharts.Kino.new(%{
  xAxis: %{type: "category", data: ["Mon", "Tue", "Wed"]},
  yAxis: %{type: "value"},
  series: [%{type: "line", data: [120, 200, 150]}]
})

Use render/2 when you do not need to keep the kino around:

ECharts.Kino.render(option, height: 360)

Live updates

chart = ECharts.Kino.new(option)

for point <- stream do
  ECharts.Kino.push(chart, updated_option)
end

push/2 accepts a raw option map or an ECharts.Chart.

Events

The chart forwards the events listed in the :events option (by default "click" and "legendselectchanged"). Subscribe the current process:

ECharts.Kino.subscribe(chart, :chart_events)

receive do
  {:chart_events, %{"type" => "click", "data" => data}} ->
    IO.inspect(data)
end

Subscribe to a single event type with subscribe(chart, tag, :click), or subscribe another process with subscribe(chart, pid, tag).

Assets are public. Never put secrets in the option map.

Summary

Functions

Builds a live kino from an ECharts option map or an ECharts.Chart.

Pushes a new chart to every connected client.

Renders the chart immediately and returns the kino.

Subscribes a process to chart events.

Types

kino()

@type kino() :: Kino.JS.Live.t()

Functions

new(option_or_chart, opts \\ [])

@spec new(ECharts.option() | ECharts.Chart.t(), keyword()) :: kino()

Builds a live kino from an ECharts option map or an ECharts.Chart.

Accepts the options of ECharts.new/2 when given an option map.

push(kino, option_or_chart)

@spec push(kino(), ECharts.option() | ECharts.Chart.t()) :: :ok

Pushes a new chart to every connected client.

Returns :ok. The update runs asynchronously; the chart is replaced in place.

render(option_or_chart, opts \\ [])

@spec render(ECharts.option() | ECharts.Chart.t(), keyword()) :: kino()

Renders the chart immediately and returns the kino.

This is a convenience for notebook cells where you do not need to keep a reference to the kino.

subscribe(kino, tag)

@spec subscribe(kino(), atom()) :: :ok

Subscribes a process to chart events.

Without a pid, subscribes the calling process. filter is :all (default) or an event name such as :click.

Messages are sent as {tag, payload} where payload is %{"type" => event_name, "data" => event_data}.

subscribe(kino, tag, filter)

@spec subscribe(kino(), atom(), atom() | String.t()) :: :ok
@spec subscribe(kino(), pid(), atom()) :: :ok

subscribe(kino, pid, tag, filter)

@spec subscribe(kino(), pid(), atom(), atom() | String.t()) :: :ok