layerchart 2.0.0-next.1 → 2.0.0-next.3

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 (266) hide show
  1. package/dist/actions/movable.d.ts +28 -0
  2. package/dist/actions/movable.js +91 -0
  3. package/dist/components/AnnotationLine.svelte +143 -0
  4. package/dist/components/AnnotationLine.svelte.d.ts +30 -0
  5. package/dist/components/AnnotationPoint.svelte +119 -0
  6. package/dist/components/AnnotationPoint.svelte.d.ts +34 -0
  7. package/dist/components/AnnotationRange.svelte +147 -0
  8. package/dist/components/AnnotationRange.svelte.d.ts +40 -0
  9. package/dist/components/Arc.svelte +344 -151
  10. package/dist/components/Arc.svelte.d.ts +138 -0
  11. package/dist/components/Area.svelte +165 -149
  12. package/dist/components/Area.svelte.d.ts +45 -0
  13. package/dist/components/Axis.svelte +283 -180
  14. package/dist/components/Axis.svelte.d.ts +117 -0
  15. package/dist/components/Bar.svelte +166 -107
  16. package/dist/components/Bar.svelte.d.ts +51 -0
  17. package/dist/components/Bars.svelte +56 -67
  18. package/dist/components/Bars.svelte.d.ts +27 -0
  19. package/dist/components/Blur.svelte +42 -12
  20. package/dist/components/Blur.svelte.d.ts +23 -21
  21. package/dist/components/Bounds.svelte +49 -19
  22. package/dist/components/Bounds.svelte.d.ts +24 -50
  23. package/dist/components/BrushContext.svelte +296 -168
  24. package/dist/components/BrushContext.svelte.d.ts +97 -65
  25. package/dist/components/Calendar.svelte +116 -59
  26. package/dist/components/Calendar.svelte.d.ts +50 -31
  27. package/dist/components/Chart.svelte +1289 -398
  28. package/dist/components/Chart.svelte.d.ts +535 -410
  29. package/dist/components/ChartClipPath.svelte +37 -15
  30. package/dist/components/ChartClipPath.svelte.d.ts +21 -19
  31. package/dist/components/Circle.svelte +124 -85
  32. package/dist/components/Circle.svelte.d.ts +52 -0
  33. package/dist/components/CircleClipPath.svelte +76 -16
  34. package/dist/components/CircleClipPath.svelte.d.ts +46 -0
  35. package/dist/components/ClipPath.svelte +71 -21
  36. package/dist/components/ClipPath.svelte.d.ts +40 -27
  37. package/dist/components/ColorRamp.svelte +75 -9
  38. package/dist/components/ColorRamp.svelte.d.ts +37 -19
  39. package/dist/components/ComputedStyles.svelte +17 -5
  40. package/dist/components/ComputedStyles.svelte.d.ts +11 -19
  41. package/dist/components/Connector.svelte +149 -0
  42. package/dist/components/Connector.svelte.d.ts +51 -0
  43. package/dist/components/Dagre.svelte +211 -122
  44. package/dist/components/Dagre.svelte.d.ts +119 -56
  45. package/dist/components/ForceSimulation.svelte +215 -90
  46. package/dist/components/ForceSimulation.svelte.d.ts +82 -35
  47. package/dist/components/Frame.svelte +33 -13
  48. package/dist/components/Frame.svelte.d.ts +13 -17
  49. package/dist/components/GeoCircle.svelte +29 -16
  50. package/dist/components/GeoCircle.svelte.d.ts +22 -24
  51. package/dist/components/GeoContext.svelte +113 -72
  52. package/dist/components/GeoContext.svelte.d.ts +49 -41
  53. package/dist/components/GeoEdgeFade.svelte +49 -13
  54. package/dist/components/GeoEdgeFade.svelte.d.ts +17 -19
  55. package/dist/components/GeoPath.svelte +157 -127
  56. package/dist/components/GeoPath.svelte.d.ts +48 -36
  57. package/dist/components/GeoPoint.svelte +52 -20
  58. package/dist/components/GeoPoint.svelte.d.ts +25 -22
  59. package/dist/components/GeoSpline.svelte +75 -26
  60. package/dist/components/GeoSpline.svelte.d.ts +29 -20
  61. package/dist/components/GeoTile.svelte +100 -49
  62. package/dist/components/GeoTile.svelte.d.ts +38 -23
  63. package/dist/components/GeoVisible.svelte +17 -9
  64. package/dist/components/GeoVisible.svelte.d.ts +10 -18
  65. package/dist/components/Graticule.svelte +30 -14
  66. package/dist/components/Graticule.svelte.d.ts +11 -52
  67. package/dist/components/Grid.svelte +230 -117
  68. package/dist/components/Grid.svelte.d.ts +71 -0
  69. package/dist/components/Group.svelte +173 -106
  70. package/dist/components/Group.svelte.d.ts +81 -0
  71. package/dist/components/Highlight.svelte +410 -308
  72. package/dist/components/Highlight.svelte.d.ts +107 -0
  73. package/dist/components/Hull.svelte +97 -46
  74. package/dist/components/Hull.svelte.d.ts +40 -30
  75. package/dist/components/Labels.svelte +127 -47
  76. package/dist/components/Labels.svelte.d.ts +70 -27
  77. package/dist/components/Legend.svelte +374 -190
  78. package/dist/components/Legend.svelte.d.ts +95 -44
  79. package/dist/components/Line.svelte +163 -125
  80. package/dist/components/Line.svelte.d.ts +75 -0
  81. package/dist/components/LinearGradient.svelte +153 -78
  82. package/dist/components/LinearGradient.svelte.d.ts +66 -31
  83. package/dist/components/Link.svelte +160 -104
  84. package/dist/components/Link.svelte.d.ts +54 -0
  85. package/dist/components/Marker.svelte +100 -39
  86. package/dist/components/Marker.svelte.d.ts +59 -27
  87. package/dist/components/MarkerWrapper.svelte +35 -0
  88. package/dist/components/MarkerWrapper.svelte.d.ts +18 -0
  89. package/dist/components/MonthPath.svelte +65 -20
  90. package/dist/components/MonthPath.svelte.d.ts +23 -17
  91. package/dist/components/MotionPath.svelte +80 -24
  92. package/dist/components/MotionPath.svelte.d.ts +46 -27
  93. package/dist/components/Pack.svelte +53 -17
  94. package/dist/components/Pack.svelte.d.ts +42 -21
  95. package/dist/components/Partition.svelte +64 -22
  96. package/dist/components/Partition.svelte.d.ts +49 -26
  97. package/dist/components/Pattern.svelte +297 -11
  98. package/dist/components/Pattern.svelte.d.ts +103 -19
  99. package/dist/components/Pie.svelte +122 -76
  100. package/dist/components/Pie.svelte.d.ts +65 -51
  101. package/dist/components/Point.svelte +20 -9
  102. package/dist/components/Point.svelte.d.ts +16 -20
  103. package/dist/components/Points.svelte +148 -137
  104. package/dist/components/Points.svelte.d.ts +45 -34
  105. package/dist/components/RadialGradient.svelte +143 -70
  106. package/dist/components/RadialGradient.svelte.d.ts +69 -31
  107. package/dist/components/Rect.svelte +121 -102
  108. package/dist/components/Rect.svelte.d.ts +36 -0
  109. package/dist/components/RectClipPath.svelte +82 -18
  110. package/dist/components/RectClipPath.svelte.d.ts +55 -0
  111. package/dist/components/Rule.svelte +107 -63
  112. package/dist/components/Rule.svelte.d.ts +40 -19
  113. package/dist/components/Sankey.svelte +132 -55
  114. package/dist/components/Sankey.svelte.d.ts +61 -31
  115. package/dist/components/Spline.svelte +281 -218
  116. package/dist/components/Spline.svelte.d.ts +95 -0
  117. package/dist/components/Text.svelte +437 -176
  118. package/dist/components/Text.svelte.d.ts +130 -0
  119. package/dist/components/Threshold.svelte +48 -16
  120. package/dist/components/Threshold.svelte.d.ts +29 -31
  121. package/dist/components/TileImage.svelte +103 -30
  122. package/dist/components/TileImage.svelte.d.ts +48 -23
  123. package/dist/components/TransformContext.svelte +365 -171
  124. package/dist/components/TransformControls.svelte +50 -26
  125. package/dist/components/TransformControls.svelte.d.ts +27 -19
  126. package/dist/components/Tree.svelte +74 -33
  127. package/dist/components/Tree.svelte.d.ts +42 -30
  128. package/dist/components/Treemap.svelte +119 -42
  129. package/dist/components/Treemap.svelte.d.ts +75 -27
  130. package/dist/components/Voronoi.svelte +108 -76
  131. package/dist/components/Voronoi.svelte.d.ts +40 -41
  132. package/dist/components/charts/ArcChart.svelte +464 -0
  133. package/dist/components/charts/ArcChart.svelte.d.ts +90 -0
  134. package/dist/components/charts/AreaChart.svelte +450 -393
  135. package/dist/components/charts/AreaChart.svelte.d.ts +61 -0
  136. package/dist/components/charts/BarChart.svelte +454 -389
  137. package/dist/components/charts/BarChart.svelte.d.ts +76 -0
  138. package/dist/components/charts/ChartAnnotations.svelte +37 -0
  139. package/dist/components/charts/ChartAnnotations.svelte.d.ts +10 -0
  140. package/dist/components/charts/DefaultTooltip.svelte +60 -0
  141. package/dist/components/charts/DefaultTooltip.svelte.d.ts +10 -0
  142. package/dist/components/charts/LineChart.svelte +369 -314
  143. package/dist/components/charts/LineChart.svelte.d.ts +53 -0
  144. package/dist/components/charts/PieChart.svelte +458 -316
  145. package/dist/components/charts/PieChart.svelte.d.ts +137 -353
  146. package/dist/components/charts/ScatterChart.svelte +334 -296
  147. package/dist/components/charts/ScatterChart.svelte.d.ts +39 -0
  148. package/dist/components/charts/index.d.ts +8 -0
  149. package/dist/components/charts/index.js +7 -0
  150. package/dist/components/charts/types.d.ts +253 -0
  151. package/dist/components/charts/utils.svelte.d.ts +30 -0
  152. package/dist/components/charts/utils.svelte.js +55 -0
  153. package/dist/components/index.d.ts +76 -4
  154. package/dist/components/index.js +76 -5
  155. package/dist/components/layout/Canvas.svelte +321 -155
  156. package/dist/components/layout/Canvas.svelte.d.ts +104 -55
  157. package/dist/components/layout/Html.svelte +82 -42
  158. package/dist/components/layout/Html.svelte.d.ts +39 -28
  159. package/dist/components/layout/Layer.svelte +39 -0
  160. package/dist/components/layout/Layer.svelte.d.ts +17 -0
  161. package/dist/components/layout/Svg.svelte +122 -70
  162. package/dist/components/layout/Svg.svelte.d.ts +53 -34
  163. package/dist/components/layout/WebGL.svelte +135 -0
  164. package/dist/components/layout/WebGL.svelte.d.ts +50 -0
  165. package/dist/components/tooltip/Tooltip.svelte +246 -78
  166. package/dist/components/tooltip/Tooltip.svelte.d.ts +149 -31
  167. package/dist/components/tooltip/TooltipContext.svelte +409 -271
  168. package/dist/components/tooltip/TooltipContext.svelte.d.ts +86 -55
  169. package/dist/components/tooltip/TooltipHeader.svelte +100 -11
  170. package/dist/components/tooltip/TooltipHeader.svelte.d.ts +43 -23
  171. package/dist/components/tooltip/TooltipItem.svelte +167 -27
  172. package/dist/components/tooltip/TooltipItem.svelte.d.ts +63 -31
  173. package/dist/components/tooltip/TooltipList.svelte +22 -3
  174. package/dist/components/tooltip/TooltipList.svelte.d.ts +6 -17
  175. package/dist/components/tooltip/TooltipSeparator.svelte +27 -1
  176. package/dist/components/tooltip/TooltipSeparator.svelte.d.ts +6 -15
  177. package/dist/components/tooltip/index.d.ts +6 -0
  178. package/dist/components/tooltip/index.js +6 -0
  179. package/dist/components/tooltip/tooltipMetaContext.d.ts +79 -0
  180. package/dist/components/tooltip/tooltipMetaContext.js +139 -0
  181. package/dist/components/types.d.ts +1 -0
  182. package/dist/components/types.js +1 -0
  183. package/dist/docs/Blockquote.svelte.d.ts +18 -14
  184. package/dist/docs/Code.svelte.d.ts +26 -22
  185. package/dist/docs/ConnectorSweepMenuField.svelte +17 -0
  186. package/dist/docs/ConnectorSweepMenuField.svelte.d.ts +7 -0
  187. package/dist/docs/ConnectorTypeMenuField.svelte +17 -0
  188. package/dist/docs/ConnectorTypeMenuField.svelte.d.ts +7 -0
  189. package/dist/docs/CurveMenuField.svelte +14 -3
  190. package/dist/docs/CurveMenuField.svelte.d.ts +9 -18
  191. package/dist/docs/GeoDebug.svelte +47 -42
  192. package/dist/docs/GeoDebug.svelte.d.ts +4 -16
  193. package/dist/docs/Header1.svelte.d.ts +27 -16
  194. package/dist/docs/Json.svelte.d.ts +20 -16
  195. package/dist/docs/Layout.svelte.d.ts +18 -13
  196. package/dist/docs/Link.svelte.d.ts +33 -21
  197. package/dist/docs/PathDataMenuField.svelte +14 -10
  198. package/dist/docs/PathDataMenuField.svelte.d.ts +8 -18
  199. package/dist/docs/Preview.svelte +20 -7
  200. package/dist/docs/Preview.svelte.d.ts +12 -22
  201. package/dist/docs/TilesetField.svelte.d.ts +21 -17
  202. package/dist/docs/TransformDebug.svelte +5 -6
  203. package/dist/docs/TransformDebug.svelte.d.ts +18 -14
  204. package/dist/docs/ViewSourceButton.svelte.d.ts +21 -17
  205. package/dist/types/d3-shape-extentions.d.ts +7 -0
  206. package/dist/utils/afterTick.d.ts +5 -0
  207. package/dist/utils/afterTick.js +8 -0
  208. package/dist/utils/arcText.svelte.d.ts +57 -0
  209. package/dist/utils/arcText.svelte.js +262 -0
  210. package/dist/utils/array.d.ts +9 -1
  211. package/dist/utils/array.js +13 -0
  212. package/dist/utils/attributes.d.ts +29 -0
  213. package/dist/utils/attributes.js +40 -0
  214. package/dist/utils/canvas.js +47 -10
  215. package/dist/utils/chart.d.ts +78 -0
  216. package/dist/utils/chart.js +512 -0
  217. package/dist/utils/color.d.ts +1 -0
  218. package/dist/utils/color.js +8 -0
  219. package/dist/utils/common.d.ts +3 -5
  220. package/dist/utils/common.js +3 -2
  221. package/dist/utils/connectorUtils.d.ts +21 -0
  222. package/dist/utils/connectorUtils.js +111 -0
  223. package/dist/utils/createId.d.ts +7 -0
  224. package/dist/utils/createId.js +9 -0
  225. package/dist/utils/debug.d.ts +1 -0
  226. package/dist/utils/debug.js +84 -0
  227. package/dist/utils/filterObject.d.ts +9 -0
  228. package/dist/utils/filterObject.js +12 -0
  229. package/dist/utils/graph/dagre.d.ts +34 -0
  230. package/dist/utils/graph/dagre.js +78 -0
  231. package/dist/utils/graph/dagre.test.d.ts +1 -0
  232. package/dist/utils/{graph.test.js → graph/dagre.test.js} +19 -33
  233. package/dist/utils/graph/sankey.d.ts +28 -0
  234. package/dist/utils/{graph.js → graph/sankey.js} +13 -41
  235. package/dist/utils/index.d.ts +3 -1
  236. package/dist/utils/index.js +3 -1
  237. package/dist/utils/key.svelte.d.ts +3 -0
  238. package/dist/utils/key.svelte.js +11 -0
  239. package/dist/utils/legendPayload.d.ts +7 -0
  240. package/dist/utils/legendPayload.js +8 -0
  241. package/dist/utils/motion.svelte.d.ts +140 -0
  242. package/dist/utils/motion.svelte.js +180 -0
  243. package/dist/utils/motion.test.d.ts +1 -0
  244. package/dist/utils/motion.test.js +213 -0
  245. package/dist/utils/{rect.d.ts → rect.svelte.d.ts} +7 -4
  246. package/dist/utils/rect.svelte.js +105 -0
  247. package/dist/utils/scales.svelte.d.ts +90 -0
  248. package/dist/utils/{scales.js → scales.svelte.js} +100 -39
  249. package/dist/utils/stack.d.ts +2 -3
  250. package/dist/utils/stack.js +1 -1
  251. package/dist/utils/string.js +87 -0
  252. package/dist/utils/ticks.d.ts +8 -2
  253. package/dist/utils/ticks.js +28 -0
  254. package/dist/utils/ticks.test.d.ts +1 -0
  255. package/dist/utils/ticks.test.js +67 -0
  256. package/dist/utils/types.d.ts +81 -0
  257. package/package.json +21 -19
  258. package/dist/components/ChartContext.svelte +0 -295
  259. package/dist/components/ChartContext.svelte.d.ts +0 -139
  260. package/dist/components/TransformContext.svelte.d.ts +0 -158
  261. package/dist/stores/motionStore.d.ts +0 -30
  262. package/dist/stores/motionStore.js +0 -62
  263. package/dist/utils/graph.d.ts +0 -37
  264. package/dist/utils/rect.js +0 -107
  265. package/dist/utils/scales.d.ts +0 -66
  266. /package/dist/{utils/graph.test.d.ts → components/charts/types.js} +0 -0
