layerchart 0.54.0 → 0.55.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.
Files changed (105) hide show
  1. package/dist/components/Arc.svelte +170 -144
  2. package/dist/components/Area.svelte +96 -67
  3. package/dist/components/Area.svelte.d.ts +1 -0
  4. package/dist/components/Axis.svelte +205 -155
  5. package/dist/components/Bar.svelte +72 -45
  6. package/dist/components/Bars.svelte +45 -34
  7. package/dist/components/Blur.svelte +5 -3
  8. package/dist/components/Bounds.svelte +37 -21
  9. package/dist/components/Brush.svelte +269 -112
  10. package/dist/components/Brush.svelte.d.ts +7 -0
  11. package/dist/components/Calendar.svelte +51 -38
  12. package/dist/components/Chart.svelte +295 -74
  13. package/dist/components/Chart.svelte.d.ts +17 -17
  14. package/dist/components/ChartClipPath.svelte +8 -5
  15. package/dist/components/ChartContext.svelte +243 -93
  16. package/dist/components/ChartContext.svelte.d.ts +15 -23
  17. package/dist/components/Circle.svelte +25 -16
  18. package/dist/components/CircleClipPath.svelte +16 -10
  19. package/dist/components/ClipPath.svelte +11 -7
  20. package/dist/components/ColorRamp.svelte +12 -10
  21. package/dist/components/ForceSimulation.svelte +185 -116
  22. package/dist/components/Frame.svelte +10 -6
  23. package/dist/components/GeoCircle.svelte +15 -9
  24. package/dist/components/GeoContext.svelte +109 -62
  25. package/dist/components/GeoEdgeFade.svelte +20 -14
  26. package/dist/components/GeoPath.svelte +107 -69
  27. package/dist/components/GeoPoint.svelte +32 -18
  28. package/dist/components/GeoSpline.svelte +30 -22
  29. package/dist/components/GeoTile.svelte +40 -30
  30. package/dist/components/GeoVisible.svelte +10 -7
  31. package/dist/components/Graticule.svelte +14 -8
  32. package/dist/components/Grid.svelte +75 -48
  33. package/dist/components/Group.svelte +43 -31
  34. package/dist/components/Highlight.svelte +284 -243
  35. package/dist/components/HitCanvas.svelte +75 -42
  36. package/dist/components/Hull.svelte +40 -20
  37. package/dist/components/Labels.svelte +81 -70
  38. package/dist/components/Legend.svelte +105 -74
  39. package/dist/components/Legend.svelte.d.ts +1 -1
  40. package/dist/components/Line.svelte +65 -19
  41. package/dist/components/Line.svelte.d.ts +13 -1
  42. package/dist/components/LinearGradient.svelte +21 -15
  43. package/dist/components/Link.svelte +94 -22
  44. package/dist/components/Link.svelte.d.ts +17 -1
  45. package/dist/components/Marker.svelte +81 -0
  46. package/dist/components/Marker.svelte.d.ts +28 -0
  47. package/dist/components/MonthPath.svelte +23 -16
  48. package/dist/components/MotionPath.svelte +34 -25
  49. package/dist/components/Pack.svelte +21 -14
  50. package/dist/components/Partition.svelte +35 -20
  51. package/dist/components/Pattern.svelte +8 -6
  52. package/dist/components/Pie.svelte +76 -57
  53. package/dist/components/Point.svelte +11 -7
  54. package/dist/components/Points.svelte +178 -143
  55. package/dist/components/RadialGradient.svelte +25 -18
  56. package/dist/components/Rect.svelte +33 -19
  57. package/dist/components/RectClipPath.svelte +16 -11
  58. package/dist/components/Rule.svelte +50 -42
  59. package/dist/components/Sankey.svelte +55 -30
  60. package/dist/components/Spline.svelte +167 -96
  61. package/dist/components/Spline.svelte.d.ts +15 -0
  62. package/dist/components/Text.svelte +137 -104
  63. package/dist/components/Threshold.svelte +18 -7
  64. package/dist/components/TileImage.svelte +56 -50
  65. package/dist/components/TransformContext.svelte +235 -135
  66. package/dist/components/TransformControls.svelte +57 -29
  67. package/dist/components/TransformControls.svelte.d.ts +1 -1
  68. package/dist/components/Tree.svelte +33 -23
  69. package/dist/components/Treemap.svelte +69 -41
  70. package/dist/components/Voronoi.svelte +55 -28
  71. package/dist/components/charts/AreaChart.svelte +138 -87
  72. package/dist/components/charts/AreaChart.svelte.d.ts +4 -4
  73. package/dist/components/charts/BarChart.svelte +179 -114
  74. package/dist/components/charts/BarChart.svelte.d.ts +4 -4
  75. package/dist/components/charts/LineChart.svelte +97 -53
  76. package/dist/components/charts/LineChart.svelte.d.ts +4 -4
  77. package/dist/components/charts/PieChart.svelte +104 -54
  78. package/dist/components/charts/PieChart.svelte.d.ts +3 -3
  79. package/dist/components/charts/ScatterChart.svelte +83 -48
  80. package/dist/components/charts/ScatterChart.svelte.d.ts +4 -4
  81. package/dist/components/layout/Canvas.svelte +63 -43
  82. package/dist/components/layout/Html.svelte +28 -18
  83. package/dist/components/layout/Svg.svelte +47 -32
  84. package/dist/components/tooltip/Tooltip.svelte +137 -91
  85. package/dist/components/tooltip/Tooltip.svelte.d.ts +1 -1
  86. package/dist/components/tooltip/TooltipContext.svelte +315 -249
  87. package/dist/components/tooltip/TooltipHeader.svelte +9 -3
  88. package/dist/components/tooltip/TooltipItem.svelte +17 -9
  89. package/dist/components/tooltip/TooltipList.svelte +2 -1
  90. package/dist/components/tooltip/TooltipSeparator.svelte +3 -2
  91. package/dist/docs/Blockquote.svelte +4 -3
  92. package/dist/docs/Code.svelte +15 -8
  93. package/dist/docs/CurveMenuField.svelte +17 -12
  94. package/dist/docs/GeoDebug.svelte +13 -9
  95. package/dist/docs/Header1.svelte +2 -1
  96. package/dist/docs/Json.svelte +6 -4
  97. package/dist/docs/Layout.svelte +6 -6
  98. package/dist/docs/PathDataMenuField.svelte +52 -44
  99. package/dist/docs/Preview.svelte +39 -33
  100. package/dist/docs/TilesetField.svelte +80 -62
  101. package/dist/docs/TransformDebug.svelte +8 -5
  102. package/dist/docs/ViewSourceButton.svelte +13 -9
  103. package/dist/stores/motionStore.d.ts +1 -1
  104. package/dist/utils/scales.d.ts +3 -3
  105. package/package.json +29 -30
