@swiftplot/vue 0.6.0 → 0.7.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
 
@@ -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` | 15.7 |
104
+ | plot-line-features | the same with `tooltip`, `legend`, `zoom` and `animate` | 21.5 |
105
+ | line-chart | `lineChart` from `swiftplot/charts` | 45.1 |
106
+ | all-charts | every chart of `swiftplot/charts` | 64.0 |
107
+ | react-line-chart | `LineChart` from `@swiftplot/react`, React not counted | 47.0 |
108
+ | vue-line-chart | `LineChart` from `@swiftplot/vue`, Vue not counted | 47.7 |
109
+ | element-line | `define` from `swiftplot/element` with `lineChart` | 46.7 |
110
+ | element-all | `swiftplot/element/all`, every chart as `<swift-plot>` | 65.8 |
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.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,41 @@ 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
+ };
182
+ for (const k of Object.keys(props))
183
+ Object.defineProperty(exposed, k, { get: () => props[k], enumerable: true });
184
+ expose(exposed);
180
185
  let slotted = false;
181
186
  const overlay = shallowRef();
182
187
  const layout = shallowRef();
@@ -185,6 +190,7 @@ export function bind(chart, declared) {
185
190
  const attempted = shallowRef();
186
191
  let reported;
187
192
  let sizing;
193
+ let drop;
188
194
  // emit calls a .once listener (onClickOnce, an attr) on its first emit only, for the life of the component.
189
195
  const fired = new Set();
190
196
  const listens = (event) => !!(props[toHandlerKey(event)] || (attrs[`${toHandlerKey(event)}Once`] && !fired.has(event)));
@@ -227,11 +233,12 @@ export function bind(chart, declared) {
227
233
  (JSON.stringify(was.layout) !== JSON.stringify(layout.value) ||
228
234
  (!!was.hover && was.hover !== hover.value));
229
235
  const sync = () => {
236
+ due = false;
230
237
  const el = host.value;
231
238
  const style = hostStyle(attrs.style);
232
239
  const next = [props.width, props.height, attrs.class, attrs.id, style];
233
240
  if ((!bound || !equal(next, sizing)) && style.height === undefined && props.height === undefined)
234
- fallback(el);
241
+ drop = fallback(el, install);
235
242
  sizing = next;
236
243
  // width and height size the host only: the core measures it on both axes, so its ResizeObserver
237
244
  // follows whatever else resizes the host (min-/max-, !important, a parent, a media query).
@@ -292,19 +299,23 @@ export function bind(chart, declared) {
292
299
  overlay.value = bound.overlay;
293
300
  reported = undefined;
294
301
  };
295
- onMounted(sync);
302
+ const settle = () => due && sync();
303
+ onMounted(settle);
296
304
  // 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' });
305
+ const source = () => [{ ...props }, attrs.class, attrs.style, attrs.id];
306
+ watch(source, () => (due = true), { flush: 'sync' });
307
+ watch(source, settle, { flush: 'post' });
298
308
  // A slot that appears or goes changes no prop, so the watcher does not see it.
299
309
  onUpdated(() => {
300
310
  if (slotted !== !!slots.default)
301
311
  sync();
302
312
  });
303
313
  onBeforeUnmount(() => {
314
+ gone = true;
304
315
  // A Suspense boundary unmounted while pending never set the ref.
305
316
  if (!host.value)
306
317
  return;
307
- detached.get(windowOf(host.value))?.unobserve(host.value);
318
+ drop?.();
308
319
  bound?.destroy();
309
320
  bound = undefined;
310
321
  });
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import type { Options } from 'swiftplot';
2
- import { type AreaChartProps as AreaProps, type BarChartProps as BarProps, type BoxPlotChartProps as BoxPlotProps, type CandlestickChartProps as CandlestickProps, type ComboChartProps as ComboProps, type FunnelChartProps as FunnelProps, type HeatmapChartProps as HeatmapProps, type HistogramChartProps as HistogramProps, type LineChartProps as LineProps, type PieChartProps as PieProps, type RadarChartProps as RadarProps, type RadialBarChartProps as RadialBarProps, type SankeyChartProps as SankeyProps, type ScatterChartProps as ScatterProps, type SunburstChartProps as SunburstProps, type TreemapChartProps as TreemapProps } from 'swiftplot/charts';
2
+ import { type AreaChartProps as AreaProps, type BarChartProps as BarProps, type BoxPlotChartProps as BoxPlotProps, type PlotOptions, type CandlestickChartProps as CandlestickProps, type ComboChartProps as ComboProps, type FunnelChartProps as FunnelProps, type HeatmapChartProps as HeatmapProps, type HistogramChartProps as HistogramProps, type LineChartProps as LineProps, type PieChartProps as PieProps, type RadarChartProps as RadarProps, type RadialBarChartProps as RadialBarProps, type SankeyChartProps as SankeyProps, type ScatterChartProps as ScatterProps, type SunburstChartProps as SunburstProps, type TreemapChartProps as TreemapProps } from 'swiftplot/charts';
3
3
  import type { AllowedComponentProps, VNode, VNodeProps } from 'vue';
4
4
  import { type OverlaySlot } from './bind.js';
5
+ export type { ScaledY2AxisProps, ScaledYAxisProps, XAxisProps, Y2AxisProps, YAxisProps, } from 'swiftplot/charts';
5
6
  /** The chart's change callbacks arrive as Vue listeners: `@click`, `@layout`, `@hover`, `@error` and `v-model:view`, `:hidden`, `:pointer`. */
6
7
  export type Listeners = {
7
8
  onClick?: Options['onClick'];
@@ -33,30 +34,31 @@ export type ComboChartProps<D = undefined> = ComboProps<D> & Listeners;
33
34
  export type SankeyChartProps<D = undefined> = SankeyProps<D> & StillListeners;
34
35
  export type TreemapChartProps = TreemapProps & StillListeners;
35
36
  export type SunburstChartProps = SunburstProps & StillListeners;
36
- export type PlotProps = Options & Listeners;
37
- type Host = AllowedComponentProps & VNodeProps & {
37
+ export type PlotProps = PlotOptions & Listeners;
38
+ export type HostProps = AllowedComponentProps & VNodeProps & {
38
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'];
39
42
  };
40
43
  type Context = {
41
44
  slots: {
42
45
  default?: OverlaySlot;
43
46
  };
44
47
  };
45
- export declare const LineChart: <D = undefined>(props: LineChartProps<D> & Host, ctx?: Context) => VNode;
46
- export declare const AreaChart: <D = undefined>(props: AreaChartProps<D> & Host, ctx?: Context) => VNode;
47
- export declare const BarChart: <D = undefined>(props: BarChartProps<D> & Host, ctx?: Context) => VNode;
48
- export declare const ScatterChart: <D = undefined>(props: ScatterChartProps<D> & Host, ctx?: Context) => VNode;
49
- export declare const PieChart: <D = undefined>(props: PieChartProps<D> & Host, ctx?: Context) => VNode;
50
- export declare const FunnelChart: <D = undefined>(props: FunnelChartProps<D> & Host, ctx?: Context) => VNode;
51
- export declare const RadialBarChart: <D = undefined>(props: RadialBarChartProps<D> & Host, ctx?: Context) => VNode;
52
- export declare const RadarChart: <D = undefined>(props: RadarChartProps<D> & Host, ctx?: Context) => VNode;
53
- export declare const HeatmapChart: <D = undefined>(props: HeatmapChartProps<D> & Host, ctx?: Context) => VNode;
54
- export declare const HistogramChart: <D = undefined>(props: HistogramChartProps<D> & Host, ctx?: Context) => VNode;
55
- export declare const BoxPlotChart: <D = undefined>(props: BoxPlotChartProps<D> & Host, ctx?: Context) => VNode;
56
- export declare const CandlestickChart: <D = undefined>(props: CandlestickChartProps<D> & Host, ctx?: Context) => VNode;
57
- export declare const ComboChart: <D = undefined>(props: ComboChartProps<D> & Host, ctx?: Context) => VNode;
58
- export declare const SankeyChart: <D = undefined>(props: SankeyChartProps<D> & Host, ctx?: Context) => VNode;
59
- export declare const TreemapChart: (props: TreemapChartProps & Host, ctx?: Context) => VNode;
60
- export declare const SunburstChart: (props: SunburstChartProps & Host, ctx?: Context) => VNode;
61
- export declare const Plot: (props: PlotProps & Host, ctx?: Context) => VNode;
62
- export {};
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,6 +17,7 @@ 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,
@@ -160,6 +163,7 @@ export const Plot = /*#__PURE__*/ bind(mount, () => ({
160
163
  marks: null,
161
164
  use: null,
162
165
  description: null,
166
+ locale: null,
163
167
  dim: null,
164
168
  theme: null,
165
169
  view: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swiftplot/vue",
3
- "version": "0.6.0",
3
+ "version": "0.7.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.0"
41
+ "swiftplot": "^0.7.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.0"
53
+ "swiftplot": "0.7.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
  }