ECharts.Chart (echarts v0.1.1)

Copy Markdown View Source

A validated ECharts chart specification.

A chart wraps a raw ECharts option 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.

Summary

Functions

Builds a chart from a raw ECharts option map.

Returns the raw ECharts option map.

Returns the payload as a JSON string.

Returns the payload consumed by the JavaScript clients.

Types

option()

@type option() :: map()

t()

@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()

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

Functions

new(option, opts \\ [])

@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(chart)

@spec option(t()) :: option()

Returns the raw ECharts option map.

to_json(chart)

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

Returns the payload as a JSON string.

to_map(chart)

@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.