@@ -1,39 +1,131 @@
1
- <script lang="ts">
2
- import { forceSimulation, type Force } from 'd3-force';
3
- import { chartContext } from './ChartContext.svelte';
4
-
5
- const { data } = chartContext();
6
-
7
- // MARK: Public Props
1
+ <script lang="ts" module>
2
+ import { forceSimulation, type Force, type Simulation, type SimulationNodeDatum } from 'd3-force';
3
+ import type { Snippet } from 'svelte';
8
4
 
9
5
  type Forces = Record<string, Force<any, any>>;
10
- export let forces: Forces;
11
-
12
- export let alpha: number = 1;
13
- export let alphaTarget: number = 0;
14
- export let alphaDecay: number = 1 - Math.pow(0.001, 1 / 300);
15
- export let alphaMin: number = 0.001;
16
-
17
- export let velocityDecay = 0.4;
18
6
 
19
- /** Stop simulation */
20
- export let stopped = false;
21
-
22
- let _static = false;
23
- /** If true, will only update nodes after simulation has completed */
24
- export { _static as static };
7
+ export type LinkPosition = {
8
+ x1: number;
9
+ y1: number;
10
+ x2: number;
11
+ y2: number;
12
+ };
13
+
14
+ export type ForceSimulationProps = {
15
+ /**
16
+ * Force simulation parameters
17
+ */
18
+ forces: Forces;
19
+
20
+ /**
21
+ * An array of links to be used for position calculation.
22
+ */
23
+ links?: any[];
24
+
25
+ /**
26
+ * Current alpha value of the simulation
27
+ * @default 1
28
+ */
29
+ alpha?: number;
30
+
31
+ /**
32
+ * Target alpha value for the simulation
33
+ * @default 0
34
+ */
35
+ alphaTarget?: number;
36
+
37
+ /**
38
+ * Alpha decay rate per tick
39
+ * @default 1 - Math.pow(0.001, 1 / 300)
40
+ */
41
+ alphaDecay?: number;
42
+
43
+ /**
44
+ * Minimum alpha value at which simulation stops
45
+ * @default 0.01
46
+ */
47
+ alphaMin?: number;
48
+
49
+ /**
50
+ * Velocity decay factor applied to nodes each tick
51
+ * @default 0.4
52
+ */
53
+ velocityDecay?: number;
54
+
55
+ /**
56
+ * Stop simulation
57
+ * @default false
58
+ */
59
+ stopped?: boolean;
60
+
61
+ /**
62
+ * If true, will only update nodes after simulation has completed
63
+ * @default false
64
+ */
65
+ static?: boolean;
66
+
67
+ /**
68
+ * Clone nodes since simulation mutates original
69
+ * @default false
70
+ */
71
+ cloneNodes?: boolean;
72
+
73
+ /**
74
+ * Callback function triggered when simulation starts
75
+ */
76
+ onStart?: () => void;
77
+
78
+ /**
79
+ * Callback function triggered on each simulation tick
80
+ */
81
+ onTick?: (e: { alpha: number; alphaTarget: number }) => void;
82
+
83
+ /**
84
+ * Callback function triggered when simulation ends
85
+ */
86
+ onEnd?: () => void;
87
+
88
+ children?: Snippet<
89
+ [
90
+ {
91
+ nodes: any[];
92
+ simulation: Simulation<SimulationNodeDatum, undefined>;
93
+ linkPositions: LinkPosition[];
94
+ },
95
+ ]
96
+ >;
97
+ };
98
+ </script>
25
99
 
