KinoECharts (kino_echarts v0.6.0)

Copy Markdown View Source

Apache ECharts charts for Livebook.

This kino renders a raw ECharts option map. It is lower-level than a grammar-of-graphics library on purpose: you get candlesticks, dataZoom, financial tooltips and multi-grid layouts without leaving Elixir or paying for a Vega-Lite abstraction you do not need.

The ECharts bundle is vendored, so charts work offline and do not depend on a CDN.

Usage

Mix.install([{:kino_echarts, "~> 0.1"}])

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

Options are passed as a map with string or atom keys. They are sent to the browser as JSON, so values must be JSON-encodable.

Assets are public, like any Livebook kino. Never put secrets (API keys, account balances you would not show on screen) in the option map.

Summary

Functions

Builds a kino from an ECharts option map.

Renders the chart immediately and returns the kino.

Types

option()

@type option() :: map()

Functions

new(option, opts \\ [])

@spec new(option(), keyword()) :: %Kino.JS{
  export: term(),
  module: term(),
  ref: term()
}

Builds a kino from an ECharts option map.

Options

  • :height - canvas height in pixels, defaults to 420

  • :theme - :light (ECharts default light), :dark (built-in dark), a custom theme name, or a theme map

  • :renderer - "canvas" (default) or "svg"; SVG can be useful for crisp screenshots and printing, canvas is faster for large series

  • :gl - set to true to load the bundled echarts-gl extension for bar3D, scatter3D, line3D, surface, map3D and globe series

  • :maps - map of name => GeoJSON registered with ECharts before the option is applied. Use this for map series

  • :functions - map of callback name to JavaScript function source. Use %{"$function" => "name"} in the option where ECharts expects a function, such as a custom series' renderItem. Functions run in the browser and must be trusted code:

    KinoECharts.new(
      %{
        series: [
          %{
            type: "custom",
            renderItem: %{"$function" => "renderItem"},
            data: [[0, 10], [1, 20]]
          }
        ]
      },
      functions: %{
        "renderItem" => "function (params, api) { ... }"
      }
    )

Examples

KinoECharts.new(%{
  xAxis: %{type: "category", data: ["a", "b", "c"]},
  yAxis: %{type: "value"},
  series: [%{type: "bar", data: [1, 3, 2]}]
}, height: 300, theme: "dark")

A map chart registers its GeoJSON first:

KinoECharts.new(
  %{
    series: [%{type: "map", map: "demo", data: [%{name: "A", value: 10}]}]
  },
  maps: %{"demo" => geo_json}
)

render(option, opts \\ [])

@spec render(option(), keyword()) :: %Kino.JS{
  export: term(),
  module: term(),
  ref: term()
}

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:

KinoECharts.render(option, height: 360)