ECharts.Express (echarts v0.1.1)

Copy Markdown View Source

Concise builders for the most common ECharts charts.

ECharts.Express turns tabular data into a validated ECharts.Chart without writing the option map by hand. It is a convenience layer, not a different renderer: every function accepts the rendering options of ECharts.new/2, and the result works in Livebook, Phoenix LiveView and static HTML alike.

Data sources

  • a list of maps with string or atom keys — [%{"month" => "Jan", "sales" => 42}]
  • a columnar map — %{"month" => ["Jan", "Feb"], "sales" => [42, 55]}
  • an Explorer.DataFrame when Explorer is available

Examples

data = [
  %{"month" => "Jan", "sales" => 42},
  %{"month" => "Feb", "sales" => 55},
  %{"month" => "Mar", "sales" => 38}
]

ECharts.Express.bar(data, x: "month", y: "sales", title: "Monthly sales")
|> ECharts.Kino.render()

Multiple :y columns become multiple series:

ECharts.Express.line(data, x: "month", y: ["sales", "cost"])

Summary

Functions

Builds a line chart with a filled area. Accepts the same options as line/2; :area is implied.

Builds a bar chart. Accepts the same options as line/2.

Builds a candlestick (OHLC) chart, optionally with a volume panel.

Builds a line chart.

Builds a pie or donut chart.

Builds a scatter chart. Accepts the same options as line/2; two numeric columns produce an [x, y] point cloud, a category x column produces one marker per category.

Functions

area(data, opts \\ [])

@spec area(Enumerable.t() | map(), keyword()) :: ECharts.Chart.t()

Builds a line chart with a filled area. Accepts the same options as line/2; :area is implied.

bar(data, opts \\ [])

@spec bar(Enumerable.t() | map(), keyword()) :: ECharts.Chart.t()

Builds a bar chart. Accepts the same options as line/2.

candlestick(data, opts \\ [])

@spec candlestick(Enumerable.t() | map(), keyword()) :: ECharts.Chart.t()

Builds a candlestick (OHLC) chart, optionally with a volume panel.

ECharts expects each candle as [open, close, low, high]; this function reads those fields from the rows for you.

Options

  • :x - field used for the category axis (required)
  • :open, :high, :low, :close - OHLC fields (required)
  • :volume - optional volume field; when given, a volume panel is added below the price grid and both grids share the zoom
  • :data_zoom - defaults to true; pass false to disable the zoom
  • plus the rendering options accepted by ECharts.new/2

Example

ECharts.Express.candlestick(candles,
  x: "date",
  open: "open",
  high: "high",
  low: "low",
  close: "close",
  volume: "volume",
  height: 480
)

line(data, opts \\ [])

@spec line(Enumerable.t() | map(), keyword()) :: ECharts.Chart.t()

Builds a line chart.

Options are the cartesian options below plus the rendering options accepted by ECharts.new/2 (:height, :theme, :renderer, :gl, :plugins, :maps, :functions, :events).

  • :x - field used for the category/value axis (required)
  • :y - field or list of fields used for the values (required)
  • :title / :subtitle - chart title and subtitle
  • :x_type - "category", "value", "time" or "log"; inferred as "value" when every x value is a number, "category" otherwise
  • :y_type - "value" (default) or "log"
  • :smooth - draw smooth curves
  • :area - fill the area under the line
  • :stack - stack series under a shared name
  • :symbol_size - marker size in pixels
  • :data_zoom - true for inside + slider zoom, or a list/map passed to dataZoom

pie(data, opts \\ [])

@spec pie(Enumerable.t() | map(), keyword()) :: ECharts.Chart.t()

Builds a pie or donut chart.

Options

  • :name - field with the slice label (required)
  • :value - field with the slice value (required)
  • :title / :subtitle - chart title and subtitle
  • :donut - render a donut instead of a pie
  • :rose - use the rose (nightingale) layout
  • plus the rendering options accepted by ECharts.new/2

Example

ECharts.Express.pie(
  [%{"name" => "A", "value" => 40}, %{"name" => "B", "value" => 60}],
  name: "name",
  value: "value",
  donut: true
)

scatter(data, opts \\ [])

@spec scatter(Enumerable.t() | map(), keyword()) :: ECharts.Chart.t()

Builds a scatter chart. Accepts the same options as line/2; two numeric columns produce an [x, y] point cloud, a category x column produces one marker per category.