layerchart 0.5.0 → 0.6.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.
@@ -15,12 +15,12 @@
15
15
  import { getContext } from 'svelte';
16
16
  import { arc as d3arc } from 'd3-shape';
17
17
  import { scaleLinear } from 'd3-scale';
18
- import { createMotionStore } from '../stores/motionStore';
18
+ import { motionStore } from '../stores/motionStore';
19
19
  import { degreesToRadians } from '../utils/math';
20
20
  export let spring = undefined;
21
21
  export let tweened = undefined;
22
22
  export let value = 0;
23
- let tweened_value = createMotionStore(value, { spring, tweened });
23
+ let tweened_value = motionStore(value, { spring, tweened });
24
24
  $: tweened_value.set(value);
25
25
  export let domain = [0, 100];
26
26
  /**
@@ -1,7 +1,7 @@
1
1
  <script>import { getContext } from 'svelte';
2
2
  import { area as d3Area } from 'd3-shape';
3
3
  import { interpolatePath } from 'd3-interpolate-path';
4
- import { createMotionStore } from '../stores/motionStore';
4
+ import { motionStore } from '../stores/motionStore';
5
5
  import Path from './Path.svelte';
6
6
  const { data: contextData, xGet, yGet, yRange } = getContext('LayerCake');
7
7
  // Properties to override what is used from context
@@ -18,7 +18,7 @@ export let color = 'var(--color-blue-500)';
18
18
  export let opacity = 0.3;
19
19
  export let line = false;
20
20
  $: tweenedOptions = tweened ? { interpolate: interpolatePath, ...tweened } : false;
21
- $: tweened_d = createMotionStore('', { tweened: tweenedOptions });
21
+ $: tweened_d = motionStore('', { tweened: tweenedOptions });
22
22
  $: {
23
23
  const path = d3Area()
24
24
  .x(x ?? $xGet)
@@ -1,11 +1,11 @@
1
1
  <script>import { getContext } from 'svelte';
2
- import { cubicOut } from 'svelte/easing';
3
2
  import { scaleLinear } from 'd3-scale';
4
- import { tweenedScale } from '../utils/scales';
3
+ import { motionScale } from '../utils/scales';
5
4
  const { width, height } = getContext('LayerCake');
6
5
  export let domain;
7
6
  export let range;
8
- const tweenedOptions = { easing: cubicOut, duration: 800 };
7
+ export let spring = undefined;
8
+ export let tweened = undefined;
9
9
  function getExtents(extents, axis, fallback) {
10
10
  const resolvedExtents = typeof extents === 'function' ? extents({ width: $width, height: $height }) : extents;
11
11
  return [
@@ -13,10 +13,10 @@ function getExtents(extents, axis, fallback) {
13
13
  resolvedExtents?.[axis + '1'] ?? fallback // x1 or y1, fallback as $width or $height
14
14
  ];
15
15
  }
16
- const xScale = tweenedScale(scaleLinear, tweenedOptions);
16
+ const xScale = motionScale(scaleLinear, { spring, tweened });
17
17
  $: xScale.domain(getExtents(domain, 'x', $width));
18
18
  $: xScale.range(getExtents(range, 'x', $width));
19
- const yScale = tweenedScale(scaleLinear, tweenedOptions);
19
+ const yScale = motionScale(scaleLinear, { spring, tweened });
20
20
  $: yScale.domain(getExtents(domain, 'y', $height));
21
21
  $: yScale.range(getExtents(range, 'y', $height));
22
22
  </script>
@@ -1,4 +1,5 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
+ import { motionScale } from '../utils/scales';
2
3
  declare const __propDef: {
3
4
  props: {
4
5
  domain: {
@@ -29,6 +30,8 @@ declare const __propDef: {
29
30
  x1: number;
30
31
  y1: number;
31
32
  });
33
+ spring?: boolean | Parameters<typeof motionScale>[1]['spring'];
34
+ tweened?: boolean | Parameters<typeof motionScale>[1]['tweened'];
32
35
  };
33
36
  events: {
34
37
  [evt: string]: CustomEvent<any>;
@@ -1,12 +1,12 @@
1
- <script>import { createMotionStore } from '../stores/motionStore';
2
- export let cx;
3
- export let cy;
1
+ <script>import { motionStore } from '../stores/motionStore';
2
+ export let cx = 0;
3
+ export let cy = 0;
4
4
  export let r;
5
5
  export let spring = undefined;
6
6
  export let tweened = undefined;
7
- let tweened_cx = createMotionStore(cx, { spring, tweened });
8
- let tweened_cy = createMotionStore(cy, { spring, tweened });
9
- let tweened_r = createMotionStore(r, { spring, tweened });
7
+ let tweened_cx = motionStore(cx, { spring, tweened });
8
+ let tweened_cy = motionStore(cy, { spring, tweened });
9
+ let tweened_r = motionStore(r, { spring, tweened });
10
10
  $: tweened_cx.set(cx);
11
11
  $: tweened_cy.set(cy);
12
12
  $: tweened_r.set(r);
@@ -3,8 +3,8 @@ import type { spring as springStore, tweened as tweenedStore } from 'svelte/moti
3
3
  declare const __propDef: {
4
4
  props: {
5
5
  [x: string]: any;
6
- cx: number;
7
- cy: number;
6
+ cx?: number;
7
+ cy?: number;
8
8
  r: number;
9
9
  spring?: boolean | Parameters<typeof springStore>[1];
10
10
  tweened?: boolean | Parameters<typeof tweenedStore>[1];
@@ -1,5 +1,5 @@
1
1
  <script>import { getContext } from 'svelte';
2
- import { createMotionStore } from '../stores/motionStore';
2
+ import { motionStore } from '../stores/motionStore';
3
3
  const { width, height } = getContext('LayerCake');
4
4
  /**
5
5
  * Translate x
@@ -15,8 +15,8 @@ export let y = undefined;
15
15
  export let center = false;
16
16
  export let spring = undefined;
17
17
  export let tweened = undefined;
18
- let tweened_x = createMotionStore(x, { spring, tweened });
19
- let tweened_y = createMotionStore(y, { spring, tweened });
18
+ let tweened_x = motionStore(x, { spring, tweened });
19
+ let tweened_y = motionStore(y, { spring, tweened });
20
20
  $: tweened_x.set(x);
21
21
  $: tweened_y.set(y);
22
22
  let transform = undefined;
@@ -1,14 +1,14 @@
1
- <script>import { createMotionStore } from '../stores/motionStore';
1
+ <script>import { motionStore } from '../stores/motionStore';
2
2
  export let x1;
3
3
  export let y1;
4
4
  export let x2;
5
5
  export let y2;
6
6
  export let spring = undefined;
7
7
  export let tweened = undefined;
8
- let tweened_x1 = createMotionStore(x1, { spring, tweened });
9
- let tweened_y1 = createMotionStore(y1, { spring, tweened });
10
- let tweened_x2 = createMotionStore(x2, { spring, tweened });
11
- let tweened_y2 = createMotionStore(y2, { spring, tweened });
8
+ let tweened_x1 = motionStore(x1, { spring, tweened });
9
+ let tweened_y1 = motionStore(y1, { spring, tweened });
10
+ let tweened_x2 = motionStore(x2, { spring, tweened });
11
+ let tweened_y2 = motionStore(y2, { spring, tweened });
12
12
  $: tweened_x1.set(x1);
13
13
  $: tweened_y1.set(y1);
14
14
  $: tweened_x2.set(x2);
@@ -1,6 +1,6 @@
1
1
  <script>import { link as d3Link, curveBumpX, curveBumpY } from 'd3-shape';
2
2
  import { interpolatePath } from 'd3-interpolate-path';
3
- import { createMotionStore } from '../stores/motionStore';
3
+ import { motionStore } from '../stores/motionStore';
4
4
  // Properties to override what is used from context
5
5
  export let data = undefined; // TODO: Update Type
6
6
  export let orientation = 'horizontal';
@@ -17,7 +17,7 @@ export let tweened = undefined;
17
17
  export let color = 'black';
18
18
  export let width = undefined;
19
19
  $: tweenedOptions = tweened ? { interpolate: interpolatePath, ...tweened } : false;
20
- $: tweened_d = createMotionStore('', { tweened: tweenedOptions });
20
+ $: tweened_d = motionStore('', { tweened: tweenedOptions });
21
21
  $: {
22
22
  orientation; // subscribe to orientation changes to link is update
23
23
  const link = d3Link(curve).source(source).target(target).x(x).y(y);
@@ -0,0 +1,19 @@
1
+ <script>import { getContext } from 'svelte';
2
+ import { pack as d3Pack } from 'd3-hierarchy';
3
+ const { data, width, height } = getContext('LayerCake');
4
+ export let size = undefined;
5
+ /**
6
+ * see: https://github.com/d3/d3-hierarchy#pack_padding
7
+ */
8
+ export let padding = undefined;
9
+ let pack;
10
+ $: {
11
+ pack = d3Pack().size(size ?? [$width, $height]);
12
+ if (padding) {
13
+ pack.padding(padding);
14
+ }
15
+ }
16
+ $: packData = pack($data);
17
+ </script>
18
+
19
+ <slot nodes={packData.descendants()} />
@@ -0,0 +1,23 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ declare const __propDef: {
3
+ props: {
4
+ size?: [number, number] | undefined;
5
+ /**
6
+ * see: https://github.com/d3/d3-hierarchy#pack_padding
7
+ */ padding?: number | undefined;
8
+ };
9
+ events: {
10
+ [evt: string]: CustomEvent<any>;
11
+ };
12
+ slots: {
13
+ default: {
14
+ nodes: import("d3-hierarchy").HierarchyCircularNode<unknown>[];
15
+ };
16
+ };
17
+ };
18
+ export declare type PackProps = typeof __propDef.props;
19
+ export declare type PackEvents = typeof __propDef.events;
20
+ export declare type PackSlots = typeof __propDef.slots;
21
+ export default class Pack extends SvelteComponentTyped<PackProps, PackEvents, PackSlots> {
22
+ }
23
+ export {};
@@ -6,7 +6,7 @@ import { getContext } from 'svelte';
6
6
  import { line as d3Line } from 'd3-shape';
