# `ECharts.Chart`
[🔗](https://github.com/Mateus-M-Reis/echarts/blob/main/lib/echarts/chart.ex#L1)

A validated ECharts chart specification.

A chart wraps a raw [ECharts option](https://echarts.apache.org/en/option.html)
map together with the rendering options shared by every integration
(Livebook, Phoenix LiveView and static HTML).

Charts are immutable values. Build one with `ECharts.new/2` (or one of the
`ECharts.Express` helpers) and pass it to the integration of your choice:

    chart = ECharts.new(%{series: [%{type: "line", data: [1, 3, 2]}]})

    ECharts.Kino.render(chart)
    # or, in a LiveView template:
    # <.echarts id="sales" chart={chart} />

The option map is validated only at the boundaries this library owns: it
must be a map, and the rendering options must have valid types. ECharts
validates the option internals in the browser, so new series types work
without a package release.

# `option`

```elixir
@type option() :: map()
```

# `t`

```elixir
@type t() :: %ECharts.Chart{
  events: [String.t()],
  functions: map(),
  gl: boolean(),
  height: pos_integer(),
  maps: map(),
  option: option(),
  plugins: [String.t()],
  renderer: String.t(),
  theme: theme()
}
```

# `theme`

```elixir
@type theme() :: nil | String.t() | map()
```

# `new`

```elixir
@spec new(option(), keyword()) :: t()
```

Builds a chart from a raw ECharts option map.

## Options

  * `:height` - chart height in pixels, defaults to `420`
  * `:theme` - `:light` (the ECharts default), `:dark` (the built-in dark
    theme), a custom theme name, or a theme map. Use `ECharts.Theme.light/0`
    or `ECharts.Theme.dark/0` for a ready-made palette map
  * `:renderer` - `"canvas"` (default) or `"svg"`. SVG is useful for crisp
    screenshots and printing; canvas is faster for large series. ECharts GL
    requires canvas
  * `:gl` - set to `true` to load the bundled `echarts-gl` extension for
    `bar3D`, `scatter3D`, `line3D`, `surface`, `globe`, `map3D`, `lines3D`,
    `polygons3D`, `scatterGL`, `graphGL` and `flowGL` charts
  * `:maps` - map of `name => GeoJSON` registered before rendering, for
    `map`, `map3D`, `lines3D` and `polygons3D` charts
  * `:plugins` - bundled extensions to load: `"wordcloud"` for `wordCloud`
    series and `"liquidfill"` for `liquidFill` series
  * `:functions` - map of callback name to JavaScript source. Use
    `%{"$function" => "name"}` in the option where ECharts expects a
    function, such as a `custom` series' `renderItem`
  * `:events` - browser events forwarded by the integrations, defaults to
    `["click", "legendselectchanged"]`. Pass `:all` for every event listed
    in `ECharts.Events.all/0`, or `[]` to disable forwarding

## Examples

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

# `option`

```elixir
@spec option(t()) :: option()
```

Returns the raw ECharts option map.

# `to_json`

```elixir
@spec to_json(t()) :: String.t()
```

Returns the payload as a JSON string.

# `to_map`

```elixir
@spec to_map(t()) :: map()
```

Returns the payload consumed by the JavaScript clients.

The payload is a plain map, safe to send through Kino or JSON-encode for a
Phoenix data attribute.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