26
- /** Clone data since simulation mutates original */
27
- export const cloneData: boolean = false;
100
+ <script lang="ts">
101
+ import { getChartContext } from './Chart.svelte';
102
+ import { watch } from 'runed';
103
+
104
+ let {
105
+ forces,
106
+ links = [],
107
+ alpha = $bindable(1),
108
+ alphaTarget = 0,
109
+ alphaDecay = 1 - Math.pow(0.001, 1 / 300),
110
+ alphaMin = 0.001,
111
+ velocityDecay = 0.4,
112
+ stopped = false,
113
+ static: staticProp,
114
+ onStart: onStartProp = () => {},
115
+ onTick: onTickProp = () => {},
116
+ onEnd: onEndProp = () => {},
117
+ children,
118
+ cloneNodes = false,
119
+ }: ForceSimulationProps = $props();
120
+
121
+ const ctx = getChartContext();
28
122
 
29
- export let onstart: (() => void) | undefined = undefined;
30
- export let ontick: ((e: { alpha: number; alphaTarget: number }) => void) | undefined = undefined;
31
- // export let onchange: | (() => void) | undefined = undefined;
32
- export let onend: (() => void) | undefined = undefined;
123
+ // MARK: Public Props
33
124
 
34
125
  // MARK: Private Props
