# LovelyChart
> LovelyChart is a dependency-free JavaScript library of Telegram-style interactive charts for the web, written in TypeScript: line, area, bar, step, pie and donut charts drawn on canvas, with a tooltip, a legend, a draggable minimap, drill-down zooming, annotations, day and night skins and colours from CSS variables.
Key facts:
- Install with `npm install lovely-chart`. The package is one ES module with its TypeScript declarations and no dependencies; there is no CommonJS or UMD build.
- Import the class, the default export, and the stylesheet once: `import LovelyChart from 'lovely-chart'` and `import 'lovely-chart/LovelyChart.css'`. A chart needs the stylesheet.
- Build a chart with `new LovelyChart(element, { type, labels, datasets })`: `type` is `line`, `area`, `bar`, `step`, `pie` or `donut`; `labels` are UNIX timestamps in milliseconds, or strings; each dataset is `{ name, values }`, with `values[i]` at `labels[i]`.
- The instance has `update(newData, options?)`, `destroy()` and a few more methods to drive it.
- The chart fills the width of its element and builds again when the width of the window changes, not when only the element resizes.
- The `theme-dark` class on `` switches the chart to its night skin, or `colorScheme: 'system'` follows the system setting.
- The examples of these docs import `./demo-data.js` and fetch `../data/*.json`, which are helpers of the docs at https://lovelychart.io/examples/demo-data.js and https://lovelychart.io/data/, not part of the package.
Every page below is Markdown; the same page for a browser is at the same URL without `.md`. [llms-full.txt](https://lovelychart.io/llms-full.txt) has all of them in one file.
## Getting started
- [Introduction](https://lovelychart.io/guide/introduction.md): LovelyChart is a small, dependency-free library of Telegram-style interactive charts for the web, minimalist and lovely out of the box.
- [Installation](https://lovelychart.io/guide/installation.md): LovelyChart ships as one ES module and one stylesheet. Install it from npm for an app with a bundler, or load both files from a CDN for a plain HTML page.
- [Your first chart](https://lovelychart.io/guide/getting-started.md): This page takes you from an empty element to an interactive chart with a minimap, then shows you how to give it new data and how to remove it.
- [Using with frameworks](https://lovelychart.io/guide/frameworks.md): LovelyChart works with any framework, or none: all it needs is an element. A wrapper component creates the chart when it mounts, hands it new data with `update()`, and destroys it when it unmounts.
## Core concepts
- [Data format](https://lovelychart.io/guide/data.md): Every chart takes one plain object: a `type`, the `labels` along the X axis, and the `datasets` to draw, each with a value per label.
- [Chart types](https://lovelychart.io/guide/chart-types.md): LovelyChart draws six types of chart: line, area, bar, step, pie and donut. You pick one with `type`, then shape it with stacking, percentages, datasets of other types, a second Y axis or a sparkline.
- [Axes and values](https://lovelychart.io/guide/axes.md): Every chart but a pie, a donut and a sparkline draws a Y axis of values and an X axis of labels.
- [Dates, time zones and locales](https://lovelychart.io/guide/dates.md): Most charts run along time.
## Features
- [Tooltip](https://lovelychart.io/guide/tooltip.md): Point at a label, or touch it, and the chart shows its values in a balloon beside the pointer.
- [Legend](https://lovelychart.io/guide/legend.md): A chart of more than one dataset ends with a legend: a button for each dataset, which hides it or shows it again.
- [Minimap and ranges](https://lovelychart.io/guide/minimap.md): A long series reads best a window at a time.
- [Zoom and drill-down](https://lovelychart.io/guide/zoom.md): A chart can drill into a label: a click on a day opens the hours of that day, or the shares of its datasets in a pie.
- [Annotations](https://lovelychart.io/guide/annotations.md): Annotations are marks the chart draws on the canvas together with your data: horizontal rules at values, hatched runs of labels, vertical rules at moments, and a dashed line for a previous period.
- [Updating data and loading](https://lovelychart.io/guide/updating.md): A chart holds one set of data at a time. `update()` swaps in the next one, `setLoading()` shows the chart as waiting for it, and `destroy()` tears the chart down when you're done.
- [Events and external control](https://lovelychart.io/guide/interaction.md): A chart talks to your page in both directions.
- [Accessibility](https://lovelychart.io/guide/accessibility.md): A chart is a picture of data, and a picture needs help to reach every reader.
## Styling
- [Theming and colours](https://lovelychart.io/guide/theming.md): Every chart comes with a day and a night skin, and switches between them with your page.
- [Layout, sizing and fonts](https://lovelychart.io/guide/layout.md): A chart takes the width your layout gives its element and a height you choose for its plot.
- [Overlays and custom marks](https://lovelychart.io/guide/overlays.md): Sometimes a mark belongs in the HTML of your page rather than on the canvas: a badge with a figure, an empty-state message, a link at the label of a release.
## Reference
- [API reference](https://lovelychart.io/guide/api.md): Everything the package exports: the `LovelyChart` class with every option of a chart and every method, the default colours and the TypeScript types.
- [CSS variables and classes](https://lovelychart.io/guide/css.md): The chart reads custom properties for its sizes, its spacing, its fonts and the colours of its skins, and sets a few of its own for the page to place elements by.
## Examples
The code of each example of the gallery, a module that builds its chart in `
` unless it has a page of its own (`.html`), with the styles of some in `.css` beside it.
- [Line chart with a minimap](https://lovelychart.io/examples/code/line-basic.js): Three series over half a year, with a minimap to pan and resize the visible window.
- [Smooth lines](https://lovelychart.io/examples/code/line-smooth.js): `areLinesSmooth` draws monotone curves that never overshoot a value; Kiwi starts hidden in the legend.
- [Solid, dashed and dotted](https://lovelychart.io/examples/code/line-styles.js): One hue for actuals, the plan and last year, told apart by `isDashed` and `isDotted`.
- [Below zero](https://lovelychart.io/examples/code/line-negative.js): Temperatures that cross zero, with a unit suffix and a reference line at freezing.
- [Gaps in the data](https://lovelychart.io/examples/code/line-gaps.js): Hourly readings where `null` values leave a gap for a sensor outage.
- [Select a range with the mouse](https://lovelychart.io/examples/code/line-range-selection.js): Drag across the plot to open a window on those weeks, double-click to see them all; `onRangeChange` reports the range. Also .
- [Stacked areas](https://lovelychart.io/examples/code/area-stacked.js): A year of six fruits stacked on each other, with a minimap.
- [Percentage areas](https://lovelychart.io/examples/code/area-percentage.js): Shares of a whole that add up to 100%; click a day to zoom into a pie of its shares.
- [Translucent area with an outline](https://lovelychart.io/examples/code/area-outline.js): A single area at a low `areaOpacity`, outlined by `areaBorderColor`.
- [Daily bars](https://lovelychart.io/examples/code/bar-basic.js): One series of bars, its Y range pinned to zero by a transparent reference line.
- [Stacked bars with drill-down](https://lovelychart.io/examples/code/bar-drill-down.js): Click a day to load its hours with `onZoom`, and zoom back out to the year.
- [Positive and negative stacks](https://lovelychart.io/examples/code/bar-negative.js): Revenue above zero and refunds below it, in dollars with the minus sign before the `$`.
- [Monthly spending with zoom into shares](https://lovelychart.io/examples/code/bar-monthly-shares.js): Stacked monthly bars of absolute values; `zoomToShares` opens a donut of a month’s categories.
- [Categories as text labels](https://lovelychart.io/examples/code/bar-categories.js): Text labels in place of dates, for days of the week or any other categories.
- [Steps with a limit](https://lovelychart.io/examples/code/step-limit.js): Plan quotas as steps under a limit line, the days still to come hatched as planned.
- [Pie of totals](https://lovelychart.io/examples/code/pie-totals.js): One total per dataset in a single `Total` label. Hover a sector for its value and share.
- [Donut with a gradient](https://lovelychart.io/examples/code/donut-gradient.js): `withGradient` shades each sector from its inner to its outer edge.
- [Pie over a range of days](https://lovelychart.io/examples/code/pie-range.js): A pie of daily values sums the days the minimap selects; drag the window to sum other days.
- [Bars under a budget line](https://lovelychart.io/examples/code/combined-budget.js): Stacked spending bars with a dashed budget drawn as steps by `datasets[*].type`.
- [Two Y axes](https://lovelychart.io/examples/code/combined-two-axes.js): Stacked bars on the main axis and a share line on an axis of its own with `hasSecondYAxis`.
- [A second unit on the axis](https://lovelychart.io/examples/code/secondary-scale.js): `secondaryYAxis` labels the same values in another unit at the right, and adds it to the tooltip.
- [Currency and number formatting](https://lovelychart.io/examples/code/currency.js): Profit and loss in dollars: `valuePrefix`, `isCurrencyPrefix`, `formatTooltipValue` and labels outside the plot.
- [Hours across days](https://lovelychart.io/examples/code/hourly.js): Three days of hourly traffic, labelled by day and hour in UTC.
- [Targets and thresholds](https://lovelychart.io/examples/code/reference-lines.js): Response times against a target and an alert level, in colours of their own.
- [Forecast](https://lovelychart.io/examples/code/forecast.js): The days ahead hatched by `hatchedRanges`, labelled on the plot and noted in the tooltip.
- [Launches and incidents](https://lovelychart.io/examples/code/event-lines.js): `eventLines` mark moments with rules and pins, and add a hint to the tooltip of their day.
- [Against the previous period](https://lovelychart.io/examples/code/comparison.js): A dashed line of the period before, and the change against it in the tooltip.
- [Chart linked to a list](https://lovelychart.io/examples/code/external-list.js): Hovering a row selects its day with `selectLabel()`, and `onSelectedIndexChange` marks the row of the day the chart shows. Also and .
- [A readout in place of the tooltip](https://lovelychart.io/examples/code/readout.js): `onTooltipChange` feeds a panel of the page, and `--lovely-chart-balloon-display` hides the balloon. Also and .
- [Synchronised charts](https://lovelychart.io/examples/code/synced-charts.js): Two charts of the same days select together: each passes its selection to the other. Also .
- [Legend outside the chart](https://lovelychart.io/examples/code/custom-legend.js): `legendContainer` puts the legend in a panel of the page; `onHiddenSeriesChange` reports what is hidden. Also and .
- [Zoom-out control of your own](https://lovelychart.io/examples/code/custom-zoom-out.js): Opens zoomed into the last day with `initialZoom`, and zooms out from a button of the page with `zoomOut()`. Also .
- [Locked history](https://lovelychart.io/examples/code/limit-date.js): `limitDate` locks the months before it behind a mask of the minimap that the reader can click. Also .
- [Elements over the plot](https://lovelychart.io/examples/code/overlay.js): A badge placed by the plot-area variables, and a link that follows its label with `getLabelX()`. Also .
- [Switch periods with a cross-fade](https://lovelychart.io/examples/code/update-period.js): `update()` with `isAnimated` cross-fades into another period, keeping the hidden datasets. Also .
- [Live data](https://lovelychart.io/examples/code/live-data.js): A new value every second, shifting the window and keeping what the reader hid and selected.
- [Loading skeleton](https://lovelychart.io/examples/code/loading.js): A skeleton in the chart’s own shape until the data arrives, and dimmed data while the next loads. Also .
- [Sparklines in a table](https://lovelychart.io/examples/code/sparklines.js): `isSparkline` draws the plot alone, small enough for a row of a table. Also and .
- [Small multiples](https://lovelychart.io/examples/code/small-multiples.js): A grid of compact charts without headers, tooltips or legends, under one key of the page. Also and .
- [A theme of your own](https://lovelychart.io/examples/code/custom-theme.js): Colours from the page’s custom properties, a font, spacing and a skin of its own, repainted by `refreshColors()`. Also and .
- [Right to left](https://lovelychart.io/examples/code/rtl.js): Hebrew dates and names on a right-to-left page, the minus signs kept before the digits.
- [Localised dates](https://lovelychart.io/examples/code/localized-dates.js): Spanish month and weekday names through `dateLocale`, on weekly labels.
- [Accessible name and live readout](https://lovelychart.io/examples/code/accessible-readout.js): An `ariaLabel` of your own, and a polite live region that reads the selected day aloud. Also .
## Optional
- [Changelog](https://lovelychart.io/CHANGELOG.md): the changes of each release
- [Examples gallery](https://lovelychart.io/examples/): every example live and editable, for a browser
- [Source code](https://github.com/Ajaxy/lovely-chart): the repository, with the TypeScript source in `src/`