ECharts.Phoenix.Component (echarts v0.1.1)

Copy Markdown View Source

Phoenix function components for embedding ECharts charts.

The LiveView component renders a container with the EChartsHook LiveView hook. The hook loads the vendored bundle, renders the chart and keeps it responsive on resize. When the chart assign changes, the hook updates the existing chart in place.

Setup

  1. Mount the asset plug in your router so the bundle is served locally:

    forward "/echarts", ECharts.Phoenix.Plug
  2. Register the hook in assets/js/app.js:

    import { EChartsHook } from "../../deps/echarts/priv/static/echarts_hook.js"
    
    let liveSocket = new LiveSocket("/live", Socket, {
      hooks: { EChartsHook }
    })
  3. Use the component in a template:

    <ECharts.Phoenix.Component.echarts
      id="sales"
      chart={@chart}
      class="h-96 w-full"
    />

    Or import the component and call it as <.echarts ...>.

The hook forwards the chart events listed in the chart's :events option to the LiveView as "echarts_event":

def handle_event("echarts_event", %{"type" => type, "data" => data}, socket) do
  {:noreply, assign(socket, last_event: {type, data})}
end

Large updates

Assigning a new chart re-renders the whole option through the DOM differ. For large series, use ECharts.Phoenix.push_update/3 instead; it sends the chart through a LiveView event and bypasses the differ:

socket = ECharts.Phoenix.push_update(socket, "sales", chart)

Static pages

static_echarts/2 renders a chart in a controller-rendered template. It requires the asset plug as well, since the loader is served from it.

Summary

Functions

Renders a chart in a LiveView template.

Renders a chart as static HTML, for controller-rendered templates.

Functions

echarts(assigns)

Renders a chart in a LiveView template.

Examples

<ECharts.Phoenix.Component.echarts id="revenue" chart={@chart} />

<ECharts.Phoenix.Component.echarts
  id="revenue"
  chart={ECharts.Express.line(data, x: "month", y: "sales")}
  class="h-96 w-full"
/>

Attributes

  • chart (:any) (required) - An ECharts.Chart or a raw ECharts option map.
  • id (:string) (required) - DOM id for the chart container; must be unique per page.
  • class (:any) - Optional CSS classes for the container. Defaults to nil.
  • style (:string) - Optional inline styles for the container. Defaults to nil.
  • asset_base (:string) - Base URL where ECharts.Phoenix.Plug is mounted. Defaults to "/echarts".
  • Global attributes are accepted.

static_echarts(chart, opts \\ [])

Renders a chart as static HTML, for controller-rendered templates.

Returns safe HTML with a container and the module that mounts every static container on the page. Mount ECharts.Phoenix.Plug in your router so the module and the ECharts bundle are served locally.

The mount module scans the page on load and also observes DOM insertions, so charts rendered later (for example by a LiveView patch) still mount.

Options

  • :id - DOM id, defaults to a generated one
  • :asset_base - base URL of the plug, defaults to "/echarts"

Example

# In a controller:
render(conn, :index, chart: ECharts.Express.bar(data, x: "month", y: "sales"))

# In the template:
<%= ECharts.Phoenix.Component.static_echarts(@chart, id: "sales") %>