@@ -1,134 +1,167 @@
1
- <script>import { tick } from 'svelte';
2
- import { cls } from '@layerstack/tailwind';
3
- import { getStringWidth } from '../utils/string.js';
4
- import { motionStore } from '../stores/motionStore.js';
5
- /*
6
- TODO:
7
- - [ ] Handle styled text (use <slot /> to measure?)
8
- - [ ] Simplify by using `alignment-baseline` / `dominant-baseline`, rework multiline or drop support, etc
9
- - https://svelte.dev/repl/f12d3003313a43ba8a0be53e5786f1c7?version=3.44.3
10
- - https://observablehq.com/@neocartocnrs/cheat-sheet-on-texts-in-svg
11
-
12
- Reference:
13
- - https://bl.ocks.org/mbostock/7555321
14
- - https://github.com/airbnb/visx/blob/master/packages/visx-text/src/Text.tsx
15
- - https://airbnb.io/visx/text
16
- - https://github.com/airbnb/visx/blob/master/packages/visx-demo/src/pages/text.tsx
17
- */
18
- /** text value */
19
- export let value = 0;
20
- /** Maximum width to occupy (approximate as words are not split) */
21
- export let width = undefined;
22
- /** x position of the text */
23
- export let x = 0;
24
- export let initialX = x;
25
- /** y position of the text */
26
- export let y = 0;
27
- export let initialY = y;
28
- /** dx offset of the text */
29
- export let dx = 0;
30
- /** dy offset of the text */
31
- export let dy = 0;
32
- /** Desired "line height" of the text, implemented as y offsets */
33
- export let lineHeight = '1em';
34
- /** Cap height of the text */
35
- export let capHeight = '0.71em'; // Magic number from d3
36
- /** Whether to scale the fontSize to accommodate the specified width */
37
- export let scaleToFit = false;
38
- /** Horizontal text anchor */
39
- export let textAnchor = 'start';
40
- /** Vertical text anchor */
41
- export let verticalAnchor = 'end'; // default SVG behavior
42
- /** Rotational angle of the text */
43
- export let rotate = undefined;
44
- let wordsByLines = [];
45
- let wordsWithWidth = [];
46
- let spaceWidth = 0;
47
- let style = undefined; // TODO: read from DOM?
48
- $: words = value != null ? value.toString().split(/(?:(?!\u00A0+)\s+)/) : [];
49
- $: wordsWithWidth = words.map((word) => ({
1
+ <script lang="ts">
2
+ import { tick } from 'svelte';
3
+ import type { spring as springStore, tweened as tweenedStore } from 'svelte/motion';
4
+ import { cls } from '@layerstack/tailwind';
5
+
6
+ import { getStringWidth } from '../utils/string.js';
7
+ import { motionStore } from '../stores/motionStore.js';
8
+
9
+ /*
10
+ TODO:
11
+ - [ ] Handle styled text (use <slot /> to measure?)
12
+ - [ ] Simplify by using `alignment-baseline` / `dominant-baseline`, rework multiline or drop support, etc
13
+ - https://svelte.dev/repl/f12d3003313a43ba8a0be53e5786f1c7?version=3.44.3
14
+ - https://observablehq.com/@neocartocnrs/cheat-sheet-on-texts-in-svg
15
+
16
+ Reference:
17
+ - https://bl.ocks.org/mbostock/7555321
18
+ - https://github.com/airbnb/visx/blob/master/packages/visx-text/src/Text.tsx
19
+ - https://airbnb.io/visx/text
20
+ - https://github.com/airbnb/visx/blob/master/packages/visx-demo/src/pages/text.tsx
21
+ */
22
+
23
+ /** text value */
24
+ export let value: string | number = 0;
25
+
26
+ /** Maximum width to occupy (approximate as words are not split) */
27
+ export let width: number | undefined = undefined;
28
+
29
+ /** x position of the text */
30
+ export let x: string | number = 0;
31
+ export let initialX = x;
32
+
33
+ /** y position of the text */
34
+ export let y: string | number = 0;
35
+ export let initialY = y;
36
+
37
+ /** dx offset of the text */
38
+ export let dx: string | number = 0;
39
+
40
+ /** dy offset of the text */
41
+ export let dy: string | number = 0;
42
+
43
+ /** Desired "line height" of the text, implemented as y offsets */
44
+ export let lineHeight = '1em';
45
+
46
+ /** Cap height of the text */
47
+ export let capHeight = '0.71em'; // Magic number from d3
48
+
49
+ /** Whether to scale the fontSize to accommodate the specified width */
50
+ export let scaleToFit: boolean = false;
51
+
52
+ /** Horizontal text anchor */
53
+ export let textAnchor: 'start' | 'middle' | 'end' | 'inherit' = 'start';
54
+
55
+ /** Vertical text anchor */
56
+ export let verticalAnchor: 'start' | 'middle' | 'end' | 'inherit' = 'end'; // default SVG behavior
57
+
58
+ /** Rotational angle of the text */
59
+ export let rotate: number | undefined = undefined;
60
+
61
+ let wordsByLines: { words: string[]; width?: number }[] = [];
62
+ let wordsWithWidth: { word: string; width: number }[] = [];
63
+ let spaceWidth: number = 0;
64
+
65
+ let style: CSSStyleDeclaration | undefined = undefined; // TODO: read from DOM?
66
+
67
+ $: words = value != null ? value.toString().split(/(?:(?!\u00A0+)\s+)/) : [];
68
+
69
+ $: wordsWithWidth = words.map((word) => ({
50
70
  word,
51
71
  width: getStringWidth(word, style) || 0,
52
- }));
53
- $: spaceWidth = getStringWidth('\u00A0', style) || 0;
54
- $: wordsByLines = wordsWithWidth.reduce((result, item) => {
72
+ }));
73
+
74
+ $: spaceWidth = getStringWidth('\u00A0', style) || 0;
75
+
76
+ $: wordsByLines = wordsWithWidth.reduce((result: typeof wordsByLines, item) => {
55
77
  const currentLine = result[result.length - 1];
56
- if (currentLine &&
57
- (width == null || scaleToFit || (currentLine.width || 0) + item.width + spaceWidth < width)) {
58
- // Word can be added to an existing line
59
- currentLine.words.push(item.word);
60
- currentLine.width = currentLine.width || 0;
61
- currentLine.width += item.width + spaceWidth;
62
- }
63
- else {
64
- // Add first word to line or word is too long to scaleToFit on existing line
65
- const newLine = { words: [item.word], width: item.width };
66
- result.push(newLine);
78
+
79
+ if (
80
+ currentLine &&
81
+ (width == null || scaleToFit || (currentLine.width || 0) + item.width + spaceWidth < width)
82
+ ) {
83
+ // Word can be added to an existing line
84
+ currentLine.words.push(item.word);
85
+ currentLine.width = currentLine.width || 0;
86
+ currentLine.width += item.width + spaceWidth;
87
+ } else {
88
+ // Add first word to line or word is too long to scaleToFit on existing line
89
+ const newLine = { words: [item.word], width: item.width };
90
+ result.push(newLine);
67
91
  }
92
+
68
93
  return result;
69
- }, []);
70
- $: lines = wordsByLines.length;
71
- /**
72
- * Convert css value to pixel value (ex. 0.71em => 11.36)
73
- */
74
- function getPixelValue(cssValue) {
94
+ }, []);
95
+ $: lines = wordsByLines.length;
96
+
97
+ /**
98
+ * Convert css value to pixel value (ex. 0.71em => 11.36)
99
+ */
100
+ function getPixelValue(cssValue: string) {
75
101
  // TODO: Properly measure pixel values using DOM (handle inherited font size, zoom, etc)
76
102
  // @ts-expect-error
77
103
  const [match, value, units] = cssValue.match(/([\d.]+)(\D+)/);
78
104
  const number = Number(value);
79
105
  switch (units) {
80
- case 'px':
81
- return number;
82
- case 'em':
83
- case 'rem':
84
- return number * 16;
85
- default:
86
- return 0;
106
+ case 'px':
107
+ return number;
108
+ case 'em':
109
+ case 'rem':
110
+ return number * 16;
111
+ default:
112
+ return 0;
87
113
  }
88
- }
89
- let startDy = 0;
90
- $: if (verticalAnchor === 'start') {
114
+ }
115
+
116
+ let startDy = 0;
117
+ $: if (verticalAnchor === 'start') {
91
118
  startDy = getPixelValue(capHeight);
92
- }
93
- else if (verticalAnchor === 'middle') {
119
+ } else if (verticalAnchor === 'middle') {
94
120
  startDy = ((lines - 1) / 2) * -getPixelValue(lineHeight) + getPixelValue(capHeight) / 2;
95
- }
96
- else {
121
+ } else {
97
122
  startDy = (lines - 1) * -getPixelValue(lineHeight);
98
- }
99
- let scaleTransform = '';
100
- $: if (scaleToFit &&
123
+ }
124
+
125
+ let scaleTransform = '';
126
+ $: if (
127
+ scaleToFit &&
101
128
  lines > 0 &&
102
129
  typeof x == 'number' &&
103
130
  typeof y == 'number' &&
104
- typeof width == 'number') {
131
+ typeof width == 'number'
132
+ ) {
105
133
  const lineWidth = wordsByLines[0].width || 1;
106
134
  const sx = width / lineWidth;
107
135
  const sy = sx;
108
136
  const originX = x - sx * x;
109
137
  const originY = y - sy * y;
110
138
  scaleTransform = `matrix(${sx}, 0, 0, ${sy}, ${originX}, ${originY})`;
111
- }
112
- else {
139
+ } else {
113
140
  scaleTransform = '';
114
- }
115
- $: rotateTransform = rotate ? `rotate(${rotate}, ${x}, ${y})` : '';
116
- $: transform = `${scaleTransform} ${rotateTransform}`;
117
- function isValidXOrY(xOrY) {
141
+ }
142
+ $: rotateTransform = rotate ? `rotate(${rotate}, ${x}, ${y})` : '';
143
+
144
+ $: transform = `${scaleTransform} ${rotateTransform}`;
145
+
146
+ function isValidXOrY(xOrY: string | number | undefined) {
118
147
  return (
119
- // number that is not NaN or Infinity
120
- (typeof xOrY === 'number' && Number.isFinite(xOrY)) ||
121
- // for percentage
122
- typeof xOrY === 'string');
123
- }
124
- export let spring = undefined;
125
- export let tweened = undefined;
126
- let tweened_x = motionStore(initialX, { spring, tweened });
127
- let tweened_y = motionStore(initialY, { spring, tweened });
128
- $: tick().then(() => {
148
+ // number that is not NaN or Infinity
149
+ (typeof xOrY === 'number' && Number.isFinite(xOrY)) ||
150
+ // for percentage
151
+ typeof xOrY === 'string'
152
+ );
153
+ }
154
+
155
+ export let spring: boolean | Parameters<typeof springStore>[1] = undefined;
156
+ export let tweened: boolean | Parameters<typeof tweenedStore>[1] = undefined;
157
+
158
+ let tweened_x = motionStore(initialX, { spring, tweened });
159
+ let tweened_y = motionStore(initialY, { spring, tweened });
160
+
161
+ $: tick().then(() => {
129
162
  tweened_x.set(x);
130
163
  tweened_y.set(y);
131
- });
164
+ });
132
165
  </script>
