# KinoECharts

Apache ECharts charts for [Livebook](https://livebook.dev), with no Vega-Lite
layer in between.

`KinoECharts` renders a raw [ECharts option](https://echarts.apache.org/en/option.html)
map. That keeps the full ECharts surface available: candlesticks, `dataZoom`,
financial tooltips, multiple grids, custom axes and any series type ECharts
supports. The ECharts 6 bundle is vendored, so charts work offline.

## Installation

Once published on Hex:

```elixir
Mix.install([{:kino_echarts, "~> 0.6"}])
```

Until then, install straight from GitHub:

```elixir
Mix.install([
  {:kino, "~> 0.19"},
  {:kino_echarts, github: "Mateus-M-Reis/kino_echarts", tag: "v0.6.0"}
])
```

## Usage

```elixir
KinoECharts.new(%{
  xAxis: %{type: "category", data: ["Mon", "Tue", "Wed", "Thu", "Fri"]},
  yAxis: %{type: "value"},
  series: [
    %{type: "line", data: [120, 200, 150, 80, 170], smooth: true}
  ]
})
```

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

```elixir
KinoECharts.render(option, height: 360)
```

### Candlestick with volume

ECharts wants OHLC values as `[open, close, low, high]`:

```elixir
candles = [
  %{t: "2026-11-24", o: 100.0, h: 106.0, l: 98.0, c: 104.0, v: 1_200},
  %{t: "2026-11-25", o: 104.0, h: 109.0, l: 102.0, c: 107.0, v: 1_800},
  %{t: "2026-11-26", o: 107.0, h: 108.0, l: 99.0, c: 101.0, v: 2_100}
]

option = %{
  tooltip: %{trigger: "axis"},
  xAxis: %{type: "category", data: Enum.map(candles, & &1.t)},
  yAxis: %{type: "value", scale: true},
  dataZoom: [%{type: "inside", start: 0, end: 100}],
  series: [
    %{
      type: "candlestick",
      data: Enum.map(candles, &[&1.o, &1.c, &1.l, &1.h])
    }
  ]
}

KinoECharts.new(option, height: 420, theme: "dark")
```

### Map

ECharts does not bundle geographic data. Register one or more GeoJSON maps with
`:maps`; the client calls `echarts.registerMap/2` before creating the instance:

```elixir
KinoECharts.new(
  %{
    visualMap: %{min: 0, max: 100, left: 20, bottom: 20},
    series: [
      %{
        type: "map",
        map: "demo",
        data: [
          %{name: "Norte", value: 80},
          %{name: "Sul", value: 45}
        ]
      }
    ]
  },
  maps: %{"demo" => geo_json}
)
```

### Custom series

ECharts callbacks cannot travel as JSON. Pass their source with `:functions`
and use a marker where the function is expected:

```elixir
render_item = """
function (params, api) {
  const x = api.value(0);
  const y = api.value(1);
  return {
    type: "rect",
    shape: {x: api.coord([x, y])[0], y: 0, width: 20, height: 20},
    style: api.style()
  };
}
"""

KinoECharts.new(
  %{
    xAxis: %{type: "category", data: ["A", "B", "C"]},
    yAxis: %{type: "value"},
    series: [
      %{
        type: "custom",
        renderItem: %{"$function" => "renderItem"},
        data: [[0, 10], [1, 20], [2, 15]]
      }
    ]
  },
  functions: %{"renderItem" => render_item}
)
```

Functions are compiled with `new Function` in the browser. Only pass trusted
code, just like any other Livebook cell.

## Themes

`KinoECharts` accepts `theme: :light` and `theme: :dark` out of the box.
`:light` uses the ECharts default palette and `:dark` uses the built-in dark
theme. Custom theme names and theme maps are passed through to `echarts.init/3`.

The gallery notebook has a **Tema** selector; change it and re-evaluate a chart
cell to compare light and dark.

## Charts

The gallery covers every built-in 2D chart family in ECharts 6:

- cartesian: line, bar, scatter, effectScatter, heatmap, pictorialBar, lines,
  themeRiver;
- financial: candlestick, boxplot, calendar heatmap;
- circular: pie, radar, gauge, funnel, chord;
- hierarchy and flow: tree, treemap, sunburst, graph, sankey, parallel;
- geo and matrix: map, matrix;
- extensibility: custom series with a JavaScript `renderItem`, passed through
  the `:functions` option;
- 3D/GL: `bar3D`, `scatter3D`, `line3D`, `surface`, `globe`, `map3D`,
  `lines3D`, `polygons3D`, `scatterGL`, `graphGL` and `flowGL`, loaded on
  demand with `gl: true`.

See the
[gallery notebook](https://github.com/Mateus-M-Reis/kino_echarts/blob/main/examples/gallery.livemd)
for a rendered example of each one.

The optional `echarts-gl` bundle is vendored in `priv/static`. It is only
loaded when a chart uses `gl: true`, so 2D charts do not pay for it.

## Options

`KinoECharts.new(option, opts)` accepts:

| Option | Default | Description |
|---|---|---|
| `:height` | `420` | Chart height in pixels. |
| `:theme` | `nil` | `:light` (default), `:dark`, a custom theme name, or a theme map. |
| `:renderer` | `"canvas"` | `"canvas"` or `"svg"`. SVG is useful for printing and crisp screenshots. |
| `:gl` | `false` | Load `echarts-gl` for 3D series. |
| `:maps` | `%{}` | Map of `name => GeoJSON` registered before rendering, for `map` series. |
| `:functions` | `%{}` | Map of callback name to JavaScript source, referenced in the option as `%{"$function" => "name"}`. |

The option map can use atom or string keys. It is JSON-encoded before being
sent to the browser, so all values must be JSON-encodable.

## Troubleshooting

- **Blank output**: run the setup cell and wait for `Mix.install/2` to finish
  compiling the package. Re-run the chart cell afterwards.
- **Map is blank**: make sure the series' `map` name matches a key in
  `:maps`, and that the value is a valid GeoJSON `FeatureCollection`.
- **3D is blank**: pass `gl: true` and use a browser with WebGL enabled.
- **Custom series fails**: every `%{"$function" => "name"}` marker needs a
  matching entry in `:functions`; the client renders JavaScript errors in red
  inside the kino.

## Security

Livebook assets are public. Never put secrets (API keys, private account data)
in the option map. Treat everything you pass to `KinoECharts.new/2` as visible
to anyone who can load the notebook output.

## Development

There are runnable notebooks at
[examples/basic.livemd](https://github.com/Mateus-M-Reis/kino_echarts/blob/main/examples/basic.livemd)
and
[examples/gallery.livemd](https://github.com/Mateus-M-Reis/kino_echarts/blob/main/examples/gallery.livemd).
Run `livebook server examples` from the repository root.

```bash
mix deps.get
mix format
mix test
mix docs
```

The ECharts bundle lives in `priv/static/echarts.min.js` (Apache 2.0). The
client entrypoint is `priv/static/main.js`.

## License

KinoECharts is released under the MIT License. Apache ECharts is bundled under
the Apache License 2.0; see [NOTICE](NOTICE).
