Web component

Installnpm install @chartkitdev/element

<chart-kit> is a thin wrapper around @chartkitdev/core, in the same role as the Vue, Svelte, and vanilla adapters. It does not add spec keys.

Spec

The element reads the spec from the first of these that is set:

  • the spec property, a chart object
  • the spec attribute, a JSON string
  • a child <script type="application/json">

Changing any of those redraws the chart. The property wins over the attribute and the script.

Leave size.width unset and the element measures its content box, then redraws through a ResizeObserver when the box changes. A numeric size.width stays as written.

html
<script type="module">
  import "@chartkitdev/element";
</script>

<chart-kit theme="soft">
  <script type="application/json">
    {"version":"1","type":"line","data":[{"week":"W1","baskets":12},{"week":"W4","baskets":22}],"encoding":{"x":"week","y":"baskets"},"a11y":{"alt":"Baskets rose."}}
  </script>
</chart-kit>

Theme

theme reflects as an attribute. Setting it overrides spec.theme. A theme name already on the spec is copied onto the attribute when the attribute is empty.

Events

A mark click bubbles as pointclick. When the spec sets interaction.select, the same gesture also fires select. Both are DOM CustomEvents, and detail is the core payload. Removing the element from the document drops the listeners and the interact controller.

js
import "@chartkitdev/element";

const chart = document.querySelector("chart-kit");
chart.spec = spec;
chart.theme = "midnight";
chart.addEventListener("pointclick", (event) => console.log(event.detail));
chart.addEventListener("select", (event) => console.log(event.detail));

Server HTML

renderToString from @chartkitdev/element/ssr returns the element with the SVG already inside, including histogram through journey. On the client, that SVG node is patched in place. Histogram through journey still load through @chartkitdev/core/load when the SVG was not already there.

js
import { renderToString } from "@chartkitdev/element/ssr";

const html = renderToString(spec);

Plain HTML

packages/element/example/index.html has no build step. Serve the repository and open that file. Its import map points at the built dist files.