133
166
 
134
167
  <!-- `overflow: visible` allow contents to be shown outside element -->
@@ -1,10 +1,21 @@
1
- <script>import { min } from 'd3-array';
2
- import { chartContext } from './ChartContext.svelte';
3
- import Area from './Area.svelte';
4
- import ClipPath from './ClipPath.svelte';
5
- const { y, yDomain } = chartContext();
6
- export let curve = undefined;
7
- export let defined = undefined;
1
+ <script lang="ts">
2
+ /*
3
+ See also:
4
+ - https://observablehq.com/@d3/difference-chart
5
+ - https://github.com/airbnb/visx/issues/245
6
+ */
7
+ import type { ComponentProps } from 'svelte';
8
+ import type { CurveFactory } from 'd3-shape';
9
+ import { min } from 'd3-array';
10
+
11
+ import { chartContext } from './ChartContext.svelte';
12
+ import Area from './Area.svelte';
13
+ import ClipPath from './ClipPath.svelte';
14
+
15
+ const { y, yDomain } = chartContext();
16
+
17
+ export let curve: CurveFactory | undefined = undefined;
18
+ export let defined: ComponentProps<Area>['defined'] | undefined = undefined;
8
19
  </script>
9
20
 
10
21
  <!-- Recreate on curve change as otherwise is 1 state change behind for some reason -->
