JavaScript / TypeScript

Mount an SVG chart in an HTML element and manage its lifecycle from your application.

Installation

Start in the root of an npm project. For an empty directory, run npm init -y before installing.

Terminal
npm install @neural-charts/web

The package includes TypeScript types and installs Core. Use a development server that resolves npm imports. The example below uses Vite.

First chart

Save index.html at the root and main.ts under src/. The module finds the element with ID chart and mounts the chart there.

index.html
<div id="chart"></div>
<button type="button" id="remove-chart">Remove chart</button>
<script type="module" src="/src/main.ts"></script>
main.ts
import { createChart, type ChartData, type ChartOptions } from '@neural-charts/web';

const data: ChartData = {
  "type": "line",
  "axes": {
    "x": {
      "type": "linear"
    },
    "y": {
      "type": "linear",
      "includeZero": true
    }
  },
  "series": [
    {
      "id": "visits",
      "name": "Visits",
      "data": [
        {
          "id": "mon",
          "x": 1,
          "y": 12
        },
        {
          "id": "tue",
          "x": 2,
          "y": 18
        },
        {
          "id": "wed",
          "x": 3,
          "y": 15
        }
      ]
    }
  ]
};
const options: ChartOptions = {
  "title": "Weekly activity",
  "locale": "en",
  "height": 360,
  "tooltip": {
    "enabled": true,
    "crosshair": true
  }
};
const host = document.getElementById('chart');
if (!host) throw new Error('Chart host missing');
const result = createChart(host, data, options, {
  instanceId: 'weekly-visits',
  onPointActivate: event => console.log(event.id, event.observation.value),
  onDiagnostic: diagnostic => console.error(diagnostic)
});
if (result.status === 'invalid') throw new Error(result.diagnostic.message);
const chart = result.value;
document.getElementById('remove-chart')?.addEventListener('click', () => {
  chart.destroy();
}, { once: true });

If you already use Vite, run npx vite. In a new directory, install Vite and start the server with these commands. Open the address printed in your terminal.

Terminal
npm install --save-dev vite
npx vite

Data, state and events

Pass callbacks to createChart. Use update to replace data and updateOptions for presentation changes. Check status before treating an update as accepted.

mount-controlled-chart.ts
import { createChart, type ChartData, type ChartOptions } from '@neural-charts/web';

export function mountChart(host: HTMLElement, data: ChartData) {
  let applied: ChartOptions = { hiddenSeriesIds: [] };
  const mounted = createChart(host, data, applied, {
    instanceId: 'controlled-visits',
    onHiddenSeriesChange(hiddenSeriesIds) {
      const next = { ...applied, hiddenSeriesIds };
      const result = chart.updateOptions(next);
      if (result.status === 'ok') applied = next;
    },
    onPointActivate: event => console.log(event.id.pointId),
  });

  if (mounted.status === 'invalid') throw new Error(mounted.diagnostic.message);
  const chart = mounted.value;
  return () => chart.destroy();
}

Interaction, zoom and controlled state · Try drilldown

Customization and export

Use typed content ports. The controller exports SVG, PNG and original data as CSV.

tooltip-content.ts
import { createChart, ChartContentSlotName, type ChartContentPorts, type ChartData } from '@neural-charts/web';

const content: ChartContentPorts = {
  [ChartContentSlotName.Tooltip]: {
    text: tip => tip.observation.label + ' · ' + tip.observation.value,
    render(host, tip) {
      const value = host.ownerDocument.createElement('strong');
      value.textContent = tip.text;
      host.replaceChildren(value);
    },
  },
};

export function mountChart(host: HTMLElement, data: ChartData) {
  return createChart(host, data, { tooltip: { enabled: true } }, {
    instanceId: 'tooltip-example',
    content,
  });
}

Open customization lab · Try the chart toolbar

Lifecycle and testing

Call destroy() on the controller when removing the view. In browser tests, verify mounting, keyboard navigation, invalid updates, and release of SVG and resources on unmount.

Server rendering guide