multiline-chart
v1.0.4
Published
A lightweight, dependency-free multi-line time-series chart, packaged as a native [Web Component](https://developer.mozilla.org/en-US/docs/Web/API/Web_components). It renders multiple data series on a `<canvas>` with interactive hover scrubbing, a live le
Readme
multiline-chart
A lightweight, dependency-free multi-line time-series chart, packaged as a native
Web Component.
It renders multiple data series on a <canvas> with interactive hover
scrubbing, a live legend, and full responsive/touch support.
Everything (markup, styles, and rendering logic) is encapsulated in a single
custom element, <multi-line-chart>, using Shadow DOM, so it drops into any page
or framework without style leakage and without external libraries.
Features
- Zero runtime dependencies – just plain TypeScript compiled to JavaScript.
- Canvas rendering – crisp lines on any screen via
devicePixelRatioscaling. - Interactive hover – a tracking line, date tooltip, on-line value dots, and spread-out labels that avoid overlapping.
- Live legend – shows the current value per series and updates as you hover.
- Responsive – redraws on window resize and adapts label sizing/alignment on small screens.
- Touch support – scrub the chart on mobile; taps still pass through, drags prevent page scroll.
- Dynamic Y-axis scaling – axis bounds are computed from the data and rounded to clean intervals.
- Sparse-data friendly – series can have different/missing dates; missing points are interpolated for hover markers.
- Encapsulated styling – CSS is bundled into the component via Shadow DOM.
- Customizable title and logo – via the
titleattribute and alogoslot.
Project structure
multiline-chart/
├── src/
│ ├── index.ts # MultiLineChart custom element (all chart logic)
│ ├── style.css # Component styles (source of truth)
│ └── chart-styles.ts # AUTO-GENERATED from style.css (do not edit)
├── scripts/
│ └── inject-styles.js # Inlines style.css into chart-styles.ts at build time
├── dist/ # Compiled output (generated by build)
├── package.json
└── tsconfig.jsonThe build has two steps (wired together in the build script):
scripts/inject-styles.jsreadssrc/style.cssand writes it intosrc/chart-styles.tsas an exportedCHART_CSSstring. This bundles the CSS into the JS output without needing a CSS-aware bundler.tsccompiles the TypeScript insrc/todist/.
Getting started
Prerequisites
- Node.js and npm
Install
npm installBuild
npm run buildThis regenerates src/chart-styles.ts from src/style.css and compiles the
TypeScript into dist/.
Usage
Import the compiled module and register the element, then use the
<multi-line-chart> tag in your HTML.
<multi-line-chart title="Market Share">
<!-- optional: any markup projected into the header logo slot -->
<img slot="logo" src="logo.svg" alt="Logo" width="120" />
</multi-line-chart>
<script type="module">
import { MultiLineChart } from './dist/index.js';
// Register the <multi-line-chart> custom element.
MultiLineChart.initialize();
const chart = document.querySelector('multi-line-chart');
chart.setData([
{
name: 'Series A',
data: [
{ date: '2024-01-01', value: 0.42 },
{ date: '2024-02-01', value: 0.45 },
{ date: '2024-03-01', value: 0.51 },
],
},
{
name: 'Series B',
data: [
{ date: '2024-01-01', value: 0.30 },
{ date: '2024-02-01', value: 0.28 },
{ date: '2024-03-01', value: 0.35 },
],
},
]);
</script>You can call setData() before the element is attached to the DOM; the data is
buffered and rendered once it connects.
API
MultiLineChart.initialize()
Static method that registers the custom element under the tag name
multi-line-chart (no-op if already registered).
setData(lines)
Sets the chart data and triggers a render.
interface DataPoint {
date: string; // any value parseable by `new Date(...)`
value: number; // fractional value; rendered as a percentage (value * 100)
}
interface InputLine {
name: string; // series name shown in the legend / hover labels
data: DataPoint[];
}
setData(lines: InputLine[]): void;Notes:
valueis treated as a fraction and displayed as a percentage (e.g.0.42renders as42%).- Series are sorted by their leading value and assigned colors automatically from the built-in color scheme.
- Dates are unioned across all series; gaps within a series are interpolated for hover markers.
title attribute
Sets the header title. Changing it after render updates the header in place.
<multi-line-chart title="Quarterly Revenue"></multi-line-chart>logo slot
Project arbitrary markup (e.g. an inline SVG or image) into the header's logo
area using slot="logo".
Development
- Edit styles in
src/style.css(notsrc/chart-styles.ts, which is generated). - Run
npm run buildafter changes to regenerate styles and recompile.
License
ISC
