swiftplot 0.0.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +348 -3
- package/dist/chart.d.ts +7 -0
- package/dist/chart.js +1311 -0
- package/dist/charts/build.d.ts +52 -0
- package/dist/charts/build.js +63 -0
- package/dist/charts/candlestick.d.ts +8 -0
- package/dist/charts/candlestick.js +105 -0
- package/dist/charts/combo.d.ts +8 -0
- package/dist/charts/combo.js +50 -0
- package/dist/charts/controller.d.ts +32 -0
- package/dist/charts/controller.js +231 -0
- package/dist/charts/defaults.d.ts +20 -0
- package/dist/charts/defaults.js +121 -0
- package/dist/charts/distribution.d.ts +15 -0
- package/dist/charts/distribution.js +178 -0
- package/dist/charts/fields.d.ts +64 -0
- package/dist/charts/fields.js +251 -0
- package/dist/charts/flow.d.ts +15 -0
- package/dist/charts/flow.js +189 -0
- package/dist/charts/heatmap.d.ts +9 -0
- package/dist/charts/heatmap.js +104 -0
- package/dist/charts/index.d.ts +13 -0
- package/dist/charts/index.js +10 -0
- package/dist/charts/mount.d.ts +5 -0
- package/dist/charts/mount.js +21 -0
- package/dist/charts/parts.d.ts +23 -0
- package/dist/charts/parts.js +66 -0
- package/dist/charts/radar.d.ts +7 -0
- package/dist/charts/radar.js +77 -0
- package/dist/charts/source.d.ts +24 -0
- package/dist/charts/source.js +225 -0
- package/dist/charts/types.d.ts +643 -0
- package/dist/charts/types.js +1 -0
- package/dist/charts/xy.d.ts +46 -0
- package/dist/charts/xy.js +421 -0
- package/dist/features/animate.d.ts +20 -0
- package/dist/features/animate.js +118 -0
- package/dist/features/brush.d.ts +15 -0
- package/dist/features/brush.js +128 -0
- package/dist/features/legend.d.ts +13 -0
- package/dist/features/legend.js +236 -0
- package/dist/features/pointer.d.ts +29 -0
- package/dist/features/pointer.js +23 -0
- package/dist/features/tooltip.d.ts +22 -0
- package/dist/features/tooltip.js +192 -0
- package/dist/features/zoom.d.ts +14 -0
- package/dist/features/zoom.js +246 -0
- package/dist/gl.d.ts +5 -0
- package/dist/gl.js +52 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +32 -0
- package/dist/marks/arcs.d.ts +31 -0
- package/dist/marks/arcs.js +125 -0
- package/dist/marks/area.d.ts +25 -0
- package/dist/marks/area.js +106 -0
- package/dist/marks/bar.d.ts +52 -0
- package/dist/marks/bar.js +243 -0
- package/dist/marks/box.d.ts +33 -0
- package/dist/marks/box.js +182 -0
- package/dist/marks/candlestick.d.ts +22 -0
- package/dist/marks/candlestick.js +133 -0
- package/dist/marks/common.d.ts +102 -0
- package/dist/marks/common.js +250 -0
- package/dist/marks/dots.d.ts +16 -0
- package/dist/marks/dots.js +158 -0
- package/dist/marks/errorBars.d.ts +24 -0
- package/dist/marks/errorBars.js +99 -0
- package/dist/marks/funnel.d.ts +30 -0
- package/dist/marks/funnel.js +122 -0
- package/dist/marks/heatmap.d.ts +20 -0
- package/dist/marks/heatmap.js +139 -0
- package/dist/marks/line.d.ts +21 -0
- package/dist/marks/line.js +75 -0
- package/dist/marks/radar.d.ts +29 -0
- package/dist/marks/radar.js +162 -0
- package/dist/marks/radialBars.d.ts +34 -0
- package/dist/marks/radialBars.js +111 -0
- package/dist/marks/rects.d.ts +43 -0
- package/dist/marks/rects.js +120 -0
- package/dist/marks/rule.d.ts +47 -0
- package/dist/marks/rule.js +83 -0
- package/dist/marks/sankey.d.ts +53 -0
- package/dist/marks/sankey.js +369 -0
- package/dist/marks/sunburst.d.ts +31 -0
- package/dist/marks/sunburst.js +186 -0
- package/dist/marks/ticks.d.ts +12 -0
- package/dist/marks/ticks.js +26 -0
- package/dist/marks/tree.d.ts +49 -0
- package/dist/marks/tree.js +113 -0
- package/dist/marks/treemap.d.ts +30 -0
- package/dist/marks/treemap.js +164 -0
- package/dist/paint.d.ts +35 -0
- package/dist/paint.js +71 -0
- package/dist/scale.d.ts +16 -0
- package/dist/scale.js +97 -0
- package/dist/scales/band.d.ts +12 -0
- package/dist/scales/band.js +27 -0
- package/dist/scales/bare.d.ts +6 -0
- package/dist/scales/bare.js +10 -0
- package/dist/scales/color.d.ts +20 -0
- package/dist/scales/color.js +21 -0
- package/dist/scales/log.d.ts +27 -0
- package/dist/scales/log.js +98 -0
- package/dist/scales/time.d.ts +37 -0
- package/dist/scales/time.js +183 -0
- package/dist/text.d.ts +8 -0
- package/dist/text.js +33 -0
- package/dist/theme.d.ts +10 -0
- package/dist/theme.js +8 -0
- package/dist/transforms.d.ts +47 -0
- package/dist/transforms.js +119 -0
- package/dist/types.d.ts +465 -0
- package/dist/types.js +1 -0
- package/package.json +31 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pavel Gusev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,7 +1,352 @@
|
|
|
1
1
|
# swiftplot
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p>
|
|
4
|
+
<picture>
|
|
5
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/GusevP/swiftplot/main/assets/logo-dark.svg" />
|
|
6
|
+
<img src="https://raw.githubusercontent.com/GusevP/swiftplot/main/assets/logo.svg" alt="swiftplot" height="64" />
|
|
7
|
+
</picture>
|
|
8
|
+
</p>
|
|
4
9
|
|
|
5
|
-
|
|
10
|
+
Small, fast charts on Canvas 2D and WebGL2. MIT, TypeScript, ESM only, no dependencies.
|
|
6
11
|
|
|
7
|
-
|
|
12
|
+
**Status: prototype.** It draws lines and areas (straight or stepped), bars, box plots, error bars, heatmaps, tick marks and scatter dots on linear, time and category axes, and pies, donuts, radars, radial bars, funnels, sankey diagrams, treemaps and sunbursts, with a hover tooltip, dim-the-others on hover and optional animation. A chart can be updated and follows its container when no size is given. `swiftplot/charts` builds each of these charts from the data alone, and bars and lines together, and `@swiftplot/react` and `@swiftplot/vue` make React and Vue components of them. There is a legend that hides and shows series, zoom and pan, a brush, a click event, the pointer from code for linked charts, light and dark themes, reference lines and zones, log axes, and tick steps and formats. The repository is a pnpm workspace with the core in `packages/core` and the wrappers in `packages/react` and `packages/vue`.
|
|
13
|
+
|
|
14
|
+
**For AI coding assistants:** [`llms-full.txt`](llms-full.txt) is a guide to writing code with swiftplot, and [`llms.txt`](llms.txt) its index.
|
|
15
|
+
|
|
16
|
+
## Charts from data
|
|
17
|
+
|
|
18
|
+
`swiftplot/charts` draws a chart from the data alone and lets every default be overridden. It is a second entry of the same package, built on the API under "Use", which it leaves unchanged.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { lineChart } from 'swiftplot/charts';
|
|
22
|
+
|
|
23
|
+
const chart = lineChart(el, { data: rows }); // fields guessed; axes, colours, tooltip and legend by default
|
|
24
|
+
chart.update({ data: next }); // only what changed reaches the chart
|
|
25
|
+
chart.destroy();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
- **Charts:** `lineChart`, `areaChart`, `barChart`, `scatterChart`, `comboChart`, `pieChart`, `funnelChart`, `radialBarChart`, `radarChart`, `heatmapChart`, `histogramChart`, `boxPlotChart`, `candlestickChart`, `sankeyChart`, `treemapChart` and `sunburstChart`. Each returns `{ update(props), destroy(), chart }`; `update` takes only the props that change, and `chart` is the underlying chart.
|
|
29
|
+
- **Data** is row objects, an object of columns (arrays or typed arrays, all of one length), a plain number array (`y` against its index, as the series `value`), or `[x, y]` pairs (the series `y`; a row of any other length throws), and for `treemapChart` and `sunburstChart` a nested `{ name, value, children }` or a list of them. Pairs are `[label, value]` on a pie, a funnel and radial bars, `[name, value]` on a radar and `[group, value]` on a box plot; a histogram takes no pairs, and a heatmap, candlesticks, a combo and a sankey take neither pairs nor numbers. On the line, area, bar and scatter charts long rows, `{ date, service, value }`, take `series: 'service'` and give one series per distinct value. `data` missing, `null` or empty draws an empty frame and never throws, so a chart can be created before its data arrives.
|
|
30
|
+
- **Values:** a number, a `Date`, an ISO 8601 string (`2026-11-22`, `2026-11-22T10:00` or with a space, optional seconds and fraction, and `Z` or `+02:00` after a time) or other text. A field's first value that is not missing (`null`, `undefined`, `''`, `NaN`, an infinite number) decides its kind; later values of another kind, an invalid `Date` and an impossible date (`2026-13-40`, `T24:00`) are missing. Numbers held as text (`"12.5"`) are text and are not converted. A field of booleans or objects cannot be plotted.
|
|
31
|
+
- **Dates** without a zone are read as UTC, so the same data draws the same chart on every machine; `xAxis: { utc: false }` reads them, and shows the axis, in local time.
|
|
32
|
+
- **Fields** of the line, area, bar and scatter charts are named by `x`, `y` (one field or a list) and `series`, by name or, on row objects only, by an accessor `(row, index) => value`. A named field is never second-guessed. What is not named is guessed, in field order (the keys of the first 100 rows, or of the columns object): `x` is the first date field, else the first text field, else the first number field when a `y` is named or another number field is left, else the row index; `y` is every number field left, one series each. A scatter chart takes the first date field, else the first number field, as `x` and the next number field as its one `y`; a named `y` may be text, for a band per category. `y2` names number fields drawn against an axis on the right, set by `y2Axis`; it is never guessed, and goes with neither `series` nor `errorBars`.
|
|
33
|
+
- **The other charts** name their fields by what they have, by name or, on row objects, by an accessor. A single field of numbers is guessed only when one is left, and otherwise the error says what to pass:
|
|
34
|
+
|
|
35
|
+
| Chart | Fields | Guessed | Other props |
|
|
36
|
+
| ------------------------------- | --------------------------------------------------------------------- | ----------------------------------------------------------------------- | ------------------------------------------- |
|
|
37
|
+
| `comboChart` | `x`, and `lines`, `bars`, `y2Lines`, `y2Bars`: a field or a list each | `x` as on a line chart; the rest never, and one is needed | `y2Axis`, `stack` |
|
|
38
|
+
| `pieChart` | `label`, `value` | the first text field; the only number field | `inner` (above 0, a donut), `text`, `sweep` |
|
|
39
|
+
| `funnelChart` | `label`, `value` | as a pie | `text`, `fill` |
|
|
40
|
+
| `radialBarChart` | `label`, `value` | as a pie | `max`, `sweep`, `inner` |
|
|
41
|
+
| `radarChart` | `x` (spokes), `y` (a shape each) | the first text field; every number field left | `max`, `fill` |
|
|
42
|
+
| `heatmapChart` | `x`, `y`, `value` | the first two text or date fields; the only number field left | `color` |
|
|
43
|
+
| `histogramChart` | `x` | the only number field | `bins` |
|
|
44
|
+
| `boxPlotChart` | `x` (groups, or `null` for one box), `y` | the only text field, when its values repeat; the only number field | `fill` |
|
|
45
|
+
| `candlestickChart` | `x`, `open`, `high`, `low`, `close` | the first date field, else text; the fields of those names, in any case | `up`, `down` |
|
|
46
|
+
| `sankeyChart` | `source`, `target`, `value` | the first two text fields; the only number field left | `blend`, `text` |
|
|
47
|
+
| `treemapChart`, `sunburstChart` | none | | `shade`, `text` |
|
|
48
|
+
|
|
49
|
+
- **Errors** name the chart; an error about fields also names the fields with their kinds and the prop to pass. On the line, area, bar and scatter charts guessing throws when no number field is left for `y` (naming any field of numbers held as text), on all but the scatter when a text field is left over without `series` or `y2` (the rows look like long data, even with `x` and `y` named), for a named field that does not exist or is not numbers where numbers are needed, and for a scatter chart without two usable fields or with more than one `y`. An update that throws leaves the chart as it was.
|
|
50
|
+
- **Rows:** line and area charts, and combos on dates or numbers, sort rows by x (stably); bar and scatter charts, and combos on text, keep the given order. On these a row with a missing x (or, on a scatter, a missing `r`) is dropped; a missing y is a gap. Pies, funnels, radial bars and radars keep a row with no name, unnamed; heatmaps, box plots and sankeys drop a row with no cell, group or node. A heatmap puts text in order of first appearance and dates and numbers ascending.
|
|
51
|
+
- **Axes:** settings go under `xAxis` and `yAxis`. Which x settings apply depends on the x field:
|
|
52
|
+
|
|
53
|
+
| x | axis | settings |
|
|
54
|
+
| ----------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------- |
|
|
55
|
+
| numbers, row index | linear | `label`, `domain` (`'nice'` by default), `step`, `format`, `fit` |
|
|
56
|
+
| dates | time | `label`, `utc`, `unit` and `every`, `format`, `fit`, `domain` as `[from, to]` in milliseconds only |
|
|
57
|
+
| text, and every bar chart, box plot and heatmap | category | `label` only |
|
|
58
|
+
|
|
59
|
+
`yAxis` takes `label`, `domain`, `step`, `format` and `fit` (a heatmap's `label` only), `y2Axis` the same, and on line, scatter and combo charts `scale: 'log'`, where a value at or below zero is a gap, `step` does not apply and bars cannot share the axis. A combo or a candlestick chart puts text in categories; on dates and numbers a bar or a candle takes 0.6 of the smallest gap between x values. Pies, donuts, radars, radial bars, funnels, sankeys, treemaps and sunbursts have no axes, and only the charts that have them zoom, box plots and heatmaps excepted. The bars of `barChart` put each row (or, with `series`, each distinct x in order of first appearance) in a category of its own, dates included, labelled in UTC whatever `utc` says.
|
|
60
|
+
|
|
61
|
+
- **Defaults:**
|
|
62
|
+
|
|
63
|
+
| Thing | Default | Override |
|
|
64
|
+
| ------------------- | ------------------------------------------------------------------------- | -------------------------------------- |
|
|
65
|
+
| Axis labels | the field's name; none for the row index, an accessor or several y fields | `xAxis.label`, `yAxis.label` |
|
|
66
|
+
| Colours | `palette` in order, `paletteDark` on the dark theme | `colors` |
|
|
67
|
+
| Tooltip | on; text in the y axis format, else two decimals at most | `tooltip: false` |
|
|
68
|
+
| Legend | on with two or more series or named parts; a heatmap's strip of colours | `legend: false` or `legend()` options |
|
|
69
|
+
| Zoom | off | `zoom: true` or `zoom()` options |
|
|
70
|
+
| Animation | off | `animate: true` or `animate()` options |
|
|
71
|
+
| Theme | follows the page | `theme: 'light' \| 'dark' \| Theme` |
|
|
72
|
+
| Size | follows the container | `width`, `height`, `margin` |
|
|
73
|
+
| Several y on areas | overlapping, half transparent | `stack: true` |
|
|
74
|
+
| Several y on bars | side by side | `stack: true` |
|
|
75
|
+
| Line and area shape | straight, with gaps | `curve`, `join` |
|
|
76
|
+
| Scatter radius | 4 px | `r`: a number or a field |
|
|
77
|
+
|
|
78
|
+
- **Marks added to a chart:** line, area, bar, scatter and combo charts take `rules` (`{ x }` or `{ y }`, with `text`, `dashed` and `color`), `zones` (`{ x: [from, to] }` or `{ y: [from, to] }`, with `text` and `color`) and `brush` (`true` or `{ axes }`; with `zoom` its release shows the range), whose range goes to `onBrush`. `errorBars: { low, high, cap, width }` on line, bar and scatter charts names two fields that are values on the y axis, not distances from y, for a chart of one unstacked `y`. A line takes `fill` (`true` or a gradient) and a scatter `alpha`, `ring` and `shape: 'tick'`.
|
|
79
|
+
- **Stacking** counts a missing value as zero, so a stacked area has no gap where an unstacked one would. Long series are stacked over the union of their x values.
|
|
80
|
+
- **Interaction state** (`view`, `hidden`, `selected`, `pointer`) is kept by the chart unless the prop is given; `onViewChange`, `onHiddenChange`, `onClick` and `onPointerChange` report it as in the explicit API. Changing `tooltip`, `legend`, `zoom`, `animate` or `brush` creates the chart again and keeps a zoomed window and hidden series.
|
|
81
|
+
- **`dataVersion`:** conversion is cached on the data's identity and length (each column's, for an object of columns). Pass new data, or after changing values in place, bump `dataVersion`: `chart.update({ dataVersion: ++version })`.
|
|
82
|
+
- **Theme:** `theme` defaults to `'auto'`, which follows the computed CSS `color-scheme` of the chart's element: `dark` gives the dark theme, `light dark` follows the system setting and switches with it, anything else gives light. It is read when the chart is created, when the system setting changes and when the `theme` prop changes.
|
|
83
|
+
- **`.spec()`** is the way out to the explicit API: `lineChart.spec({ data: rows, x: 'date', y: ['api', 'web'] })` returns the `plot()` options the chart would use. It needs no element and runs in Node; with the `'auto'` theme it gives no theme and the light palette.
|
|
84
|
+
- **`mount(el, options)`** is `plot()` behind the same controller, for the explicit API in plain JavaScript: it takes the `plot()` options and returns `{ update(options), destroy(), chart }`. An update sends the core only the options that changed (marks compared by key, type and options), and leaves `view`, `hidden`, `selected` and `pointer` alone unless given. `use` is read once; `theme` is `light` unless given. `equal(a, b)`, which mount uses for that comparison, compares plain objects and arrays by value (cycles included) and anything else (typed arrays, functions, `Date`s) by identity; `plain(v)` tells whether `v` is a plain object or array.
|
|
85
|
+
|
|
86
|
+
## React and Vue
|
|
87
|
+
|
|
88
|
+
`@swiftplot/react` (React 18.3 or 19) and `@swiftplot/vue` (Vue 3.4 or later) make a component of each chart above, named as its function with a capital: `LineChart`, `AreaChart`, `BarChart`, `ScatterChart`, `ComboChart`, `PieChart`, `FunnelChart`, `RadialBarChart`, `RadarChart`, `HeatmapChart`, `HistogramChart`, `BoxPlotChart`, `CandlestickChart`, `SankeyChart`, `TreemapChart` and `SunburstChart`, and `Plot` for the explicit API. Each takes the props of its function, renders one `div`, creates the chart after mount, sends it only what changed on each render and destroys it on unmount. Install `swiftplot` beside the wrapper, which has it as a peer dependency:
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
pnpm add swiftplot @swiftplot/react # or @swiftplot/vue
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
import { LineChart } from '@swiftplot/react';
|
|
96
|
+
|
|
97
|
+
<LineChart data={rows} />;
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```vue
|
|
101
|
+
<script setup lang="ts">
|
|
102
|
+
import { LineChart } from '@swiftplot/vue';
|
|
103
|
+
</script>
|
|
104
|
+
|
|
105
|
+
<template>
|
|
106
|
+
<LineChart :data="rows" />
|
|
107
|
+
</template>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
- **Props** are the chart function's, checked against the data's type in JSX and in Vue templates (`y="nope"` on rows without that field is a type error). `createElement` and `h()` do not infer the data's type: give it, `h(LineChart<Row[]>, props)`, and the field names are checked as in a template. `className` (React) or `class` (Vue), `style` and `id` go to the `div`.
|
|
111
|
+
- **State** (`view`, `hidden`, `selected`, `pointer`) is kept by the chart unless the prop is given. To control the zoom window, pass `view` and set it from the change:
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
const [view, setView] = useState<{ x?: [number, number]; y?: [number, number] }>();
|
|
115
|
+
<LineChart data={rows} x="date" y={['api', 'web']} zoom view={view} onViewChange={setView} />;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
In Vue the changes are events: `v-model:view`, `v-model:hidden` and `v-model:pointer`, `@click`, `@brush` and `@error` (`<LineChart :data="rows" zoom v-model:view="view" />`). `selected` is a plain prop: the app sets it, from a click for example.
|
|
119
|
+
|
|
120
|
+
- **`Plot`** takes every `plot()` option as a prop, to mix marks in a way no easy chart does, for a mark of your own, or to pass large typed arrays straight in. `use` is read when it mounts, as in `plot()`: change the component's `key` to change features. Its theme is `light` unless given; it does not follow the page as the easy charts do.
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
import { band, bands, bar } from 'swiftplot';
|
|
124
|
+
import { Plot } from '@swiftplot/react';
|
|
125
|
+
|
|
126
|
+
const days = ['Mon', 'Tue', 'Wed'];
|
|
127
|
+
const [y1, y2] = bands(days.length);
|
|
128
|
+
const marks = [bar({ y1, y2, x: [12, 19, 7], color: '#2563eb', label: 'Orders' })];
|
|
129
|
+
<Plot x={{ domain: 'nice', label: 'Orders' }} y={band({ label: 'Day', categories: days })} marks={marks} />;
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
- **Size:** `width` and `height` (px) size the `div`, and `style` wins over them; the chart measures the `div` and follows whatever resizes it (a class, a parent, a media query). With no height from `height`, `style` or CSS, the `div` fills a parent that has a height, else is 300 px tall, so `<LineChart data={rows} />` shows a chart on an ordinary page and a class added later wins, in a cascade layer (Tailwind) or not. The fallback is a `<style>` kept first in the document head (or the shadow root), inserted only for a chart with no height: under a Content Security Policy that blocks inline styles, give every chart a height (`height`, `style` or CSS). A chart mounted in a detached element falls back once the element is attached.
|
|
133
|
+
- **Errors** (a missing field, an accessor or formatter that throws) go to `onError` in React or `@error` in Vue, and the last good chart stays; without a handler they are thrown to the nearest error boundary, or to Vue's error handling (`onErrorCaptured`, `app.config.errorHandler`). A failed render is tried again when a chart prop other than a callback changes; a changed callback still reaches the last good chart. A failure is reported once until its error message, the data's length (each column's, for an object of columns) or another prop that is not a function changes, so inline props, data rebuilt during render and a handler that sets state cannot loop, unless the message itself changes on every attempt. Rows are never read, so new data of the same length that fails with the same message is not reported again.
|
|
134
|
+
- **Server rendering:** the server renders the `div` alone, the client hydrates the same markup and creates the chart after hydration. The React entry is marked `'use client'`. The 300 px fallback is applied in the browser, so give a server-rendered chart a `height` (or a CSS height) to reserve its space before hydration.
|
|
135
|
+
- **Performance:** each render compares props by identity and the chart then by value, so inline objects such as `xAxis={{ label: 'Day' }}` cost little. `data` built during render and inline accessors are converted again on every render, and an inline `xAxis.format` repaints the data: build them outside render, or with `useMemo` and `useCallback`. In Vue keep the data in a `shallowRef` and replace the array: a deep `ref` makes the app wrap every row for nothing the chart uses. Data changed in place with every prop unchanged is not noticed: pass a new array or bump `dataVersion`.
|
|
136
|
+
|
|
137
|
+
## Use
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
import { dots, plot, tooltip } from 'swiftplot';
|
|
141
|
+
|
|
142
|
+
const chart = plot(document.getElementById('chart')!, {
|
|
143
|
+
width: 900,
|
|
144
|
+
height: 600,
|
|
145
|
+
x: { domain: 'nice', label: 'X' },
|
|
146
|
+
y: { domain: 'nice', label: 'Y' },
|
|
147
|
+
marks: [dots({ x, y, r, color: '#2563eb', alpha: 0.7, label: 'Group 1' })],
|
|
148
|
+
use: [tooltip({ mode: 'nearest', text: (label, x, y) => `${label}: (${x.toFixed(1)}, ${y.toFixed(1)})` })],
|
|
149
|
+
dim: 0.3,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
chart.update({ marks: [dots({ x, y: y2, r, color: '#2563eb', label: 'Group 1' })] });
|
|
153
|
+
chart.destroy();
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
- **Marks** are imported functions: `line`, `area`, `bar`, `box`, `candlestick`, `errorBars`, `heatmap`, `ticks`, `dots`, `arcs`, `radar`, `radialBars`, `funnel`, `sankey`, `treemap`, `sunburst`. Only the ones you import are bundled.
|
|
157
|
+
- **Lines and areas** are straight between points unless `curve: 'step'` or `curve: 'smooth'`, which curves through the points without overshooting them. `area({ x, y })` fills from zero; `area({ x, y1, y2 })` is a band. A `y` that is `NaN` or infinite leaves a gap: nothing is joined across it, and a lone point between two gaps draws nothing; `join: true` draws a line or fills an area straight across instead. `x` must be finite and ascending.
|
|
158
|
+
- **Bars** have explicit bounds, `bar({ x1, x2, y1, y2 })`, so vertical, horizontal, grouped and stacked bars are the same mark. A single `y` (or `x`) starts at zero. On a category axis `const [x1, x2] = bands(7)` gives the bounds. Grouped bars are `bar({ x, y, dodge: 'years' })` for each series, `x` being the category's place: the chart gives the shown series of that id a slot each, in the order listed, so hiding one widens the others. With `dodge` or `stack`, `width` is how much of x the bars of one place take together, 0.8 by default. `bands(7, { slot: 0, of: 2 })` gives one series' bounds for a chart that never hides it.
|
|
159
|
+
- **Box plots** take the five numbers as columns: `box({ x, ...fiveNumbers(groups), color })`. The whiskers go to the minimum and maximum; leave outliers out and draw them with `dots`.
|
|
160
|
+
- **Heatmaps** are `heatmap({ values, columns, color: sequential({ stops }) })`, with cell (c, r) centred on x = c, y = r so it sits on band axes; `x` and `y` give the grid's outer edges instead. Create the colour scale once, outside the update, or every update repaints the grid.
|
|
161
|
+
- **Pies and donuts** are `plot(el, { frame: 'polar', marks: [arcs({ values, colors, labels, inner: 0.6, text: true })] })`. A polar frame has no axes; slices start at twelve o'clock and run clockwise. `labels` give the legend one entry per slice, and `text` writes each slice's share on it where there is room. With `animate()` a pie is revealed by a sweep round the clock.
|
|
162
|
+
- **Radars** are `plot(el, { frame: 'polar', marks: radar({ axes, series: [{ values, color, label }] }) })`. `radar()` returns several marks: the rings and spokes, then one per series, so series are named in a legend and dimmed like lines, and a hit's row is the spoke. The rim is `max`, or the largest value rounded up.
|
|
163
|
+
- **Radial bars** are `plot(el, { frame: 'polar', marks: [radialBars({ values, max, colors, labels })] })`: one bar per value on its own ring, from twelve o'clock clockwise. Three quarters of a turn stand for `max`; the last quarter holds the names.
|
|
164
|
+
- **Treemaps and sunbursts** take a tree in columns, `{ nodes, parent, value }`, or `fromTree()` of nested `{ name, value, children }` objects: `plot(el, { frame: 'blank', marks: [treemap({ ...fromTree(tree), colors })] })` and `plot(el, { frame: 'polar', marks: [sunburst({ ...fromTree(tree), colors })] })`. Only the values of nodes without children count. Each root gets a colour, or each child of a single root. A treemap draws the leaves; a sunburst draws every node, a single root as a disc in the middle.
|
|
165
|
+
- **Funnels** are `plot(el, { frame: 'blank', marks: [funnel({ values, colors, labels, text: true })] })`: equally tall stages, each as wide as its value against the largest.
|
|
166
|
+
- **Sankey diagrams** are `sankey({ ...fromLinks(links), colors })` in a blank frame, where `links` is a list of `{ source, target, value }`. A cycle, a link to itself or a link to an unknown node throws. A hit's row is the node, or `nodes.length` plus the link.
|
|
167
|
+
- **Labels on shapes** (slices, stages, nodes) are drawn where they fit and left out where they would overlap or run off the chart.
|
|
168
|
+
- **A colour legend** for a heatmap comes with `legend()`: a strip of its colours between the ends of its domain.
|
|
169
|
+
- **Error bars** are `errorBars({ x, y1, y2 })`, or `{ y, x1, x2 }` lying down.
|
|
170
|
+
- **Candlesticks** are `candlestick({ x, open, high, low, close, color, up, down })`: a wick from low to high and a body from open to close, in `up` where the close is not below the open and in `down` where it is. `width` is the body's width in x units, 0.6 by default. A pointer anywhere on a candle hits it; a `nearest` tooltip gets the row, to read the four values from your own columns.
|
|
171
|
+
- **Colours:** `palette` is eight series colours to give out in order, and `brand` the cyan-to-violet blend. `gradient(from, to, 'down' | 'up' | 'right' | 'left')` makes a blend of two colours; a number stands for the mark's own colour at that opacity. `line({ fill: true })` puts a faint fade of the line's colour under it. A blend goes in `area({ fill })`, `bar({ fill })`, `line({ stroke })` and `line({ fill })`, so `area({ x, y, color, fill: gradient(0.4, 0) })` fades an area out towards its baseline. `color` stays the one colour a legend and a tooltip show.
|
|
172
|
+
- **Blends on other charts:** `sankey({ blend: true })` runs each ribbon from its source's colour into its target's; `funnel({ fill: gradient(a, b) })` puts one blend down the whole funnel; `arcs({ sweep: 0.8 })` and `radialBars({ sweep: 0.35 })` start each slice or bar at that opacity and bring it to full colour at its end; `treemap({ shade: 0.7 })` starts each branch at that opacity at its top, and `sunburst({ shade: 0.7 })` ends each at that opacity at the rim.
|
|
173
|
+
- **Box plots** take `radius`, `medianWidth`, `whiskers` (their opacity) and `fill`: `box({ ..., radius: 4, medianWidth: 2.5, whiskers: 0.5, fill: gradient(0.4, 0.1) })`.
|
|
174
|
+
- **Rounded bars and ringed points:** `bar({ radius: 6 })` rounds the value end of a bar that starts at zero, or all corners of one that floats, and `bar({ gap: 2 })` sets touching bars such as a histogram's two pixels apart; `dots({ ring: 2 })` draws rings round a white middle.
|
|
175
|
+
- **No axes:** `x: bare()` and `y: bare([0, 100])` place the marks and draw no ticks, grid or label, for a chart inside a card.
|
|
176
|
+
- **Rows** need no helper: `Float64Array.from(rows, r => r.value)` makes a column and `Map.groupBy(rows, r => r.series)` splits rows into series.
|
|
177
|
+
- **Ticks** thin out to fit the axis, so a narrow chart shows fewer labels, not overlapping ones.
|
|
178
|
+
- **Accessibility:** the chart is exposed as an image with a label built from the axis labels and series names; set `description` to say what it shows. The data itself is not exposed yet.
|
|
179
|
+
- **Margins** are sized from the tick labels; pass `margin: { left: 80 }` to set a side yourself.
|
|
180
|
+
- **Legend** is a feature you import: `use: [legend()]` lists each labelled series above the plot, highlights it on hover, and hides or shows it on a click, a tap, Enter or Space (`legend({ toggle: false })` turns that off). A label wider than the plot ends in an ellipsis. Rows are capped at what fits in a third of the chart's height, or at `legend({ rows: 2 })`, and the entries left over go behind "+N more".
|
|
181
|
+
- **Zoom and pan** is a feature you import: `use: [zoom()]`. Ctrl or cmd and the wheel (a trackpad pinch) zoom about the pointer, a drag pans, and a double click or double tap shows the whole domain again; on a touch screen two fingers pinch and pan and one scrolls the page. `zoom({ axes: 'xy', wheel: 'plain', drag: 'shift' })` zooms both axes, zooms on a plain wheel and pans only with shift held. A mouse drag along both axes pans x alone until it has moved 8 px up or down, so panning level keeps an axis with `fit: 'window'` fitting; panning up or down, or zooming y, gives both y axes a window of their own, which stops the fitting. Two fingers on a touch screen have no such threshold. Over the plot a ctrl or cmd wheel never reaches the page unless `wheel: false`, and with `wheel: 'plain'` a plain one reaches it only when the view cannot move further. The window is the `view` option, `{ x?: [lo, hi], y?: [lo, hi] }` in axis values, shown clamped to the domain; `onViewChange` reports it after a gesture, never for `update()`. `y: { domain: 'nice', label, fit: 'window' }` fits y to what the x window shows.
|
|
182
|
+
- **A brush** reports a range: `use: [brush({ onSelect: range => ... })]`. A drag draws a rectangle and `onSelect` gets `{ x, y }` in axis values on release, then `null` once it is cleared; `brush({ zoom: true })` shows the range brushed. With a brush beside `zoom()`, a plain drag brushes and shift and a drag pans. Mouse and pen only.
|
|
183
|
+
- **Hidden series:** `hidden: ['Group 1']` leaves out the series a legend lists under that id (a mark's `group`, or its `label`), from the start or from code, and `onHiddenChange(hidden)` reports a change the legend made. An automatic domain fits what is shown; `fit: 'all'` on an axis holds it still. The chart restacks layers given a `stack` id and re-dodges bars given a `dodge` id; the layers `stack()` returns are computed by the app, so it restacks in `onHiddenChange`.
|
|
184
|
+
- **Clicks and the pointer:** `onClick(hit, at)` gets what is within 20 px of a click on the plot, or `null`, and the axis values at the click; a click that ends a drag is not one. `selected: [{ id: 'Group 1', rows: [3] }]` picks rows out. `pointer: { x, y }` shows a tooltip as if the user's pointer were there, and `onPointerChange` reports the user's, with `null` as it leaves, so two charts can follow each other:
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
const charts = [a, b].map((el, i) =>
|
|
188
|
+
plot(el, {
|
|
189
|
+
...options,
|
|
190
|
+
onPointerChange: p => charts[1 - i]!.update({ pointer: p }),
|
|
191
|
+
use: [tooltip(tip)],
|
|
192
|
+
}),
|
|
193
|
+
);
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
- **Themes:** `theme: dark` colours the text, grid, tick marks and tooltip for a dark page; `light` is the default. The core never guesses dark mode: the app switches it, and `paletteDark` gives series colours for a dark surface. The charts of `swiftplot/charts` follow the page's `color-scheme` instead.
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
const scheme = matchMedia('(prefers-color-scheme: dark)');
|
|
200
|
+
const chart = plot(el, { ...options, theme: scheme.matches ? dark : light });
|
|
201
|
+
scheme.addEventListener('change', e => chart.update({ theme: e.matches ? dark : light }));
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
- **Reference lines and zones:** `rule({ y: 50, color: '#dc2626', dashed: true, text: 'Target' })` draws a line across the plot at each value, and `zone({ x1, x2, color, text })` a band between two values. A rule is always above the data and a zone under it, wherever they are in `marks`; neither widens a domain or counts as a series.
|
|
205
|
+
- **Tick step and format:** an axis's `step` puts ticks on multiples of it, thinned when their labels do not fit, and `format` labels them; `time({ label, every: 3, unit: 'month', format })` does the same on a time axis. Define `format` once, outside the update: a new function each time counts as a changed axis, so its ticks are worked out again and the data is repainted, which `animate()` shows as a cross-fade.
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
const percent = (v: number) => `${v * 100}%`;
|
|
209
|
+
chart.update({ y: { domain: 'nice', label: 'Share', step: 0.25, format: percent } });
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
- **Log axes:** `const y = log({ label: 'Count' })` (`base` defaults to 10, `domain` takes real values above zero). Marks take their columns through it, `line({ x, y: y.col(counts), color })`, and a value at or below zero becomes a gap. Everything the app is given or gives (`view`, `pointer`, callbacks, tooltip text) is a real value; a rule or zone takes `y.to(v, y)`. Bars need both ends given, `bar({ x1, x2, y1: y.col(lows), y2: y.col(highs), color })` with `[x1, x2]` from `bands()`, since zero has no place on a log axis, and nothing is stacked on one.
|
|
213
|
+
- **Histograms and stacks** are plain functions feeding the marks: `bar({ ...bin(values, 20), color })`, and `area({ x, y, stack: 'traffic' })` or `bar({ x, y, stack: 'traffic' })` for each series stacks the shown series of that id in the order listed, negative values downwards, so hiding one closes the gap; a tooltip and `onClick` report a layer's own value. `stack([a, b, c])` gives each series the `y1` and `y2` of its layer once, for a chart that never hides one. A `NaN` in a layer counts as zero: that layer has no thickness there.
|
|
214
|
+
- **Hover** highlights the series under the pointer and dims the others (`dim`). Where a chart has only one series, the hovered bar is picked out from the other bars instead.
|
|
215
|
+
- **Tooltips** are `tooltip({ mode: 'nearest', text })`, for what is nearest the pointer, or `tooltip({ mode: 'x', title, value })`, for every labelled line and area at its x; there `snap: x => other` reads them at another x, such as the nearest row's.
|
|
216
|
+
- **Touch:** a tap shows the tooltip, and it stays after the finger lifts until a tap elsewhere or the next update or resize of the chart, which lets it go. A pen behaves as a mouse. Without `zoom()` the chart adds no listener that blocks the page from scrolling; with it the chart takes `touch-action: pan-x pan-y`, so one finger still scrolls the page.
|
|
217
|
+
- **Animation** is a feature you import: `use: [animate({ duration: 300 })]`. Marks are revealed from the left on the first paint (`enter: 'fade'` fades them in instead). When the data changes, bars move to their new values, row for row, and everything else cross-fades. It is skipped for a resize, with reduced motion, and while updates arrive faster than the duration.
|
|
218
|
+
- **A second y axis:** `y2: { domain, label }` draws an axis on the right, and a mark given `yAxis: 'y2'` is drawn against it: `marks: [bar({ x, y: revenue, color }), line({ x, y: margin, color, yAxis: 'y2' })]`. Each axis takes its extent from its own marks, grid lines come from `y` only, and there are exactly two. A zoom or a pan on y moves `y2` through the same pixels; `view`, `onViewChange`, the click's `at`, `pointer`, `onPointerChange` and a brush's range carry `y2`, and an `x` tooltip's `value(v, axis)` is told which axis a value is on. A y `rule` or `zone` takes `yAxis` too. Bars or areas stacked under one name on the two axes are two stacks; dodged bars share their slots whichever axis they are on.
|
|
219
|
+
- **Axes** are linear by default: `{ domain: 'nice' | 'data' | [lo, hi], label }`. `time({ label })` takes millisecond timestamps and puts ticks on calendar boundaries, in UTC unless `utc: false`. `log({ label })` is a log axis. `step` and `format` set the ticks, and `fit` which marks an automatic domain is fitted to. `band({ label, categories })` puts category `i` at `x = i`, owning `i - 0.5` to `i + 0.5`.
|
|
220
|
+
- **Columns** (`x`, `y`, `r`) are typed arrays or plain number arrays, one set per mark.
|
|
221
|
+
- **Size**: leave out `width` or `height` and the chart follows its container, which must then have a size of its own (a container with no height gives an empty chart, which draws once the container gets a height).
|
|
222
|
+
- **Updates**: a mark whose options are unchanged keeps its geometry, so passing a fresh `marks` array is cheap. Columns are compared by identity: after writing new values into the same array, pass a new array or view of it (`x.subarray(0)`).
|
|
223
|
+
- **Without a DOM:** the package imports in Node, and marks and transforms such as `bin` and `stack` run there; `plot()` needs a browser.
|
|
224
|
+
- **Your own mark** is an object with the `Mark` shape from the package's types.
|
|
225
|
+
|
|
226
|
+
## Why it is fast
|
|
227
|
+
|
|
228
|
+
Each point comes from profiling uPlot, the fastest of thirteen libraries in [chart-bench](https://github.com/gusevp/chart-bench), and timing the alternative.
|
|
229
|
+
|
|
230
|
+
- **Axis text is DOM, not canvas text.** uPlot's axis drawing cost 17–21 ms at 4× CPU slowdown at every data size.
|
|
231
|
+
- **Each mark has its own x column.** No aligned table, so no join step.
|
|
232
|
+
- **Step lines and bands are reduced to one min/max per pixel column** before the path is built.
|
|
233
|
+
- **Typed-array columns in, scale maths inlined** in the draw loops.
|
|
234
|
+
- **Scatter switches to WebGL2 at 20,000 points, bars, error bars, box plots and candlesticks at 10,000, and a sankey at 1,000 nodes and links together.** WebGL2 set-up costs about 4 ms, so a scatter on Canvas 2D mounts sooner below roughly 20,000 points. When the browser takes a WebGL context away, as it does with many WebGL charts on one page, that chart falls back to Canvas 2D.
|
|
235
|
+
- **Paths and GPU buffers are kept,** so a hover repaint never touches the data again.
|
|
236
|
+
|
|
237
|
+
## Measured
|
|
238
|
+
|
|
239
|
+
Mount time in chart-bench, median ms, 2026-11-24, Apple M3 Pro that was not idle. Each cell is the mean of two runs of nine loads, each load a new page, with the two libraries taking turns to go first. 4× is Chrome's CPU slowdown. At native speed the two smaller sizes sit at the empty-page floor (about 27 ms), so only the other rows and columns separate the libraries.
|
|
240
|
+
|
|
241
|
+
| case | swiftplot | uPlot | swiftplot, 4× | uPlot, 4× |
|
|
242
|
+
| ------------------------ | --------: | ----: | ------------: | --------: |
|
|
243
|
+
| survival, 3 × 200 points | 26.8 | 28.8 | 47.9 | 58.3 |
|
|
244
|
+
| survival, 6 × 1,000 | 29.4 | 22.1 | 49.6 | 71.1 |
|
|
245
|
+
| survival, 10 × 10,000 | 33.1 | 55.2 | 86.0 | 218.2 |
|
|
246
|
+
| scatter, 500 | 26.2 | 26.1 | 37.7 | 60.0 |
|
|
247
|
+
| scatter, 10,000 | 25.3 | 25.8 | 52.0 | 66.5 |
|
|
248
|
+
| scatter, 100,000 | 32.2 | 34.9 | 68.0 | 137.5 |
|
|
249
|
+
|
|
250
|
+
Time until the chart's own code has finished, which leaves out the wait for the next frame:
|
|
251
|
+
|
|
252
|
+
| case | swiftplot | uPlot | swiftplot, 4× | uPlot, 4× |
|
|
253
|
+
| ------------------------ | --------: | ----: | ------------: | --------: |
|
|
254
|
+
| survival, 3 × 200 points | 5.1 | 9.4 | 25.5 | 45.1 |
|
|
255
|
+
| survival, 6 × 1,000 | 8.0 | 13.2 | 30.9 | 58.7 |
|
|
256
|
+
| survival, 10 × 10,000 | 19.6 | 51.3 | 66.9 | 200.9 |
|
|
257
|
+
| scatter, 500 | 4.3 | 8.3 | 19.2 | 45.9 |
|
|
258
|
+
| scatter, 10,000 | 6.8 | 11.8 | 31.4 | 51.9 |
|
|
259
|
+
| scatter, 100,000 | 14.2 | 29.3 | 48.8 | 119.1 |
|
|
260
|
+
|
|
261
|
+
- Every load is the first chart on a new page, so each cell includes the browser parsing the library's code for the first time. A second chart on the same page does not pay that.
|
|
262
|
+
- The 100k scatter runs on WebGL, where the draw call returns before the GPU finishes, so its numbers flatter swiftplot.
|
|
263
|
+
- uPlot has no built-in scatter, so its scatter numbers include circle-drawing code written for the benchmark.
|
|
264
|
+
- Only these two chart types are in the benchmark. Bars, areas, the time axis, the legend and animation are not measured against uPlot.
|
|
265
|
+
- uPlot ships far more features in the 23.9 KB gzip it adds to the benchmark page (plus a stylesheet); swiftplot adds 15.8 KB, with its tooltip and animation.
|
|
266
|
+
|
|
267
|
+
Script time of one pan step, `propose()` of a window moved by a hundredth of its width (at the whole domain, one narrowed by a thousandth), median ms of 50, on an 800 × 400 chart, 2026-11-22, Apple M3 Pro, Chrome on Metal. Before → after is the remedy of the same date: a line or an area reads only the rows its window shows, and points on the canvas skip those that cannot reach the plot.
|
|
268
|
+
|
|
269
|
+
| case | 100,000 | 1,000,000 |
|
|
270
|
+
| ------------------ | ---------: | ----------: |
|
|
271
|
+
| line, whole | 0.9 | 5.1 |
|
|
272
|
+
| line, 10× | 0.2 | 0.8 |
|
|
273
|
+
| line, 5000× | 3.5 → 0.1 | 40.0 → 0.1 |
|
|
274
|
+
| area, whole | 0.8 | 5.0 |
|
|
275
|
+
| area, 10× | 0.2 | 0.8 |
|
|
276
|
+
| area, 5000× | 5.0 → 0.0 | 55.8 → 0.1 |
|
|
277
|
+
| filled line, whole | 0.9 | 5.0 |
|
|
278
|
+
| filled line, 10× | 0.2 | 0.9 |
|
|
279
|
+
| filled line, 5000× | 0.1 | 0.1 |
|
|
280
|
+
| filled area, whole | 0.8 | 4.9 |
|
|
281
|
+
| filled area, 10× | 0.2 | 0.8 |
|
|
282
|
+
| filled area, 5000× | 0.0 | 0.1 |
|
|
283
|
+
| bars, whole | 0.1 | 0.0 |
|
|
284
|
+
| bars, 10× | 0.0 | 0.1 |
|
|
285
|
+
| bars, 5000× | 4.9 | 61.0 |
|
|
286
|
+
| points, whole | 0.1 | 0.0 |
|
|
287
|
+
| points, 10× | 0.0 | 0.0 |
|
|
288
|
+
| points, 5000× | 13.3 → 0.2 | 156.9 → 1.9 |
|
|
289
|
+
|
|
290
|
+
- Bars of 10,000 rows or more and points of 20,000 or more are drawn by WebGL, whose draw call returns before the GPU finishes, so their whole and 10× cells leave the drawing out. Magnified past 4096 times every mark is drawn on the canvas.
|
|
291
|
+
- A million bars at 5000× is over the 16 ms budget: the canvas still walks every bar.
|
|
292
|
+
|
|
293
|
+
The same step at 1,000,000 points with `fit: 'window'` on the y axis, 2026-11-22, beside the step without it in the same run (mean of two runs each). At the whole domain two windows narrowed by a thousandth and by two take turns, since one window repeated would be fitted once and kept.
|
|
294
|
+
|
|
295
|
+
| case | without | with fit |
|
|
296
|
+
| ------------- | ------: | -------: |
|
|
297
|
+
| line, whole | 4.8 | 7.0 |
|
|
298
|
+
| line, 10× | 0.8 | 1.3 |
|
|
299
|
+
| line, 5000× | 0.1 | 0.1 |
|
|
300
|
+
| area, whole | 4.8 | 7.0 |
|
|
301
|
+
| area, 10× | 0.9 | 1.3 |
|
|
302
|
+
| area, 5000× | 0.1 | 0.1 |
|
|
303
|
+
| bars, whole | 0.0 | 8.4 |
|
|
304
|
+
| bars, 10× | 0.0 | 5.2 |
|
|
305
|
+
| bars, 5000× | 55.4 | 63.8 |
|
|
306
|
+
| points, whole | 0.0 | 7.4 |
|
|
307
|
+
| points, 10× | 0.1 | 3.8 |
|
|
308
|
+
| points, 5000× | 1.9 | 5.0 |
|
|
309
|
+
|
|
310
|
+
- A line or an area finds the rows in its window by binary search, so the fit costs the rows the window holds. Bars and points need not be in x order, so the fit reads every row.
|
|
311
|
+
|
|
312
|
+
## Develop
|
|
313
|
+
|
|
314
|
+
From the repository root. `build`, `test` and `typecheck` run in every package under `packages/` and in the site (`site/`), `test:import` in the packages only. GitHub runs these checks, then packs the packages and checks the tarballs, on every push to `main` and every pull request (`.github/workflows/ci.yml`, read-only):
|
|
315
|
+
|
|
316
|
+
```sh
|
|
317
|
+
pnpm install
|
|
318
|
+
pnpm --filter swiftplot exec playwright install chromium # once, for the tests
|
|
319
|
+
pnpm build
|
|
320
|
+
pnpm test:import # imports each package's dist/ in Node, after pnpm build
|
|
321
|
+
pnpm test:smoke # Playwright on the built site, after pnpm build
|
|
322
|
+
pnpm test # Vitest (in a real browser for the packages), one package at a time, then node --test on scripts/
|
|
323
|
+
pnpm typecheck
|
|
324
|
+
pnpm --filter swiftplot run test # one package
|
|
325
|
+
pnpm lint # oxlint, type-aware
|
|
326
|
+
pnpm format # oxfmt
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
The site is the Astro project in `site/`. `pnpm --filter swiftplot-site dev` serves it while you edit; `pnpm --filter swiftplot-site build`, then `pnpm --filter swiftplot-site exec astro preview`, serves the build, with search. `pnpm test:smoke` needs that build in `site/dist` and the Chromium installed above, and starts the preview itself. The site's other commands and its Cloudflare Pages settings are in [site/README.md](site/README.md).
|
|
330
|
+
|
|
331
|
+
## Releasing
|
|
332
|
+
|
|
333
|
+
Every package has the same version, and one release is one tag `vX.Y.Z` and one GitHub Release with the three tarballs and `SHA256SUMS`. The changelog is `CHANGELOG.md`: work adds its entries under `## Unreleased`, written by hand. Two commands, run on your machine with your own `git`, `gh` and `npm` logins:
|
|
334
|
+
|
|
335
|
+
```sh
|
|
336
|
+
pnpm release:prepare <version | patch | minor | major> # one local commit: versions and changelog
|
|
337
|
+
pnpm release [--npm] [--dry-run] # releases the commit on main
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
`release:prepare` needs a clean tree, a version higher than the current one and than every `v` tag on `origin`, and entries under `## Unreleased`. It sets the version in every package, turns `## Unreleased` into `## X.Y.Z - date` and commits `release: vX.Y.Z`. It does not push or tag: get the commit onto `main` as usual and let its checks run. A version with a hyphen (`0.1.0-rc.0`) is a pre-release.
|
|
341
|
+
|
|
342
|
+
`pnpm release` takes the version from the packages and, when the tag does not exist yet, requires a clean tree, `HEAD` equal to `main` on `origin`, a version higher than every tag and changelog entries for it. It waits for the `ci.yml` push run on `main` for that commit and requires it and its `check` and `pack` jobs to have passed (a pull-request run does not count). It builds the tarballs from a clean export of the commit, checks them and keeps them in `.release/<version>/`. It then shows the commit, each file with its size and SHA-256, and the notes (the changelog section), and asks you to type the version. Only then does it push the tag to `origin` and create, upload to and publish the GitHub Release. A published release is never changed: one that differs from what was confirmed stops the command, naming what differs.
|
|
343
|
+
|
|
344
|
+
If it stops, fix the cause and run it again: each step checks what is already done. Once the tag exists, a run continues that release on the tag's commit, even when `main` has moved on, and asks again before writing. A `.release/<version>/` that no longer matches stops the command with a message to remove it. `--dry-run` builds and shows the same preview, lists every reason a real run would stop, and writes nothing. It looks at the `ci.yml` run once, so it also lists a run still queued or in progress, which a real run waits up to 30 minutes for.
|
|
345
|
+
|
|
346
|
+
`--npm` then publishes the published release's own files to npm in the order `swiftplot`, `@swiftplot/react`, `@swiftplot/vue`, a pre-release under the dist-tag `next`. npm refuses a `private` package, and every package is `"private": true` for now, so `--npm` stops before anything is written. To publish, remove `private` from the three packages before `release:prepare`: the check reads the packages at the release commit. A release made without `--npm` can go to npm later with `pnpm release --npm`.
|
|
347
|
+
|
|
348
|
+
npm's staged publishing holds a new version for approval on npmjs.com, and a waiting version does not show in `npm view`. So the command keeps a receipt per package in `.release/<version>/npm.json` on your machine and never publishes a package twice. At the end it says for each package whether it is public, waiting, unconfirmed (sent, but npm did not answer; check npmjs.com), not published or not checked. A waiting version is approved on npmjs.com. If npmjs.com shows no publish of a package, because it was rejected or never arrived, remove that package's entry from `npm.json` and run again. `.release/<version>/npm.lock` stops a second publish of the same version; if a run was killed and none is running, remove `npm.lock`, not `npm.json`.
|
|
349
|
+
|
|
350
|
+
## Licence
|
|
351
|
+
|
|
352
|
+
The code is MIT. The swiftplot name and logo are not covered by that licence.
|
package/dist/chart.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Chart, Options } from './types.js';
|
|
2
|
+
export declare const NEAREST_PX = 20;
|
|
3
|
+
/**
|
|
4
|
+
* Draws the marks into `el` on the axes given, with the features in `use`, and returns the chart to update or destroy.
|
|
5
|
+
* @param el The element the chart fills.
|
|
6
|
+
*/
|
|
7
|
+
export declare function plot(el: HTMLElement, options: Options): Chart;
|