7
7
  // import { interpolateString } from 'd3-interpolate';
8
8
  import { interpolatePath } from 'd3-interpolate-path';
9
- import { createMotionStore } from '../stores/motionStore';
9
+ import { motionStore } from '../stores/motionStore';
10
10
  const { data: contextData, xGet, yGet, zGet } = getContext('LayerCake');
11
11
  // Properties to override what is used from context
12
12
  export let data = undefined; // TODO: Update Type
@@ -19,7 +19,7 @@ export let defined = undefined;
19
19
  export let color = 'black';
20
20
  export let width = undefined;
21
21
  $: tweenedOptions = tweened ? { interpolate: interpolatePath, ...tweened } : false;
22
- $: tweened_d = createMotionStore('', { tweened: tweenedOptions });
22
+ $: tweened_d = motionStore('', { tweened: tweenedOptions });
23
23
  $: {
24
24
  const path = d3Line()
25
25
  .x(x ?? $xGet)
@@ -3,7 +3,7 @@ import { pie as d3pie } from 'd3-shape';
3
3
  import Arc from './Arc.svelte';
4
4
  import Group from './Group.svelte';
5
5
  import { degreesToRadians } from '../utils/math';
6
- import { createMotionStore } from '../stores/motionStore';
6
+ import { motionStore } from '../stores/motionStore';
7
7
  /*
8
8
  TODO:
9
9
  - [ ] Offset (always, on hover)
@@ -48,7 +48,7 @@ export let tweened = undefined;
48
48
  export let offset = 0;
49
49
  const { data: contextData, x, y, xRange, rGet, config } = getContext('LayerCake');
50
50
  $: resolved_endAngle = endAngle ?? degreesToRadians($config.xRange ? $xRange[1] : range[1]);
51
- let tweened_endAngle = createMotionStore(0, { spring, tweened });
51
+ let tweened_endAngle = motionStore(0, { spring, tweened });
52
52
  $: tweened_endAngle.set(resolved_endAngle);
53
53
  $: pie = d3pie()
54
54
  .startAngle(startAngle ?? degreesToRadians($config.xRange ? $xRange[0] : range[0]))
@@ -1,14 +1,14 @@
1
- <script>import { createMotionStore } from '../stores/motionStore';
1
+ <script>import { motionStore } from '../stores/motionStore';
2
2
  export let x = 0;
3
3
  export let y = 0;
4
4
  export let width;
5
5
  export let height;
6
6
  export let spring = undefined;
7
7
  export let tweened = undefined;
8
- let tweened_x = createMotionStore(x, { spring, tweened });
9
- let tweened_y = createMotionStore(y, { spring, tweened });
10
- let tweened_width = createMotionStore(width, { spring, tweened });
11
- let tweened_height = createMotionStore(height, { spring, tweened });
8
+ let tweened_x = motionStore(x, { spring, tweened });
9
+ let tweened_y = motionStore(y, { spring, tweened });
10
+ let tweened_width = motionStore(width, { spring, tweened });
11
+ let tweened_height = motionStore(height, { spring, tweened });
12
12
  $: tweened_x.set(x);
13
13
  $: tweened_y.set(y);
14
14
  $: tweened_width.set(width);
@@ -1,10 +1,5 @@
1
- <script>/**
2
- * TODO:
3
- * - [ ] Improve zoomable nested (apply extent ratio? const extentRatio = ($extents.y1 - $extents.y0) / $height;
4
- */
5
- import { getContext } from 'svelte';
1
+ <script>import { getContext } from 'svelte';
6
2
  import * as d3 from 'd3-hierarchy';
7
- import { group } from 'd3-array';
8
3
  import { aspectTile } from '../utils/treemap';
9
4
  const { data, width, height } = getContext('LayerCake');
10
5
  export let tile = d3.treemapSquarify;
@@ -15,7 +10,6 @@ export let paddingTop = 0;
15
10
  export let paddingBottom = 0;
16
11
  export let paddingLeft = undefined;
17
12
  export let paddingRight = undefined;
18
- export let nodeKey = (node, i) => i;
19
13
  export let selected = null;
20
14
  $: tileFunc =
21
15
  tile === 'squarify'
@@ -56,17 +50,9 @@ $: {
56
50
  treemap.paddingRight(paddingRight);
57
51
  }
58
52
  }