35
126
 
36
- let nodes: any[] = [];
127
+ let nodes: SimulationNodeDatum[] = $state([]);
128
+ let linkPositions: LinkPosition[] = $state([]);
37
129
 
38
130
  const simulation = forceSimulation().stop();
39
131
 
@@ -45,57 +137,71 @@
45
137
 
46
138
  // MARK: Reactivity Effects
47
139
 
48
- $: {
49
- // Any time the `stopped` prop gets toggled we
50
- // update the running state of the simulation:
140
+ watch.pre(
141
+ () => stopped,
142
+ () => {
143
+ // Any time the `stopped` prop gets toggled we
144
+ // update the running state of the simulation:
145
+ if (stopped) {
146
+ pauseDynamicSimulation();
147
+ } else {
148
+ runOrResumeSimulation();
149
+ }
150
+ }
151
+ );
152
+
153
+ watch.pre(
154
+ () => staticProp,
155
+ () => {
156
+ // Any time the `static` prop gets toggled we
157
+ // either attach or detach our internal event listeners:
158
+ if (staticProp) {
159
+ simulation.on('tick', null).on('end', null);
160
+ } else {
161
+ simulation.on('tick', onTick).on('end', onEnd);
162
+ }
51
163
 
52
- if (stopped) {
53
- pauseDynamicSimulation();
54
- } else {
55
164
  runOrResumeSimulation();
56
165
  }
57
- }
58
-
59
- $: {
60
- // Any time the `static` prop gets toggled we
61
- // either attach or detach our internal event listeners:
62
- if (_static) {
63
- simulation.on('tick', null).on('end', null);
64
- } else {
65
- simulation.on('tick', onTick).on('end', onEnd);
166
+ );
167
+
168
+ watch.pre(
169
+ () => ctx.data,
170
+ () => {
171
+ // Any time the `data` store gets changed we
172
+ // pass them to the internal d3 simulation object:
173
+ pushNodesToSimulation(ctx.data as any[]);
174
+ runOrResumeSimulation();
66
175
  }
67
-
68
- runOrResumeSimulation();
69
- }
70
-
71
- $: {
72
- // Any time the `$data` store gets changed we
73
- // pass them to the internal d3 simulation object:
74
- pushNodesToSimulation($data as any[]);
75
- runOrResumeSimulation();
76
- }
77
-
78
- $: {
79
- // Any time the `forces` prop gets changed we
80
- // pass them to the internal d3 simulation object:
81
- pushForcesToSimulation(forces);
82
- runOrResumeSimulation();
83
- }
84
-
85
- $: {
86
- // Any time the `alpha` prop gets changed we
87
- // pass it to the internal d3 simulation object:
88
- pushAlphaToSimulation(alpha);
89
-
90
- // Only resume the simulation as long as `alpha`
91
- // is above the cut-off threshold of `alphaMin`,
92
- // otherwise our simulation will never terminate:
93
- if (simulation.alpha() >= simulation.alphaMin()) {
176
+ );
177
+
178
+ watch.pre(
179
+ () => forces,
180
+ () => {
181
+ // Any time the `forces` prop gets changed we
182
+ // pass them to the internal d3 simulation object:
183
+ pushForcesToSimulation(forces);
94
184
  runOrResumeSimulation();
95
185
  }
96
- }
186
+ );
187
+
188
+ watch.pre(
189
+ () => alpha,
190
+ () => {
191
+ // Any time the `alpha` prop gets changed we
192
+ // pass it to the internal d3 simulation object:
193
+ pushAlphaToSimulation(alpha);
194
+
195
+ // Only resume the simulation as long as `alpha`
196
+ // is above the cut-off threshold of `alphaMin`,
197
+ // otherwise our simulation will never terminate:
198
+ if (simulation.alpha() >= simulation.alphaMin()) {
199
+ runOrResumeSimulation();
200
+ }
201
+ }
202
+ );
97
203
 
98
- $: {
204
+ watch.pre([() => alphaTarget, () => alphaMin, () => alphaDecay, () => velocityDecay], () => {
99
205
  // Any time any of the the alpha props get changed we
100
206
  // pass them all to the internal d3 simulation object
101
207
  // (they are cheap, so passing them as a batch is fine!):
@@ -117,7 +223,7 @@
117
223
  .velocityDecay(velocityDecay);
118
224
 
119
225
  runOrResumeSimulation();
120
- }
226
+ });
121
227
 
122
228
  // MARK: Push State
123
229
 
@@ -126,31 +232,45 @@
126
232
  }
127
233
 
128
234
  function pushNodesToSimulation(nodes: any[]) {
129
- simulation.nodes(cloneData ? structuredClone(nodes) : nodes);
235
+ simulation.nodes(nodes);
130
236
  }
131
237
 
132
238
  function pushForcesToSimulation(forces: Forces) {
133
239
  // Evict obsolete forces:
134
- Object.keys(previousForces).forEach((name) => {
240
+ const names = Object.keys(previousForces);
241
+ for (const name of names) {
135
242
  if (!(name in forces)) {
136
243
  simulation.force(name, null);
137
244
  }
138
- });
245
+ }
246
+
247
+ const entries = Object.entries(forces);
139
248
 
140
- // Add new or overwrite existing forces:
141
- Object.entries(forces).forEach(([name, force]) => {
249
+ for (const [name, force] of entries) {
142
250
  if (!(name in previousForces) || force !== previousForces[name]) {
143
251
  simulation.force(name, force);
144
252
  }
145
- });
253
+ }
146
254
 
147
255
  previousForces = forces;
148
256
  }
149
257
 
258
+ function updateLinkPositions() {
259
+ // Keeping the link positions in sync with the simulation
260
+ // so we don't need to recalculate _all_ link positions on each tick
261
+ // which bogs down the simulation
262
+ linkPositions = links.map((link: any) => ({
263
+ x1: link.source.x ?? 0,
264
+ y1: link.source.y ?? 0,
265
+ x2: link.target.x ?? 0,
266
+ y2: link.target.y ?? 0,
267
+ }));
268
+ }
269
+
150
270
  // MARK: Pull State
151
271
 
152
272
  function pullNodesFromSimulation() {
153
- nodes = simulation.nodes();
273
+ nodes = cloneNodes ? structuredClone(simulation.nodes()) : simulation.nodes();
154
274
  }
155
275
 
156
276
  function pullAlphaFromSimulation() {
@@ -160,7 +280,7 @@
160
280
  // MARK: Resume / Pause
161
281
 
162
282
  function runOrResumeSimulation() {
163
- if (_static) {
283
+ if (staticProp) {
164
284
  runStaticSimulationToCompletion();
165
285
  } else {
166
286
  resumeDynamicSimulation();
@@ -173,7 +293,7 @@
173
293
  return;
174
294
  }
175
295
 
176
- if (!_static) {
296
+ if (!staticProp) {
177
297
  // Only static simulations are run to completion.
178
298
  return;
179
299
  }
@@ -212,7 +332,7 @@
212
332
  return;
213
333
  }
214
334
 
215
- if (_static) {
335
+ if (staticProp) {
216
336
  // Only dynamic simulations can be resumed.
217
337
  return;
218
338
  }
@@ -244,17 +364,15 @@
244
364
  }
245
365
 
246
366
  paused = false;
247
- onstart?.();
367
+ onStartProp();
248
368
  }
249
369
 
250
370
  function onTick() {
251
371
  pullNodesFromSimulation();
252
372
  pullAlphaFromSimulation();
373
+ updateLinkPositions();
253
374
 
254
- ontick?.({
255
- alpha,
256
- alphaTarget,
257
- });
375
+ onTickProp({ alpha, alphaTarget });
258
376
  }
259
377
 
260
378
  function onEnd() {
@@ -264,8 +382,15 @@
264
382
  }
265
383
 
266
384
  paused = true;
267
- onend?.();
385
+ onEndProp();
268
386
  }
387
+
388
+ $effect(() => {
389
+ return () => {
390
+ simulation.stop();
391
+ simulation.on('tick', null).on('end', null);
392
+ };
393
+ });
269
394
  </script>
270
395
 
271
- <slot {nodes} {simulation} />
396
+ {@render children?.({ nodes: nodes, simulation, linkPositions })}
@@ -1,37 +1,84 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- import { type Force } from 'd3-force';
3
- declare const __propDef: {
4
- props: {
5
- forces: Record<string, Force<any, any>>;
6
- alpha?: number;
7
- alphaTarget?: number;
8
- alphaDecay?: number;
9
- alphaMin?: number;
10
- velocityDecay?: number;
11
- /** Stop simulation */ stopped?: boolean;
12
- /** If true, will only update nodes after simulation has completed */ static?: boolean;
13
- /** Clone data since simulation mutates original */ cloneData?: boolean;
14
- onstart?: (() => void) | undefined;
15
- ontick?: ((e: {
16
- alpha: number;
17
- alphaTarget: number;
18
- }) => void) | undefined;
19
- onend?: (() => void) | undefined;
20
- };
21
- events: {
22
- [evt: string]: CustomEvent<any>;
23
- };
24
- slots: {
25
- default: {
1
+ import { type Force, type Simulation, type SimulationNodeDatum } from 'd3-force';
2
+ import type { Snippet } from 'svelte';
3
+ type Forces = Record<string, Force<any, any>>;
4
+ export type LinkPosition = {
5
+ x1: number;
6
+ y1: number;
7
+ x2: number;
8
+ y2: number;
9
+ };
10
+ export type ForceSimulationProps = {
11
+ /**
12
+ * Force simulation parameters
13
+ */
14
+ forces: Forces;
15
+ /**
16
+ * An array of links to be used for position calculation.
17
+ */
18
+ links?: any[];
19
+ /**
20
+ * Current alpha value of the simulation
21
+ * @default 1
22
+ */
23
+ alpha?: number;
24
+ /**
25
+ * Target alpha value for the simulation
26
+ * @default 0
27
+ */
28
+ alphaTarget?: number;
29
+ /**
30
+ * Alpha decay rate per tick
31
+ * @default 1 - Math.pow(0.001, 1 / 300)
32
+ */
33
+ alphaDecay?: number;
34
+ /**
35
+ * Minimum alpha value at which simulation stops
36
+ * @default 0.01
37
+ */
38
+ alphaMin?: number;
39
+ /**
40
+ * Velocity decay factor applied to nodes each tick
41
+ * @default 0.4
42
+ */
43
+ velocityDecay?: number;
44
+ /**
45
+ * Stop simulation
46
+ * @default false
47
+ */
48
+ stopped?: boolean;
49
+ /**
50
+ * If true, will only update nodes after simulation has completed
51
+ * @default false
52
+ */
53
+ static?: boolean;
54
+ /**
55
+ * Clone nodes since simulation mutates original
56
+ * @default false
57
+ */
58
+ cloneNodes?: boolean;
59
+ /**
60
+ * Callback function triggered when simulation starts
61
+ */
62
+ onStart?: () => void;
63
+ /**
64
+ * Callback function triggered on each simulation tick
65
+ */
66
+ onTick?: (e: {
67
+ alpha: number;
68
+ alphaTarget: number;
69
+ }) => void;
70
+ /**
71
+ * Callback function triggered when simulation ends
72
+ */
73
+ onEnd?: () => void;
74
+ children?: Snippet<[
75
+ {
26
76
  nodes: any[];
27
- simulation: import("d3-force").Simulation<import("d3-force").SimulationNodeDatum, undefined>;
28
- };
29
- };
77
+ simulation: Simulation<SimulationNodeDatum, undefined>;
78
+ linkPositions: LinkPosition[];
79
+ }
80
+ ]>;
30
81
  };
31
- export type ForceSimulationProps = typeof __propDef.props;
32
- export type ForceSimulationEvents = typeof __propDef.events;
33
- export type ForceSimulationSlots = typeof __propDef.slots;
34
- export default class ForceSimulation extends SvelteComponentTyped<ForceSimulationProps, ForceSimulationEvents, ForceSimulationSlots> {
35
- get cloneData(): NonNullable<boolean | undefined>;
36
- }
37
- export {};
82
+ declare const ForceSimulation: import("svelte").Component<ForceSimulationProps, {}, "alpha">;
83
+ type ForceSimulation = ReturnType<typeof ForceSimulation>;
84
+ export default ForceSimulation;
@@ -1,21 +1,41 @@
1
+ <script lang="ts" module>
2
+ import type { Without } from '../utils/types.js';
3
+ import Rect, { type RectProps, type RectPropsWithoutHTML } from './Rect.svelte';
4
+
5
+ export type FramePropsWithoutHTML = RectPropsWithoutHTML & {
6
+ /**
7
+ * Whether to include padding area
8
+ *
9
+ * @default false
10
+ */
11
+ full?: boolean;
12
+ };
13
+
14
+ export type FrameProps = Omit<
15
+ FramePropsWithoutHTML & Without<RectProps, FramePropsWithoutHTML>,
16
+ 'width' | 'height'
17
+ >;
18
+ </script>
19
+
1
20
  <script lang="ts">
2
- import { chartContext } from './ChartContext.svelte';
3
- import Rect from './Rect.svelte';
21
+ import { getChartContext } from './Chart.svelte';
22
+ import { extractLayerProps } from '../utils/attributes.js';
4
23
 
5
- const { width, height, padding } = chartContext();
24
+ let { ref: refProp = $bindable(), full = false, ...restProps }: FrameProps = $props();
6
25
 
7
- /** Include padding area */
8
- export let full = false;
26
+ let ref = $state<SVGRectElement>();
27
+ $effect.pre(() => {
28
+ refProp = ref;
29
+ });
9
30
 
10
- /** Access underlying `<rect>` element */
11
- export let element: SVGRectElement | undefined = undefined;
31
+ const ctx = getChartContext();
12
32
  </script>
13
33
 
14
34
  <Rect
15
- x={full && $padding?.left ? -$padding.left : 0}
16
- y={full && $padding?.top ? -$padding.top : 0}
17
- width={$width + (full ? ($padding?.left ?? 0) + ($padding?.right ?? 0) : 0)}
18
- height={$height + (full ? ($padding?.top ?? 0) + ($padding?.bottom ?? 0) : 0)}
19
- bind:element
20
- {...$$restProps}
35
+ x={full && ctx.padding?.left ? -ctx.padding.left : 0}
36
+ y={full && ctx.padding?.top ? -ctx.padding.top : 0}
37
+ width={ctx.width + (full ? (ctx.padding?.left ?? 0) + (ctx.padding?.right ?? 0) : 0)}
38
+ height={ctx.height + (full ? (ctx.padding?.top ?? 0) + (ctx.padding?.bottom ?? 0) : 0)}
39
+ bind:ref
40
+ {...extractLayerProps(restProps, 'frame')}
21
41
  />
@@ -1,18 +1,14 @@
1
- import { SvelteComponentTyped } from "svelte";
2
- declare const __propDef: {
3
- props: {
4
- [x: string]: any;
5
- full?: boolean | undefined;
6
- element?: SVGRectElement | undefined;
7
- };
8
- events: {
9
- [evt: string]: CustomEvent<any>;
10
- };
11
- slots: {};
1
+ import type { Without } from '../utils/types.js';
2
+ import { type RectProps, type RectPropsWithoutHTML } from './Rect.svelte';
3
+ export type FramePropsWithoutHTML = RectPropsWithoutHTML & {
4
+ /**
5
+ * Whether to include padding area
6
+ *
7
+ * @default false
8
+ */
9
+ full?: boolean;
12
10
  };
13
- export type FrameProps = typeof __propDef.props;
14
- export type FrameEvents = typeof __propDef.events;
15
- export type FrameSlots = typeof __propDef.slots;
16
- export default class Frame extends SvelteComponentTyped<FrameProps, FrameEvents, FrameSlots> {
17
- }
18
- export {};
11
+ export type FrameProps = Omit<FramePropsWithoutHTML & Without<RectProps, FramePropsWithoutHTML>, 'width' | 'height'>;
12
+ declare const Frame: import("svelte").Component<FrameProps, {}, "ref">;
13
+ type Frame = ReturnType<typeof Frame>;
14
+ export default Frame;
@@ -1,24 +1,37 @@
1
- <script lang="ts">
2
- import { geoCircle } from 'd3-geo';
1
+ <script lang="ts" module>
2
+ import GeoPath, { type GeoPathProps } from './GeoPath.svelte';
3
+ import type { Without } from '../utils/types.js';
4
+ export type GeoCirclePropsWithoutHTML = {
5
+ /**
6
+ * The radius of the circle in degrees.
7
+ * @default 90
8
+ */
9
+ radius?: number;
3
10
 
4
- import GeoPath from './GeoPath.svelte';
5
- import type { ComponentProps } from 'svelte';
11
+ /**
12
+ * The center point of the circle in degrees ([longitude, latitude]).
13
+ * @default [0, 0]
14
+ */
15
+ center?: [number, number];
6
16
 
7
- /** Radius in degrees. Default: 90 */
8
- export let radius = 90;
17
+ /**
18
+ * The precision of the circle in degrees.
19
+ * @default 6
20
+ */
21
+ precision?: number;
22
+ };
9
23
 
10
- /** Center point of circle in degree ([longitude, latitude]). Default [0, 0] */
11
- export let center: [number, number] = [0, 0];
24
+ export type GeoCircleProps = GeoCirclePropsWithoutHTML &
25
+ Without<GeoPathProps, GeoCirclePropsWithoutHTML>;
26
+ </script>
12
27
 
13
- /** sets the circle precision to the specified angle in degrees */
14
- export let precision = 6;
28
+ <script lang="ts">
29
+ import { geoCircle } from 'd3-geo';
30
+ import { extractLayerProps } from '../utils/attributes.js';
15
31
 
16
- export let onclick: ComponentProps<typeof GeoPath>['onclick'] = undefined;
17
- export let onpointerenter: ComponentProps<typeof GeoPath>['onpointerenter'] = undefined;
18
- export let onpointermove: ComponentProps<typeof GeoPath>['onpointermove'] = undefined;
19
- export let onpointerleave: ComponentProps<typeof GeoPath>['onpointerleave'] = undefined;
32
+ let { radius = 90, center = [0, 0], precision = 6, ...restProps }: GeoCircleProps = $props();
20
33
 
21
- $: geojson = geoCircle().radius(radius).center(center).precision(precision)();
34
+ const geojson = $derived(geoCircle().radius(radius).center(center).precision(precision)());
22
35
  </script>
23
36
 
24
- <GeoPath {geojson} {onclick} {onpointerenter} {onpointermove} {onpointerleave} {...$$restProps} />
37
+ <GeoPath {geojson} {...extractLayerProps(restProps, 'geo-circle')} />