@@ -1,61 +1,67 @@
1
- <script context="module">"use strict";
2
- let tileCache = new Map();
1
+ <script context="module" lang="ts">
2
+ let tileCache = new Map<string, Promise<string>>();
3
3
  </script>
4
4
 
5
- <script>import Text from './Text.svelte';
6
- export let x;
7
- export let y;
8
- export let z;
9
- /** translate x */
10
- export let tx;
11
- /** translate y */
12
- export let ty;
13
- export let scale;
14
- export let disableCache = false;
15
- export let debug = false;
16
- export let url;
17
- // if disable cache, set href immediately, otherwise set from cache / dataUri
18
- let href = disableCache ? url(x, y, z) : '';
19
- function loadImage(url) {
5
+ <script lang="ts">
6
+ import Text from './Text.svelte';
7
+
8
+ export let x: number;
9
+ export let y: number;
10
+ export let z: number;
11
+ /** translate x */
12
+ export let tx: number;
13
+ /** translate y */
14
+ export let ty: number;
15
+ export let scale: number;
16
+
17
+ export let disableCache = false;
18
+ export let debug = false;
19
+
20
+ export let url: (x: number, y: number, z: number) => string;
21
+
22
+ // if disable cache, set href immediately, otherwise set from cache / dataUri
23
+ let href = disableCache ? url(x, y, z) : '';
24
+ function loadImage(url: string) {
20
25
  // const key = [x, y, z].join('-');
21
26
  const key = url;
27
+
22
28
  if (tileCache.has(key)) {
23
- tileCache.get(key)?.then((dataUri) => {
24
- // console.log('from cache', { x, y, z });
25
- href = dataUri;
26
- });
27
- }
28
- else {
29
- const promise = new Promise((resolve, reject) => {
30
- const img = new Image();
31
- img.crossOrigin = 'anonymous';
32
- img.onload = function () {
33
- var canvas = document.createElement('canvas');
34
- var context = canvas.getContext('2d');
35
- // @ts-expect-error
36
- canvas.height = this.naturalHeight;
37
- // @ts-expect-error
38
- canvas.width = this.naturalWidth;
39
- // @ts-expect-error
40
- context.drawImage(this, 0, 0);
41
- var dataUri = canvas.toDataURL('image/jpeg');
42
- // console.log('from load', { x, y, z });
43
- href = dataUri;
44
- resolve(dataUri);
45
- };
46
- img.onerror = (err) => {
47
- tileCache.delete(key);
48
- reject(err);
49
- };
50
- img.src = url;
51
- });
52
- tileCache.set(key, promise);
29
+ tileCache.get(key)?.then((dataUri) => {
30
+ // console.log('from cache', { x, y, z });
31
+ href = dataUri;
32
+ });
33
+ } else {
34
+ const promise = new Promise<string>((resolve, reject) => {
35
+ const img = new Image();
36
+ img.crossOrigin = 'anonymous';
37
+ img.onload = function () {
38
+ var canvas = document.createElement('canvas');
39
+ var context = canvas.getContext('2d')!;
40
+ // @ts-expect-error
41
+ canvas.height = this.naturalHeight;
42
+ // @ts-expect-error
43
+ canvas.width = this.naturalWidth;
44
+ // @ts-expect-error
45
+ context.drawImage(this, 0, 0);
46
+ var dataUri = canvas.toDataURL('image/jpeg');
47
+ // console.log('from load', { x, y, z });
48
+ href = dataUri;
49
+ resolve(dataUri);
50
+ };
51
+ img.onerror = (err) => {
52
+ tileCache.delete(key);
53
+ reject(err);
54
+ };
55
+ img.src = url;
56
+ });
57
+ tileCache.set(key, promise);
53
58
  }
54
- }
55
- $: if (!disableCache) {
59
+ }
60
+
61
+ $: if (!disableCache) {
56
62
  // load using cache
57
63
  loadImage(url(x, y, z));
58
- }
64
+ }
59
65
  </script>
60
66
 
61
67
  <!-- To avoid aliasing artifacts (thin white lines) between tiles, two layers of tiles are drawn, with the lower layer’s tiles enlarged by one pixel -->