59
- $: root = treemap($data);
53
+ $: treemapData = treemap($data);
60
54
  // TODO: Remove selected
61
- $: selected = root; // set initial selection
62
- // group nodes by depth so can be rendered lowest to highest, to stack properly
63
- $: nodesByDepth = group(root, (d) => d.depth);
55
+ $: selected = treemapData; // set initial selection
64
56
  </script>
65
57
 
66
- {#each Array.from(nodesByDepth) as [depth, nodes]}
67
- <g>
68
- {#each nodes as node, i (nodeKey(node, i))}
69
- <slot name="node" {node} />
70
- {/each}
71
- </g>
72
- {/each}
58
+ <slot nodes={treemapData.descendants()} />
@@ -10,15 +10,14 @@ declare const __propDef: {
10
10
  paddingBottom?: number;
11
11
  paddingLeft?: any;
12
12
  paddingRight?: any;
13
- nodeKey?: (node: d3.HierarchyNode<any>, i: number) => any;
14
13
  selected?: any;
15
14
  };
16
15
  events: {
17
16
  [evt: string]: CustomEvent<any>;
18
17
  };
19
18
  slots: {
20
- node: {
21
- node: unknown;
19
+ default: {
20
+ nodes: any;
22
21
  };
23
22
  };
24
23
  };
@@ -1,9 +1,11 @@
1
1
  <script>import { getContext } from 'svelte';
2
- import { spring } from 'svelte/motion';
2
+ import { motionStore } from '../stores/motionStore';
3
3
  const { width, height, padding } = getContext('LayerCake');
4
+ export let spring = undefined;
5
+ export let tweened = undefined;
4
6
  let dragging = false;
5
- let translate = spring({ x: 0, y: 0 });
6
- let scale = spring({ x: 1, y: 1 });
7
+ const translate = motionStore({ x: 0, y: 0 }, { spring, tweened });
8
+ const scale = motionStore({ x: 1, y: 1 }, { spring, tweened });
7
9
  let startPoint;
8
10
  let startTranslate;
9
11
  let svgEl = null;
@@ -29,6 +31,18 @@ export function translateCenter() {
29
31
  y: 0
30
32
  };
31
33
  }
34
+ export function zoomTo(newTranslate, newScale) {
35
+ $translate = {
36
+ x: $width / 2 - newTranslate.x,
37
+ y: $height / 2 - newTranslate.y
38
+ };
39
+ if (newScale) {
40
+ $scale = {
41
+ x: Math.min($width, $height) / newScale.x,
42
+ y: Math.min($width, $height) / newScale.y
43
+ };
44
+ }
45
+ }
32
46
  function handleMouseDown(e) {
33
47
  dragging = true;
34
48
  svgEl = e.target.ownerSVGElement;
@@ -51,7 +65,7 @@ function handleMouseMove(e) {
51
65
  translate.set({
52
66
  x: startTranslate.x + deltaX / $scale.x,
53
67
  y: startTranslate.y + deltaY / $scale.y
54
- }, { hard: true });
68
+ }, spring ? { hard: true } : tweened ? { duration: 0 } : undefined);
55
69
  }
