@swiftplot/vue 0.6.1 → 0.8.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/README.md CHANGED
@@ -15,7 +15,9 @@ project stands, what it lacks and what 1.0 needs: [swiftplot.dev/status](https:/
15
15
 
16
16
  **For AI coding assistants:** [`llms-full.txt`](https://swiftplot.dev/llms-full.txt) is a guide to writing code with
17
17
  swiftplot, and [`llms.txt`](https://swiftplot.dev/llms.txt) its index. `swiftplot/schema.json` is a JSON Schema of the
18
- easy charts' props, for a model that answers with a chart as JSON.
18
+ easy charts' props, for a model that answers with a chart as JSON; the recipes
19
+ [Charts for json-render](https://swiftplot.dev/guides/json-render) and [Vercel AI SDK](https://swiftplot.dev/guides/ai-sdk)
20
+ put it to work in those libraries.
19
21
 
20
22
  <img src="https://raw.githubusercontent.com/GusevP/swiftplot/main/assets/pan.gif" alt="Panning and zooming a line of a million points" width="640" />
21
23
 
@@ -58,9 +60,9 @@ Each point comes from profiling uPlot, the fastest of the other twelve libraries
58
60
 
59
61
  - **Axis text is DOM, not canvas text.** uPlot's axis drawing cost 17–21 ms at 4× CPU slowdown at every data size.
60
62
  - **Each mark has its own x column.** No aligned table, so no join step.
61
- - **Step lines and bands are reduced to one min/max per pixel column** before the path is built.
63
+ - **Lines and areas are reduced to one min/max per pixel column** before the path is built, and only the rows the window shows are read.
62
64
  - **Typed-array columns in, scale maths inlined** in the draw loops.
63
- - **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.
65
+ - **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.** Setting WebGL2 up has a fixed cost, so a scatter on Canvas 2D mounts sooner below roughly 20,000 points. Points are drawn as point sprites from one buffer in one draw call, and rectangles are instanced. 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.
64
66
  - **Paths and GPU buffers are kept,** so a hover repaint never touches the data again.
65
67
 
66
68
  ## Measured
@@ -98,12 +100,14 @@ Gzip size of the minified bundle of each entry in `site/scripts/size/`, built fr
98
100
 
99
101
  | entry | imports | KB |
100
102
  | ------------------ | ------------------------------------------------------- | ---: |
101
- | plot-line | `plot` and `line` from `swiftplot` | 15.4 |
102
- | plot-line-features | the same with `tooltip`, `legend`, `zoom` and `animate` | 20.7 |
103
- | line-chart | `lineChart` from `swiftplot/charts` | 40.2 |
104
- | all-charts | every chart of `swiftplot/charts` | 57.6 |
105
- | react-line-chart | `LineChart` from `@swiftplot/react`, React not counted | 42.0 |
106
- | vue-line-chart | `LineChart` from `@swiftplot/vue`, Vue not counted | 42.5 |
103
+ | plot-line | `plot` and `line` from `swiftplot` | 16.9 |
104
+ | plot-line-features | the same with `tooltip`, `legend`, `zoom` and `animate` | 22.8 |
105
+ | line-chart | `lineChart` from `swiftplot/charts` | 48.3 |
106
+ | all-charts | every chart of `swiftplot/charts` | 67.6 |
107
+ | react-line-chart | `LineChart` from `@swiftplot/react`, React not counted | 50.3 |
108
+ | vue-line-chart | `LineChart` from `@swiftplot/vue`, Vue not counted | 51.0 |
109
+ | element-line | `define` from `swiftplot/element` with `lineChart` | 50.0 |
110
+ | element-all | `swiftplot/element/all`, every chart as `<swift-plot>` | 69.4 |
107
111
 
108
112
  On chart-bench's page, which bundles `plot`, `line`, `area`, `ticks`, `dots`, `tooltip` and `animate`, swiftplot 0.5.0 adds 19.3 KB and uPlot 23.9 KB (`results/bundle.json`: the arm's page less the same page with no chart). uPlot ships more in its size: it is one module that comes whole, with its legend, cursor, selection and series types whether a page uses them or not.
109
113
 
package/dist/bind.d.ts CHANGED
@@ -3,6 +3,7 @@ import { type Component, type Prop } from 'vue';
3
3
  export type Mount<P> = (el: HTMLElement, props: P) => {
4
4
  update(partial: Partial<P>): void;
5
5
  destroy(): void;
6
+ refresh(): void;
6
7
  readonly overlay: HTMLElement;
7
8
  };
8
9
  /** Rendered over the chart, in its overlay, from its layout and what the pointer is over. */
package/dist/bind.js CHANGED
@@ -1,5 +1,5 @@
1
- import { equal, plain } from 'swiftplot/charts';
2
- import { defineComponent, Fragment, h, normalizeStyle, onBeforeUnmount, onMounted, onUpdated, ref, shallowRef, Teleport, toHandlerKey, toRaw, watch, withMemo, } from 'vue';
1
+ import { equal, fallback, plain } from 'swiftplot/charts';
2
+ import { defineComponent, effect, getCurrentInstance, Fragment, h, normalizeStyle, onBeforeUnmount, onMounted, onUpdated, ref, shallowRef, stop, Teleport, toHandlerKey, toRaw, watch, withMemo, } from 'vue';
3
3
  /** The controller's callbacks and the events that report them. */
4
4
  const LISTENERS = {
5
5
  onClick: 'click',
@@ -63,43 +63,16 @@ const RULE = `@layer swiftplot {
63
63
  }
64
64
  }\n`;
65
65
  const styles = new WeakMap();
66
- const detached = new WeakMap();
67
66
  const windowOf = (el) => (el.ownerDocument?.defaultView ?? globalThis);
68
67
  /**
69
68
  * Lowest priority: with no height from the app, fill a parent that has one, else 300 px. A rule in the
70
69
  * first cascade layer of the host's document or shadow root rather than an inline height, so a height
71
- * the app's CSS gives later (a media query, a layered utility class) wins. A detached host falls back
72
- * once it is laid out.
70
+ * the app's CSS gives later (a media query, a layered utility class) wins.
73
71
  */
74
- function fallback(el) {
75
- const win = windowOf(el);
76
- let observer = detached.get(win);
77
- if (!el.isConnected) {
78
- if (!observer) {
79
- observer = new win.ResizeObserver(entries => {
80
- for (const { target } of entries)
81
- if (target.isConnected)
82
- fallback(target);
83
- });
84
- detached.set(win, observer);
85
- }
86
- observer.observe(el);
87
- return;
88
- }
89
- observer?.unobserve(el);
90
- const contain = el.style.contain;
91
- el.removeAttribute(FALLBACK);
92
- el.style.contain = 'size';
93
- const cs = win.getComputedStyle(el);
94
- for (const h of ['fill', 'fixed']) {
95
- if (el.clientHeight - parseFloat(cs.paddingTop) - parseFloat(cs.paddingBottom) > 0)
96
- break;
97
- el.setAttribute(FALLBACK, h);
98
- if (h === 'fill' && !shipped(el))
99
- sheet(el);
100
- }
101
- el.style.contain = contain;
102
- }
72
+ const install = (el) => {
73
+ if (!shipped(el))
74
+ sheet(el);
75
+ };
103
76
  // A probe of its own rather than the host, whose counter-reset the app's CSS may set.
104
77
  function shipped(el) {
105
78
  const probe = el.appendChild(el.ownerDocument.createElement('swiftplot-probe'));
@@ -174,9 +147,42 @@ export function bind(chart, declared) {
174
147
  onError: null,
175
148
  ...Object.fromEntries(Object.values(LISTENERS).map(e => [toHandlerKey(e), null])),
176
149
  },
177
- setup(props, { attrs, emit, slots }) {
150
+ setup(props, { attrs, emit, slots, expose }) {
178
151
  const host = ref();
179
152
  let bound;
153
+ const self = getCurrentInstance();
154
+ let due = true;
155
+ let gone = false;
156
+ // expose() closes the template ref to what it names, so the props are named beside the ChartRef's function.
157
+ const exposed = {
158
+ [Symbol.for('swiftplot.chart')]: () => {
159
+ // A function ref is called in the parent's patch, before onMounted or the post watcher has given the chart its props.
160
+ if (due && self.isMounted && !gone) {
161
+ // An effect of its own, so the parent's render, which calls the function ref, does not track what sync reads.
162
+ let failed;
163
+ stop(effect(() => {
164
+ host.value ??= self.proxy.$el;
165
+ // Caught inside: Vue 3.4's effect() is left active, never stopped, when its first run throws.
166
+ try {
167
+ sync();
168
+ }
169
+ catch (error) {
170
+ failed = { error };
171
+ }
172
+ }));
173
+ if (failed) {
174
+ // onMounted or the post watcher tries again and reports the failure through Vue.
175
+ due = true;
176
+ throw failed.error;
177
+ }
178
+ }
179
+ return bound ? host.value : null;
180
+ },
181
+ refresh: () => bound?.refresh(),
182
+ };
183
+ for (const k of Object.keys(props))
184
+ Object.defineProperty(exposed, k, { get: () => props[k], enumerable: true });
185
+ expose(exposed);
180
186
  let slotted = false;
181
187
  const overlay = shallowRef();
182
188
  const layout = shallowRef();
@@ -185,6 +191,7 @@ export function bind(chart, declared) {
185
191
  const attempted = shallowRef();
186
192
  let reported;
187
193
  let sizing;
194
+ let drop;
188
195
  // emit calls a .once listener (onClickOnce, an attr) on its first emit only, for the life of the component.
189
196
  const fired = new Set();
190
197
  const listens = (event) => !!(props[toHandlerKey(event)] || (attrs[`${toHandlerKey(event)}Once`] && !fired.has(event)));
@@ -227,11 +234,12 @@ export function bind(chart, declared) {
227
234
  (JSON.stringify(was.layout) !== JSON.stringify(layout.value) ||
228
235
  (!!was.hover && was.hover !== hover.value));
229
236
  const sync = () => {
237
+ due = false;
230
238
  const el = host.value;
231
239
  const style = hostStyle(attrs.style);
232
240
  const next = [props.width, props.height, attrs.class, attrs.id, style];
233
241
  if ((!bound || !equal(next, sizing)) && style.height === undefined && props.height === undefined)
234
- fallback(el);
242
+ drop = fallback(el, install);
235
243
  sizing = next;
236
244
  // width and height size the host only: the core measures it on both axes, so its ResizeObserver
237
245
  // follows whatever else resizes the host (min-/max-, !important, a parent, a media query).
@@ -292,19 +300,23 @@ export function bind(chart, declared) {
292
300
  overlay.value = bound.overlay;
293
301
  reported = undefined;
294
302
  };
295
- onMounted(sync);
303
+ const settle = () => due && sync();
304
+ onMounted(settle);
296
305
  // attrs are not props, but reading them through setup's attrs is tracked, so class, style and id changes resize.
297
- watch(() => [{ ...props }, attrs.class, attrs.style, attrs.id], sync, { flush: 'post' });
306
+ const source = () => [{ ...props }, attrs.class, attrs.style, attrs.id];
307
+ watch(source, () => (due = true), { flush: 'sync' });
308
+ watch(source, settle, { flush: 'post' });
298
309
  // A slot that appears or goes changes no prop, so the watcher does not see it.
299
310
  onUpdated(() => {
300
311
  if (slotted !== !!slots.default)
301
312
  sync();
302
313
  });
303
314
  onBeforeUnmount(() => {
315
+ gone = true;
304
316
  // A Suspense boundary unmounted while pending never set the ref.
305
317
  if (!host.value)
306
318
  return;
307
- detached.get(windowOf(host.value))?.unobserve(host.value);
319
+ drop?.();
308
320
  bound?.destroy();
309
321
  bound = undefined;
310
322
  });
package/dist/index.d.ts CHANGED
@@ -35,28 +35,30 @@ export type SankeyChartProps<D = undefined> = SankeyProps<D> & StillListeners;
35
35
  export type TreemapChartProps = TreemapProps & StillListeners;
36
36
  export type SunburstChartProps = SunburstProps & StillListeners;
37
37
  export type PlotProps = PlotOptions & Listeners;
38
- type Host = AllowedComponentProps & VNodeProps & {
38
+ export type HostProps = AllowedComponentProps & VNodeProps & {
39
39
  id?: string;
40
+ /** A template ref gives the component, which is a `ChartRef` for `toBlob`: type it `ref<ChartRef | null>(null)`. */
41
+ ref?: VNodeProps['ref'];
40
42
  };
41
43
  type Context = {
42
44
  slots: {
43
45
  default?: OverlaySlot;
44
46
  };
45
47
  };
46
- export declare const LineChart: <D = undefined>(props: LineChartProps<D> & Host, ctx?: Context) => VNode;
47
- export declare const AreaChart: <D = undefined>(props: AreaChartProps<D> & Host, ctx?: Context) => VNode;
48
- export declare const BarChart: <D = undefined>(props: BarChartProps<D> & Host, ctx?: Context) => VNode;
49
- export declare const ScatterChart: <D = undefined>(props: ScatterChartProps<D> & Host, ctx?: Context) => VNode;
50
- export declare const PieChart: <D = undefined>(props: PieChartProps<D> & Host, ctx?: Context) => VNode;
51
- export declare const FunnelChart: <D = undefined>(props: FunnelChartProps<D> & Host, ctx?: Context) => VNode;
52
- export declare const RadialBarChart: <D = undefined>(props: RadialBarChartProps<D> & Host, ctx?: Context) => VNode;
53
- export declare const RadarChart: <D = undefined>(props: RadarChartProps<D> & Host, ctx?: Context) => VNode;
54
- export declare const HeatmapChart: <D = undefined>(props: HeatmapChartProps<D> & Host, ctx?: Context) => VNode;
55
- export declare const HistogramChart: <D = undefined>(props: HistogramChartProps<D> & Host, ctx?: Context) => VNode;
56
- export declare const BoxPlotChart: <D = undefined>(props: BoxPlotChartProps<D> & Host, ctx?: Context) => VNode;
57
- export declare const CandlestickChart: <D = undefined>(props: CandlestickChartProps<D> & Host, ctx?: Context) => VNode;
58
- export declare const ComboChart: <D = undefined>(props: ComboChartProps<D> & Host, ctx?: Context) => VNode;
59
- export declare const SankeyChart: <D = undefined>(props: SankeyChartProps<D> & Host, ctx?: Context) => VNode;
60
- export declare const TreemapChart: (props: TreemapChartProps & Host, ctx?: Context) => VNode;
61
- export declare const SunburstChart: (props: SunburstChartProps & Host, ctx?: Context) => VNode;
62
- export declare const Plot: (props: PlotProps & Host, ctx?: Context) => VNode;
48
+ export declare const LineChart: <D = undefined>(props: LineChartProps<D> & HostProps, ctx?: Context) => VNode;
49
+ export declare const AreaChart: <D = undefined>(props: AreaChartProps<D> & HostProps, ctx?: Context) => VNode;
50
+ export declare const BarChart: <D = undefined>(props: BarChartProps<D> & HostProps, ctx?: Context) => VNode;
51
+ export declare const ScatterChart: <D = undefined>(props: ScatterChartProps<D> & HostProps, ctx?: Context) => VNode;
52
+ export declare const PieChart: <D = undefined>(props: PieChartProps<D> & HostProps, ctx?: Context) => VNode;
53
+ export declare const FunnelChart: <D = undefined>(props: FunnelChartProps<D> & HostProps, ctx?: Context) => VNode;
54
+ export declare const RadialBarChart: <D = undefined>(props: RadialBarChartProps<D> & HostProps, ctx?: Context) => VNode;
55
+ export declare const RadarChart: <D = undefined>(props: RadarChartProps<D> & HostProps, ctx?: Context) => VNode;
56
+ export declare const HeatmapChart: <D = undefined>(props: HeatmapChartProps<D> & HostProps, ctx?: Context) => VNode;
57
+ export declare const HistogramChart: <D = undefined>(props: HistogramChartProps<D> & HostProps, ctx?: Context) => VNode;
58
+ export declare const BoxPlotChart: <D = undefined>(props: BoxPlotChartProps<D> & HostProps, ctx?: Context) => VNode;
59
+ export declare const CandlestickChart: <D = undefined>(props: CandlestickChartProps<D> & HostProps, ctx?: Context) => VNode;
60
+ export declare const ComboChart: <D = undefined>(props: ComboChartProps<D> & HostProps, ctx?: Context) => VNode;
61
+ export declare const SankeyChart: <D = undefined>(props: SankeyChartProps<D> & HostProps, ctx?: Context) => VNode;
62
+ export declare const TreemapChart: (props: TreemapChartProps & HostProps, ctx?: Context) => VNode;
63
+ export declare const SunburstChart: (props: SunburstChartProps & HostProps, ctx?: Context) => VNode;
64
+ export declare const Plot: (props: PlotProps & HostProps, ctx?: Context) => VNode;
package/dist/index.js CHANGED
@@ -8,6 +8,8 @@ const base = {
8
8
  colors: null,
9
9
  theme: null,
10
10
  tooltip: bool,
11
+ a11y: bool,
12
+ table: bool,
11
13
  legend: flag,
12
14
  animate: flag,
13
15
  dim: { type: [Number, Boolean], default: undefined },
@@ -15,12 +17,13 @@ const base = {
15
17
  height: null,
16
18
  margin: null,
17
19
  description: null,
20
+ locale: null,
18
21
  empty: null,
19
22
  hidden: null,
20
23
  selected: null,
21
24
  pointer: null,
22
25
  };
23
- const axes = { xAxis: null, yAxis: null, zoom: flag, view: null };
26
+ const axes = { xAxis: null, yAxis: null, grid: bool, zoom: flag, view: null };
24
27
  const overlays = { rules: null, zones: null, brush: flag };
25
28
  const xy = { ...base, ...axes, ...overlays, x: null, y: null, y2: null, series: null, y2Axis: null };
26
29
  export const LineChart = /*#__PURE__*/ bind(lineChart, () => ({
@@ -43,6 +46,8 @@ export const BarChart = /*#__PURE__*/ bind(barChart, () => ({
43
46
  stack: bool,
44
47
  cumulative: bool,
45
48
  errorBars: null,
49
+ text: { type: [Boolean, Function], default: undefined },
50
+ horizontal: bool,
46
51
  }));
47
52
  export const ScatterChart = /*#__PURE__*/ bind(scatterChart, () => ({
48
53
  ...xy,
@@ -91,6 +96,7 @@ export const HeatmapChart = /*#__PURE__*/ bind(heatmapChart, () => ({
91
96
  color: null,
92
97
  xAxis: null,
93
98
  yAxis: null,
99
+ grid: bool,
94
100
  }));
95
101
  export const HistogramChart = /*#__PURE__*/ bind(histogramChart, () => ({
96
102
  ...base,
@@ -106,6 +112,7 @@ export const BoxPlotChart = /*#__PURE__*/ bind(boxPlotChart, () => ({
106
112
  fill: null,
107
113
  xAxis: null,
108
114
  yAxis: null,
115
+ grid: bool,
109
116
  }));
110
117
  export const CandlestickChart = /*#__PURE__*/ bind(candlestickChart, () => ({
111
118
  ...base,
@@ -156,10 +163,13 @@ export const Plot = /*#__PURE__*/ bind(mount, () => ({
156
163
  x: null,
157
164
  y: null,
158
165
  y2: null,
166
+ grid: bool,
167
+ valueAxis: null,
159
168
  frame: null,
160
169
  marks: null,
161
170
  use: null,
162
171
  description: null,
172
+ locale: null,
163
173
  dim: null,
164
174
  theme: null,
165
175
  view: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swiftplot/vue",
3
- "version": "0.6.1",
3
+ "version": "0.8.0",
4
4
  "description": "Vue components for swiftplot charts",
5
5
  "keywords": [
6
6
  "chart",
@@ -38,22 +38,24 @@
38
38
  ],
39
39
  "peerDependencies": {
40
40
  "vue": "^3.4",
41
- "swiftplot": "^0.6.1"
41
+ "swiftplot": "^0.8.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/node": "^24.19.2",
45
45
  "@vitest/browser-playwright": "^5.1.1",
46
+ "happy-dom": "^21.0.4",
47
+ "jsdom": "^31.0.1",
46
48
  "playwright": "^1.65.1",
47
49
  "typescript": "~5.9.3",
48
50
  "vitest": "^5.1.1",
49
51
  "vue": "^3.5.46",
50
52
  "vue-tsc": "^3.3.14",
51
- "swiftplot": "0.6.1"
53
+ "swiftplot": "0.8.0"
52
54
  },
53
55
  "scripts": {
54
56
  "build": "tsc -p tsconfig.build.json",
55
57
  "typecheck": "tsc --noEmit && vue-tsc --noEmit && tsc --noEmit -p tsconfig.node.json",
56
- "test": "vitest run",
58
+ "test": "vitest run && vitest run -c vitest.dom.config.ts",
57
59
  "test:import": "node test/import.mjs"
58
60
  }
59
61
  }