56
70
  function handleDoubleClick() {
57
71
  increase();
@@ -65,7 +79,7 @@ function handleWheel(e) {
65
79
  scale.set({
66
80
  x: $scale.x * scaleBy,
67
81
  y: $scale.y * scaleBy
68
- }, { hard: true });
82
+ }, spring ? { hard: true } : tweened ? { duration: 0 } : undefined);
69
83
  }
70
84
  function localPoint(svgEl, e) {
71
85
  const screenCTM = svgEl.getScreenCTM();
@@ -104,5 +118,5 @@ $: newTranslate = {
104
118
  fill="transparent"
105
119
  />
106
120
  <g transform="translate({newTranslate.x},{newTranslate.y}) scale({$scale.x},{$scale.y})">
107
- <slot />
121
+ <slot scale={$scale} />
108
122
  </g>
@@ -1,16 +1,28 @@
1
1
  import { SvelteComponentTyped } from "svelte";
2
+ import { motionStore } from '../stores/motionStore';
2
3
  declare const __propDef: {
3
4
  props: {
5
+ spring?: boolean | Parameters<typeof motionStore>[1]['spring'];
6
+ tweened?: boolean | Parameters<typeof motionStore>[1]['tweened'];
4
7
  reset?: () => void;
5
8
  increase?: () => void;
6
9
  decrease?: () => void;
7
10
  translateCenter?: () => void;
11
+ zoomTo?: (newTranslate: {
12
+ x: number;
13
+ y: number;
14
+ }, newScale?: {
15
+ x: number;
16
+ y: number;
17
+ }) => void;
8
18
  };
9
19
  events: {
10
20
  [evt: string]: CustomEvent<any>;
11
21
  };
12
22
  slots: {
13
- default: {};
23
+ default: {
24
+ scale: any;
25
+ };
14
26
  };
15
27
  };
16
28
  export declare type ZoomProps = typeof __propDef.props;
@@ -21,5 +33,12 @@ export default class Zoom extends SvelteComponentTyped<ZoomProps, ZoomEvents, Zo
21
33
  get increase(): () => void;
22
34
  get decrease(): () => void;
23
35
  get translateCenter(): () => void;
36
+ get zoomTo(): (newTranslate: {
37
+ x: number;
38
+ y: number;
39
+ }, newScale?: {
40
+ x: number;
41
+ y: number;
42
+ }) => void;
24
43
  }
25
44
  export {};
@@ -5,6 +5,7 @@ export { default as AxisX } from './AxisX.svelte';
5
5
  export { default as AxisY } from './AxisY.svelte';
6
6
  export { default as Bars } from './Bars.svelte';
7
7
  export { default as Baseline } from './Baseline.svelte';
8
+ export { default as Bounds } from './Bounds.svelte';
8
9
  export { default as Chart } from './Chart.svelte';
9
10
  export { default as ChartClipPath } from './ChartClipPath.svelte';
10
11
  export { default as Circle } from './Circle.svelte';
@@ -18,6 +19,7 @@ export { default as Labels } from './Labels.svelte';
18
19
  export { default as Line } from './Line.svelte';
19
20
  export { default as LinearGradient } from './LinearGradient.svelte';
20
21
  export { default as Link } from './Link.svelte';
22
+ export { default as Partition } from './Partition.svelte';
21
23
  export { default as Path } from './Path.svelte';
22
24
  export { default as Pattern } from './Pattern.svelte';
23
25
  export { default as Pie } from './Pie.svelte';
@@ -28,5 +30,6 @@ export { default as Sankey } from './Sankey.svelte';
28
30
  export { default as Text } from './Text.svelte';
29
31
  export { default as Threshold } from './Threshold.svelte';
30
32
  export { default as Tooltip } from './Tooltip.svelte';
33
+ export { default as Tree } from './Tree.svelte';
31
34
  export { default as Treemap } from './Treemap.svelte';
32
35
  export { default as Zoom } from './Zoom.svelte';
@@ -5,6 +5,7 @@ export { default as AxisX } from './AxisX.svelte';
5
5
  export { default as AxisY } from './AxisY.svelte';
6
6
  export { default as Bars } from './Bars.svelte';
7
7
  export { default as Baseline } from './Baseline.svelte';
8
+ export { default as Bounds } from './Bounds.svelte';
8
9
  export { default as Chart } from './Chart.svelte';
9
10
  export { default as ChartClipPath } from './ChartClipPath.svelte';
10
11
  export { default as Circle } from './Circle.svelte';
@@ -18,6 +19,7 @@ export { default as Labels } from './Labels.svelte';
18
19
  export { default as Line } from './Line.svelte';
19
20
  export { default as LinearGradient } from './LinearGradient.svelte';
20
21
  export { default as Link } from './Link.svelte';
22
+ export { default as Partition } from './Partition.svelte';
21
23
  export { default as Path } from './Path.svelte';
22
24
  export { default as Pattern } from './Pattern.svelte';
23
25
  export { default as Pie } from './Pie.svelte';
@@ -28,5 +30,6 @@ export { default as Sankey } from './Sankey.svelte';
28
30
  export { default as Text } from './Text.svelte';
29
31
  export { default as Threshold } from './Threshold.svelte';
30
32
  export { default as Tooltip } from './Tooltip.svelte';
33
+ export { default as Tree } from './Tree.svelte';
31
34
  export { default as Treemap } from './Treemap.svelte';
32
35
  export { default as Zoom } from './Zoom.svelte';
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "author": "Sean Lynch <techniq35@gmail.com>",
4
4
  "license": "MIT",
5
5
  "repository": "techniq/layerchart",
6
- "version": "0.5.0",
6
+ "version": "0.6.0",
7
7
  "devDependencies": {
8
8
  "@rollup/plugin-dsv": "^2.0.3",
9
9
  "@sveltejs/adapter-vercel": "^1.0.0-next.58",
@@ -70,6 +70,7 @@
70
70
  "./components/Line.svelte": "./components/Line.svelte",
71
71
  "./components/LinearGradient.svelte": "./components/LinearGradient.svelte",
72
72
  "./components/Link.svelte": "./components/Link.svelte",
73
+ "./components/Pack.svelte": "./components/Pack.svelte",
73
74
  "./components/Partition.svelte": "./components/Partition.svelte",
74
75
  "./components/Path.svelte": "./components/Path.svelte",
75
76
  "./components/Pattern.svelte": "./components/Pattern.svelte",
@@ -1,8 +1,9 @@
1
1
  import { spring, tweened } from 'svelte/motion';
2
+ export declare type MotionOptions = {
3
+ spring?: boolean | Parameters<typeof spring>[1];
4
+ tweened?: boolean | Parameters<typeof tweened>[1];
5
+ };
2
6
  /**
3
7
  * Convenient wrapper to create a motion store (spring(), tweened()) based on properties, or fall back to basic writable() store
4
8
  */
5
- export declare function createMotionStore(value: any, options: {
6
- spring?: boolean | Parameters<typeof spring>[1];
7
- tweened?: boolean | Parameters<typeof tweened>[1];
8
- }): import("svelte/motion").Tweened<unknown> | import("svelte/motion").Spring<any> | import("svelte/store").Writable<any>;
9
+ export declare function motionStore(value: any, options: MotionOptions): import("svelte/motion").Tweened<unknown> | import("svelte/motion").Spring<any> | import("svelte/store").Writable<any>;
@@ -3,7 +3,7 @@ import { spring, tweened } from 'svelte/motion';
3
3
  /**
4
4
  * Convenient wrapper to create a motion store (spring(), tweened()) based on properties, or fall back to basic writable() store
5
5
  */
6
- export function createMotionStore(value, options) {
6
+ export function motionStore(value, options) {
7
7
  if (options.spring) {
8
8
  return spring(value, options.spring === true ? undefined : options.spring);
9
9
  }
package/utils/scales.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { tweened, spring } from 'svelte/motion';
1
+ import { tweened } from 'svelte/motion';
2
+ import { MotionOptions } from '../stores/motionStore';
2
3
  /**
3
4
  * Implemenation for missing `scaleBand().invert()`
4
5
  *
@@ -18,10 +19,10 @@ export declare function tweenedScale(scale: any, tweenedOptions?: Parameters<typ
18
19
  range: (values: any) => Promise<void>;
19
20
  };
20
21
  /**
21
- * Animate d3-scale as domain and/or range are updated using spring store
22
+ * Create a store wrapper around a d3-scale which interpolates the domain and/or range using `tweened()` or `spring()` stores. Fallbacks to `writable()` if not interpolating
22
23
  */
23
- export declare function springScale(scale: any, springOptions?: Parameters<typeof spring>[1]): {
24
+ export declare function motionScale(scale: any, options: MotionOptions): {
24
25
  subscribe: (this: void, run: import("svelte/store").Subscriber<any>, invalidate?: (value?: any) => void) => import("svelte/store").Unsubscriber;
25
- domain: (values: any) => Promise<void>;
26
- range: (values: any) => Promise<void>;
26
+ domain: (values: any) => void | Promise<void>;
27
+ range: (values: any) => void | Promise<void>;
27
28
  };
package/utils/scales.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { derived } from 'svelte/store';
2
- import { tweened, spring } from 'svelte/motion';
2
+ import { tweened } from 'svelte/motion';
3
+ import { motionStore } from '../stores/motionStore';
3
4
  /**
4
5
  * Implemenation for missing `scaleBand().invert()`
5
6
  *
@@ -44,12 +45,12 @@ export function tweenedScale(scale, tweenedOptions = {}) {
44
45
  };
45
46
  }
46
47
  /**
47
- * Animate d3-scale as domain and/or range are updated using spring store
48
+ * Create a store wrapper around a d3-scale which interpolates the domain and/or range using `tweened()` or `spring()` stores. Fallbacks to `writable()` if not interpolating
48
49
  */
49
- export function springScale(scale, springOptions = {}) {
50
- const springDomain = spring(undefined, springOptions);
51
- const springRange = spring(undefined, springOptions);
52
- const tweenedScale = derived([springDomain, springRange], ([domain, range]) => {
50
+ export function motionScale(scale, options) {
51
+ const domainStore = motionStore(undefined, options);
52
+ const rangeStore = motionStore(undefined, options);
53
+ const tweenedScale = derived([domainStore, rangeStore], ([domain, range]) => {
53
54
  const scaleInstance = scale.domain ? scale : scale(); // support `scaleLinear` or `scaleLinear()` (which could have `.interpolate()` and others set)
54
55
  if (domain) {
55
56
  scaleInstance.domain(domain);
@@ -61,7 +62,7 @@ export function springScale(scale, springOptions = {}) {
61
62
  });
62
63
  return {
63
64
  subscribe: tweenedScale.subscribe,
64
- domain: (values) => springDomain.set(values),
65
- range: (values) => springRange.set(values)
65
+ domain: (values) => domainStore.set(values),
66
+ range: (values) => rangeStore.set(values)
66
67
  };
67
68
  }