layerchart 2.0.0-next.0 → 2.0.0-next.2

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 +155 -0
  4. package/dist/components/AnnotationLine.svelte.d.ts +28 -0
  5. package/dist/components/AnnotationPoint.svelte +121 -0
  6. package/dist/components/AnnotationPoint.svelte.d.ts +32 -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 +287 -174
  14. package/dist/components/Axis.svelte.d.ts +116 -0
  15. package/dist/components/Bar.svelte +163 -107
  16. package/dist/components/Bar.svelte.d.ts +48 -0
  17. package/dist/components/Bars.svelte +54 -68
  18. package/dist/components/Bars.svelte.d.ts +27 -0
  19. package/dist/components/Blur.svelte +31 -7
  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 +60 -15
  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 +47 -12
  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 +28 -13
  66. package/dist/components/Graticule.svelte.d.ts +11 -52
  67. package/dist/components/Grid.svelte +226 -114
  68. package/dist/components/Grid.svelte.d.ts +70 -0
  69. package/dist/components/Group.svelte +132 -105
  70. package/dist/components/Group.svelte.d.ts +53 -0
  71. package/dist/components/Highlight.svelte +409 -307
  72. package/dist/components/Highlight.svelte.d.ts +107 -0
  73. package/dist/components/Hull.svelte +96 -45
  74. package/dist/components/Hull.svelte.d.ts +40 -30
  75. package/dist/components/Labels.svelte +125 -46
  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 +105 -62
  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 +106 -75
  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 +25 -24
  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,497 +1,1388 @@
1
- <script lang="ts" context="module">
2
- import { getContext, setContext } from 'svelte';
3
-
4
- export const renderContextKey = Symbol();
5
-
6
- type RenderContext = 'canvas' | 'svg' | 'html';
7
-
8
- /** Get render context. Useful to conditionally render components based on the render context. */
9
- export function getRenderContext() {
10
- return getContext<RenderContext>(renderContextKey);
1
+ <script lang="ts" module>
2
+ import { scaleLinear, scaleOrdinal, scaleSqrt } from 'd3-scale';
3
+ import { type Accessor, accessor, chartDataArray } from '../utils/common.js';
4
+ import { printDebug } from '../utils/debug.js';
5
+ import { filterObject } from '../utils/filterObject.js';
6
+ import {
7
+ createScale,
8
+ getRange,
9
+ isScaleBand,
10
+ makeAccessor,
11
+ type AnyScale,
12
+ type DomainType,
13
+ } from '../utils/scales.svelte.js';
14
+ import { Context, useDebounce } from 'runed';
15
+ import type {
16
+ AxisKey,
17
+ BaseRange,
18
+ DataType,
19
+ Extents,
20
+ Nice,
21
+ Padding,
22
+ PaddingArray,
23
+ XRangeWithScale,
24
+ YRangeWithScale,
25
+ } from '../utils/types.js';
26
+ import {
27
+ calcDomain,
28
+ calcScaleExtents,
29
+ createGetter,
30
+ createChartScale,
31
+ } from '../utils/chart.js';
32
+ import { onMount, type ComponentProps, type Snippet } from 'svelte';
33
+ import GeoContext, { type GeoContextValue } from './GeoContext.svelte';
34
+ import TooltipContext, { type TooltipContextValue } from './tooltip/TooltipContext.svelte';
35
+ import { extent, max, min } from 'd3-array';
36
+ import type { HierarchyNode } from 'd3-hierarchy';
37
+ import type { SankeyGraph } from 'd3-sankey';
38
+ import { unique } from '@layerstack/utils';
39
+ import { geoFitObjectTransform } from '../utils/geo.js';
40
+ import TransformContext, { type TransformContextValue } from './TransformContext.svelte';
41
+ import BrushContext, { type BrushContextValue } from './BrushContext.svelte';
42
+ import { layerClass } from '../utils/attributes.js';
43
+
44
+ const defaultPadding = { top: 0, right: 0, bottom: 0, left: 0 };
45
+
46
+ export type ChartResizeDetail = {
47
+ width: number;
48
+ height: number;
49
+ containerWidth: number;
50
+ containerHeight: number;
51
+ };
52
+
53
+ export type PreservedChartConfig<
54
+ T,
55
+ XScale extends AnyScale = AnyScale,
56
+ YScale extends AnyScale = AnyScale,
57
+ > = Pick<
58
+ ChartPropsWithoutHTML<T, XScale, YScale>,
59
+ | 'x'
60
+ | 'y'
61
+ | 'z'
62
+ | 'r'
63
+ | 'c'
64
+ | 'x1'
65
+ | 'y1'
66
+ | 'xRange'
67
+ | 'yRange'
68
+ | 'cDomain'
69
+ | 'zDomain'
70
+ | 'xDomain'
71
+ | 'yDomain'
72
+ | 'rDomain'
73
+ | 'x1Domain'
74
+ | 'y1Domain'
75
+ | 'zRange'
76
+ | 'rRange'
77
+ | 'cRange'
78
+ | 'x1Range'
79
+ | 'y1Range'
80
+ >;
81
+
82
+ export type ChartContextValue<
83
+ T = any,
84
+ XScale extends AnyScale = AnyScale,
85
+ YScale extends AnyScale = AnyScale,
86
+ > = {
87
+ activeGetters: Record<AxisKey, (d: T) => any>;
88
+ width: number;
89
+ height: number;
90
+ percentRange: boolean;
91
+ aspectRatio: number;
92
+ containerRef: HTMLElement | undefined;
93
+ containerWidth: number;
94
+ containerHeight: number;
95
+ config: PreservedChartConfig<T, XScale, YScale>;
96
+ x: (d: T) => any;
97
+ y: (d: T) => any;
98
+ z: (d: T) => any;
99
+ r: (d: T) => any;
100
+ x1: (d: T) => any;
101
+ y1: (d: T) => any;
102
+ c: (d: T) => any;
103
+ data: DataType<T>;
104
+ xNice: Nice;
105
+ yNice: Nice;
106
+ zNice: Nice;
107
+ rNice: Nice;
108
+ xDomainSort: boolean;
109
+ yDomainSort: boolean;
110
+ zDomainSort: boolean;
111
+ rDomainSort: boolean;
112
+ xReverse: boolean;
113
+ yReverse: boolean;
114
+ zReverse: boolean;
115
+ rReverse: boolean;
116
+ xPadding: PaddingArray;
117
+ yPadding: PaddingArray;
118
+ zPadding: PaddingArray;
119
+ rPadding: PaddingArray;
120
+ padding: Padding;
121
+ flatData: T[];
122
+ extents: Extents;
123
+ xDomain: number[];
124
+ yDomain: number[];
125
+ zDomain: DomainType;
126
+ rDomain: DomainType;
127
+ cDomain: DomainType;
128
+ x1Domain: DomainType;
129
+ y1Domain: DomainType;
130
+ xRange: any[];
131
+ yRange: any[];
132
+ zRange: any[];
133
+ rRange: any[];
134
+ cRange: readonly string[] | string[] | undefined;
135
+ x1Range: XRangeWithScale<XScale> | undefined;
136
+ y1Range: YRangeWithScale<YScale> | undefined;
137
+ meta: Record<string, any>;
138
+ xScale: AnyScale;
139
+ yScale: AnyScale;
140
+ zScale: AnyScale;
141
+ rScale: AnyScale;
142
+ cScale: AnyScale | null;
143
+ x1Scale: AnyScale | null;
144
+ y1Scale: AnyScale | null;
145
+ yGet: (d: T) => any;
146
+ xGet: (d: T) => any;
147
+ zGet: (d: T) => any;
148
+ rGet: (d: T) => any;
149
+ cGet: (d: T) => any;
150
+ x1Get: (d: T) => any;
151
+ y1Get: (d: T) => any;
152
+ radial: boolean;
153
+ tooltip: TooltipContextValue<T>;
154
+ geo: GeoContextValue;
155
+ brush: BrushContextValue;
156
+ transform: TransformContextValue;
157
+ };
158
+
159
+ export type LayerChartInternalMeta = {
160
+ /**
161
+ * The current chart type.
162
+ * The default is `'default'` which is any chart being composed
163
+ * that isn't a "simplified chart".
164
+ */
165
+ type:
166
+ | 'default'
167
+ | 'simplified-area'
168
+ | 'simplified-bar'
169
+ | 'simplified-line'
170
+ | 'simplified-pie'
171
+ | 'simplified-scatter';
172
+ };
173
+
174
+ const _ChartContext = new Context<ChartContextValue<any, AnyScale, AnyScale>>('ChartContext');
175
+
176
+ export function getChartContext<
177
+ T,
178
+ XScale extends AnyScale = AnyScale,
179
+ YScale extends AnyScale = AnyScale,
180
+ >(): ChartContextValue<T, XScale, YScale> {
181
+ return _ChartContext.getOr({} as ChartContextValue<T, XScale, YScale>);
11
182
  }
12
183
 
13
- /** Set by Canavs, Html, or Svg render/layout component */
14
- export function setRenderContext(context: RenderContext) {
15
- setContext(renderContextKey, context);
184
+ export function setChartContext<
185
+ T,
186
+ XScale extends AnyScale = AnyScale,
187
+ YScale extends AnyScale = AnyScale,
188
+ >(context: ChartContextValue<T, XScale, YScale>): ChartContextValue<T, XScale, YScale> {
189
+ // @ts-expect-error - shh
190
+ return _ChartContext.set(context);
16
191
  }
17
- </script>
18
192
 
19
- <script lang="ts" generics="TData">
20
- import { onMount, type ComponentProps } from 'svelte';
21
- import { LayerCake } from 'layercake';
22
- import type { HierarchyNode } from 'd3-hierarchy';
23
- import type { SankeyGraph } from 'd3-sankey';
24
- import { max, min } from 'd3-array';
193
+ export type RenderContext = 'svg' | 'canvas' | 'html';
25
194
 
26
- import ChartContext from './ChartContext.svelte';
27
- import GeoContext from './GeoContext.svelte';
28
- import TooltipContext from './tooltip/TooltipContext.svelte';
29
- import TransformContext from './TransformContext.svelte';
30
- import BrushContext from './BrushContext.svelte';
195
+ const _RenderContext = new Context<RenderContext>('RenderContext');
31
196
 
32
- import { accessor, type Accessor } from '../utils/common.js';
33
- import { isScaleBand, type AnyScale, type DomainType } from '../utils/scales.js';
34
- import { geoFitObjectTransform } from '../utils/geo.js';
197
+ export function getRenderContext(): RenderContext {
198
+ return _RenderContext.get();
199
+ }
35
200
 
36
- type LayerCakeProps = ComponentProps<LayerCake>;
201
+ export function setRenderContext(context: RenderContext): RenderContext {
202
+ return _RenderContext.set(context);
203
+ }
37
204
 
38
- interface $$Props {
39
- /** Whether this chart should be rendered server side. @default false */
205
+ export type ChartPropsWithoutHTML<
206
+ T,
207
+ XScale extends AnyScale = AnyScale,
208
+ YScale extends AnyScale = AnyScale,
209
+ > = {
210
+ /**
211
+ * Whether this chart should be rendered server side
212
+ *
213
+ * @default false
214
+ */
40
215
  ssr?: boolean;
41
216
 
42
- /** Whether to allow pointer events via CSS. Set this to `false` to set `pointer-events: none;` on all components, disabling all mouse interaction. @default true */
217
+ /**
218
+ * Whether to allow pointer events via CSS.
219
+ * Set this to `false` to set `pointer-events: none;` on all components, disabling
220
+ * all mouse interactions.
221
+ *
222
+ * @default true
223
+ */
43
224
  pointerEvents?: boolean;
44
225
 
45
- /** Determine the positioning of the wrapper div. Set this to `'absolute'` when you want to stack cakes. @default 'relative' */
226
+ /**
227
+ * Determine the positioning of the wrapper div.
228
+ * Set this to `'absolute'` when you want to stack layers.
229
+ *
230
+ * @default 'relative'
231
+ */
46
232
  position?: string;
47
233
 
48
- /** If `true`, set all scale ranges to `[0, 100]`. Ranges reversed via `xReverse`, `yReverse`, `zReverse` or `rReverse` props will continue to be reversed as usual. @default false */
234
+ /**
235
+ * If `true`, set all scale ranges to `[0, 100]`.
236
+ * Ranges reversed via `xReverse`, `yReverse`, or `rReverse` props will
237
+ * continue to be reversed as usual.
238
+ * @default false
239
+ */
49
240
  percentRange?: boolean;
50
241
 
51
- /** Override the automated width. */
52
- width?: number;
53
- /** Override the automated height. */
54
- height?: number;
55
-
56
- /** The bound container width. */
57
- containerWidth?: number;
58
- /**The bound container height. */
59
- containerHeight?: number;
60
-
61
- /** The .layercake-container `<div>` tag. Useful for bindings. */
62
- element?: HTMLDivElement;
63
-
64
- /** If `data` is not a flat array of objects and you want to use any of the scales, set a flat version of the data via the `flatData` prop. */
65
- data?: typeof data;
66
-
67
- /** A flat version of data. */
68
- flatData?: any[];
69
-
70
- /** The x accessor. The key in each row of data that corresponds to the x-field. This can be a string, an accessor function, a number or an array of any combination of those types. This property gets converted to a function when you access it through the context. */
71
- x?: Accessor<TData>;
72
- /** The y accessor. The key in each row of data that corresponds to the y-field. This can be a string, an accessor function, a number or an array of any combination of those types. This property gets converted to a function when you access it through the context. */
73
- y?: Accessor<TData>;
74
- /** The z accessor. The key in each row of data that corresponds to the z-field. This can be a string, an accessor function, a number or an array of any combination of those types. This property gets converted to a function when you access it through the context. */
75
- z?: Accessor<TData>;
76
- /** The r accessor. The key in each row of data that corresponds to the r-field. This can be a string, an accessor function, a number or an array of any combination of those types. This property gets converted to a function when you access it through the context. */
77
- r?: Accessor<TData>;
78
- /** The x1 accessor. The key in each row of data that corresponds to the x1-field. This can be a string, an accessor function, a number or an array of any combination of those types. This property gets converted to a function when you access it through the context. */
79
- x1?: Accessor<TData>;
80
- /** The y1 accessor. The key in each row of data that corresponds to the y1-field. This can be a string, an accessor function, a number or an array of any combination of those types. This property gets converted to a function when you access it through the context. */
81
- y1?: Accessor<TData>;
82
- /** The c (color) accessor. The key in each row of data that corresponds to the color. This can be a string or an accessor function. This property gets converted to a function when you access it through the context. */
83
- c?: Accessor<TData>;
84
-
85
- /** Set a min or max. For linear scales, if you want to inherit the value from the data's extent, set that value to `null`. This value can also be an array because sometimes your scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales), useful for color series. Set it to a function that receives the computed domain and lets you return a modified domain, useful for sorting values. */
242
+ /**
243
+ * A bindable reference to the root container element.
244
+ */
245
+ ref?: HTMLElement;
246
+
247
+ /**
248
+ * If `data` is not a flat array of objects and you want to use any of the scales, set a flat
249
+ * version of the data via the `flatData` prop.
250
+ */
251
+ data?: T[] | readonly T[] | HierarchyNode<T> | SankeyGraph<any, any>;
252
+
253
+ /**
254
+ * A flat version of data.
255
+ */
256
+ flatData?: T[] | readonly T[] | HierarchyNode<T> | SankeyGraph<any, any>;
257
+
258
+ /**
259
+ * The x accessor. The key in each row of data that corresponds to the x-field. This can be a
260
+ * string, an accessor function, a number or an array of any combination of those types. This
261
+ * property gets converted to a function when you access it through the context.
262
+ */
263
+ x?: Accessor<T>;
264
+
265
+ /**
266
+ * The y accessor. The key in each row of data that corresponds to the y-field. This can be a
267
+ * string, an accessor function, a number or an array of any combination of those types. This
268
+ * property gets converted to a function when you access it through the context.
269
+ */
270
+ y?: Accessor<T>;
271
+
272
+ /**
273
+ * The z accessor. The key in each row of data that corresponds to the z-field. This can be a
274
+ * string, an accessor function, a number or an array of any combination of those types. This
275
+ * property gets converted to a function when you access it through the context.
276
+ */
277
+ z?: Accessor<T>;
278
+
279
+ /**
280
+ * The r accessor. The key in each row of data that corresponds to the r-field. This can be a
281
+ * string, an accessor function, a number or an array of any combination of those types. This
282
+ * property gets converted to a function when you access it through the context.
283
+ */
284
+ r?: Accessor<T>;
285
+
286
+ /**
287
+ * The x1 accessor. The key in each row of data that corresponds to the x1-field. This can be a
288
+ * string, an accessor function, a number or an array of any combination of those types. This
289
+ * property gets converted to a function when you access it through the context.
290
+ */
291
+ x1?: Accessor<T>;
292
+
293
+ /**
294
+ * The y1 accessor. The key in each row of data that corresponds to the y1-field. This can be
295
+ * a string, an accessor function, a number or an array of any combination of those types. This
296
+ * property gets converted to a function when you access it through the context.
297
+ */
298
+ y1?: Accessor<T>;
299
+
300
+ /**
301
+ * The c (color) accessor. The key in each row of data that corresponds to the color. This can
302
+ * be a string or an accessor function. This property gets converted to a function when you
303
+ * access it through the context.
304
+ */
305
+ c?: Accessor<T>;
306
+
307
+ /**
308
+ * Set a min or max. For linear scales, if you want to inherit the value from the data's
309
+ * extent, set that value to `null`. This value can also be an array because sometimes your
310
+ * scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of
311
+ * discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales),
312
+ * useful for color series. Set it to a function that receives the computed domain and lets you
313
+ * return a modified domain, useful for sorting values.
314
+ */
86
315
  xDomain?: DomainType;
87
- /** Set a min or max. For linear scales, if you want to inherit the value from the data's extent, set that value to `null`. Set it to a function that receives the computed domain and lets you return a modified domain, useful for sorting values. */
316
+
317
+ /**
318
+ * Set a min or max. For linear scales, if you want to inherit the value from the data's
319
+ * extent, set that value to `null`. This value can also be an array because sometimes your
320
+ * scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of
321
+ * discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales),
322
+ * useful for color series. Set it to a function that receives the computed domain and lets you
323
+ * return a modified domain, useful for sorting values.
324
+ */
88
325
  yDomain?: DomainType;
89
- /** Set a min or max. For linear scales, if you want to inherit the value from the data's extent, set that value to `null`. This value can also be an array because sometimes your scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales), useful for color series. Set it to a function that receives the computed domain and lets you return a modified domain, useful for sorting values. */
326
+
327
+ /**
328
+ * Set a min or max. For linear scales, if you want to inherit the value from the data's
329
+ * extent, set that value to `null`. This value can also be an array because sometimes your
330
+ * scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of
331
+ * discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales),
332
+ * useful for color series. Set it to a function that receives the computed domain and lets you
333
+ * return a modified domain, useful for sorting values.
334
+ */
90
335
  zDomain?: DomainType;
91
- /** Set a min or max. For linear scales, if you want to inherit the value from the data's extent, set that value to `null`. This value can also be an array because sometimes your scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales), useful for color series. Set it to a function that receives the computed domain and lets you return a modified domain, useful for sorting values. */
336
+
337
+ /**
338
+ * Set a min or max. For linear scales, if you want to inherit the value from the data's
339
+ * extent, set that value to `null`. This value can also be an array because sometimes your
340
+ * scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of
341
+ * discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales),
342
+ * useful for color series. Set it to a function that receives the computed domain and lets you
343
+ * return a modified domain, useful for sorting values.
344
+ */
92
345
  rDomain?: DomainType;
93
- /** Set a min or max. For linear scales, if you want to inherit the value from the data's extent, set that value to `null`. This value can also be an array because sometimes your scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales), useful for color series. Set it to a function that receives the computed domain and lets you return a modified domain, useful for sorting values. */
346
+
347
+ /**
348
+ * Set a min or max. For linear scales, if you want to inherit the value from the data's
349
+ * extent, set that value to `null`. This value can also be an array because sometimes your
350
+ * scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of
351
+ * discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales),
352
+ * useful for color series. Set it to a function that receives the computed domain and lets you
353
+ * return a modified domain, useful for sorting values.
354
+ */
94
355
  x1Domain?: DomainType;
95
- /** Set a min or max. For linear scales, if you want to inherit the value from the data's extent, set that value to `null`. This value can also be an array because sometimes your scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales), useful for color series. Set it to a function that receives the computed domain and lets you return a modified domain, useful for sorting values. */
356
+
357
+ /**
358
+ * Set a min or max. For linear scales, if you want to inherit the value from the data's
359
+ * extent, set that value to `null`. This value can also be an array because sometimes your
360
+ * scales are [piecewise](https://github.com/d3/d3-scale#continuous_domain) or are a list of
361
+ * discrete values such as in [ordinal scales](https://github.com/d3/d3-scale#ordinal-scales),
362
+ * useful for color series. Set it to a function that receives the computed domain and lets you
363
+ * return a modified domain, useful for sorting values.
364
+ */
96
365
  y1Domain?: DomainType;
97
- /** Set the list of color values. */
366
+
367
+ /**
368
+ * Set the list of color values.
369
+ */
98
370
  cDomain?: DomainType;
99
371
 
100
- /** Applies D3's [scale.nice()](https://github.com/d3/d3-scale#continuous_nice) to the x domain. @default false */
101
- xNice?: boolean | number;
102
- /** Applies D3's [scale.nice()](https://github.com/d3/d3-scale#continuous_nice) to the y domain. @default false */
103
- yNice?: boolean | number;
104
- /** Applies D3's [scale.nice()](https://github.com/d3/d3-scale#continuous_nice) to the z domain. @default false */
105
- zNice?: boolean | number;
106
- /** Applies D3's [scale.nice()](https://github.com/d3/d3-scale#continuous_nice) to the r domain. @default false */
107
- rNice?: boolean | number;
108
-
109
- /** Assign a pixel value to add to the min or max of the scale. This will increase the scales domain by the scale unit equivalent of the provided pixels. */
110
- xPadding?: [number, number];
111
- /** Assign a pixel value to add to the min or max of the scale. This will increase the scales domain by the scale unit equivalent of the provided pixels. */
112
- yPadding?: [number, number];
113
- /** Assign a pixel value to add to the min or max of the scale. This will increase the scales domain by the scale unit equivalent of the provided pixels. */
114
- zPadding?: [number, number];
115
- /** Assign a pixel value to add to the min or max of the scale. This will increase the scales domain by the scale unit equivalent of the provided pixels. */
116
- rPadding?: [number, number];
117
-
118
- /** The D3 scale that should be used for the x-dimension. Pass in an instantiated D3 scale if you want to override the default or you want to extra options. @default scaleLinear */
119
- xScale?: AnyScale;
120
- /** The D3 scale that should be used for the x-dimension. Pass in an instantiated D3 scale if you want to override the default or you want to extra options. @default scaleLinear */
121
- yScale?: AnyScale;
122
- /** The D3 scale that should be used for the x-dimension. Pass in an instantiated D3 scale if you want to override the default or you want to extra options. @default scaleLinear */
372
+ /**
373
+ * Applies D3's [scale.nice()](https://github.com/d3/d3-scale#continuous_nice) to the x domain.
374
+ * @default false
375
+ */
376
+ xNice?: Nice;
377
+
378
+ /**
379
+ * Applies D3's [scale.nice()](https://github.com/d3/d3-scale#continuous_nice) to the y domain.
380
+ * @default false
381
+ */
382
+ yNice?: Nice;
383
+
384
+ /**
385
+ * Applies D3's [scale.nice()](https://github.com/d3/d3-scale#continuous_nice) to the z domain.
386
+ * @default false
387
+ */
388
+ zNice?: Nice;
389
+
390
+ /**
391
+ * Applies D3's [scale.nice()](https://github.com/d3/d3-scale#continuous_nice) to the r domain.
392
+ * @default false
393
+ */
394
+ rNice?: Nice;
395
+
396
+ /**
397
+ * Assign a pixel value to add to the min or max of the scale. This will increase the scales
398
+ * domain by the scale unit equivalent of the provided pixels.
399
+ */
400
+ xPadding?: PaddingArray;
401
+ /**
402
+ * Assign a pixel value to add to the min or max of the scale. This will increase the scales
403
+ * domain by the scale unit equivalent of the provided pixels.
404
+ */
405
+ yPadding?: PaddingArray;
406
+ /**
407
+ * Assign a pixel value to add to the min or max of the scale. This will increase the scales
408
+ * domain by the scale unit equivalent of the provided pixels.
409
+ */
410
+ zPadding?: PaddingArray;
411
+ /**
412
+ * Assign a pixel value to add to the min or max of the scale. This will increase the scales
413
+ * domain by the scale unit equivalent of the provided pixels.
414
+ */
415
+ rPadding?: PaddingArray;
416
+
417
+ /**
418
+ * The D3 scale that should be used for the x-dimension. Pass in an instantiated D3 scale if
419
+ * you want to override the default or you want to extra options.
420
+ * @default scaleLinear
421
+ */
422
+ xScale?: XScale;
423
+
424
+ /**
425
+ * The D3 scale that should be used for the x-dimension. Pass in an instantiated D3 scale if
426
+ * you want to override the default or you want to extra options.
427
+ * @default scaleLinear
428
+ */
429
+ yScale?: YScale;
430
+
431
+ /**
432
+ * The D3 scale that should be used for the x-dimension. Pass in an instantiated D3 scale if
433
+ * you want to override the default or you want to extra options.
434
+ * @default scaleLinear
435
+ */
123
436
  zScale?: AnyScale;
124
- /** The D3 scale that should be used for the x-dimension. Pass in an instantiated D3 scale if you want to override the default or you want to extra options. @default scaleSqrt */
437
+
438
+ /**
439
+ * The D3 scale that should be used for the x-dimension. Pass in an instantiated D3 scale if
440
+ * you want to override the default or you want to extra options.
441
+ * @default scaleSqrt
442
+ */
125
443
  rScale?: AnyScale;
126
- /** The D3 scale that should be used for the x1-dimension. Pass in an instantiated D3 scale if you want to override the default or you want to extra options. @default scaleLinear */
444
+
445
+ /**
446
+ * The D3 scale that should be used for the x1-dimension. Pass in an instantiated D3 scale if
447
+ * you want to override the default or you want to extra options.
448
+ * @default scaleLinear
449
+ */
127
450
  x1Scale?: AnyScale;
128
- /** The D3 scale that should be used for the y1-dimension. Pass in an instantiated D3 scale if you want to override the default or you want to extra options. @default scaleLinear */
451
+
452
+ /**
453
+ * The D3 scale that should be used for the y1-dimension. Pass in an instantiated D3 scale if
454
+ * you want to override the default or you want to extra options.
455
+ * @default scaleLinear
456
+ */
129
457
  y1Scale?: AnyScale;
130
- /** The D3 scale that should be used for the color dimension. Pass in an instantiated D3 scale if you want to override the default or you want to extra options. @default scaleOrdinal */
458
+
459
+ /**
460
+ * The D3 scale that should be used for the color dimension. Pass in an instantiated D3 scale
461
+ * if you want to override the default or you want to extra options.
462
+ * @default scaleOrdinal
463
+ */
131
464
  cScale?: AnyScale;
132
465
 
133
- /** Override the default x range of `[0, width]` by setting an array or function with argument `({ width, height})` that returns an array. Setting this prop overrides `xReverse`. This can also be a list of numbers or strings for scales with discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales). */
134
- xRange?:
135
- | number[]
136
- | string[]
137
- | ((args: { width: number; height: number }) => number[] | string[]);
138
- /** Override the default y range of `[0, height]` by setting an array or function with argument `({ width, height})` that returns an array. Setting this prop overrides `yReverse`. This can also be a list of numbers or strings for scales with discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales). */
139
- yRange?:
140
- | number[]
141
- | string[]
142
- | ((args: { width: number; height: number }) => number[] | string[]);
143
- /** Override the default z range of `[0, width]` by setting an array or function with argument `({ width, height})` that returns an array. Setting this prop overrides `zReverse`. This can also be a list of numbers or strings for scales with discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales). */
144
- zRange?:
145
- | number[]
146
- | string[]
147
- | ((args: { width: number; height: number }) => number[] | string[]);
148
- /** Override the default r range of `[1, 25]` by setting an array or function with argument `({ width, height})` that returns an array. Setting this prop overrides `rReverse`. This can also be a list of numbers or strings for scales with discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales). */
149
- rRange?:
150
- | number[]
151
- | string[]
152
- | ((args: { width: number; height: number }) => number[] | string[]);
153
- /** Set the x1 range by setting an array or function with argument `({ xScale, width, height})` that returns an array. This can also be a list of numbers or strings for scales with discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales). */
154
- x1Range?:
155
- | number[]
156
- | string[]
157
- | ((args: { xScale: AnyScale; width: number; height: number }) => number[] | string[]);
158
- /** Set the y1 range by setting an array or function with argument `({ yScale, width, height})` that returns an array. This can also be a list of numbers or strings for scales with discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales). */
159
- y1Range?:
160
- | number[]
161
- | string[]
162
- | ((args: { yScale: AnyScale; width: number; height: number }) => number[] | string[]);
163
- /** Override the default y1 range of `[0, width]` by setting an array or function with argument `({ yScale, width, height})` that returns an array. Setting this prop overrides `x1Reverse`. This can also be a list of numbers or strings for scales with discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales). */
466
+ /** Override the default x range of `[0, width]` by setting an array or function with argument
467
+ * `({ width, height})` that returns an array. Setting this prop overrides `xReverse`. This can
468
+ * also be a list of numbers or strings for scales with discrete ranges like
469
+ * [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or
470
+ * [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales).
471
+ */
472
+ xRange?: BaseRange;
473
+
474
+ /**
475
+ * Override the default y range of `[0, height]` by setting an array or function with argument
476
+ * `({ width, height})` that returns an array. Setting this prop overrides `yReverse`. This can
477
+ * also be a list of numbers or strings for scales with discrete ranges like
478
+ * [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or
479
+ * [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales).
480
+ */
481
+ yRange?: BaseRange;
482
+
483
+ /** Override the default z range of `[0, width]` by setting an array or function with argument
484
+ * `({ width, height})` that returns an array. Setting this prop overrides `zReverse`. This can
485
+ * also be a list of numbers or strings for scales with discrete ranges like
486
+ * [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or
487
+ * [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales).
488
+ */
489
+ zRange?: BaseRange;
490
+
491
+ /** Override the default r range of `[1, 25]` by setting an array or function with argument
492
+ * `({ width, height})` that returns an array. Setting this prop overrides `rReverse`. This can
493
+ * also be a list of numbers or strings for scales with discrete ranges like
494
+ * [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or
495
+ * [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales).
496
+ */
497
+ rRange?: BaseRange;
498
+
499
+ /**
500
+ * Set the x1 range by setting an array or function with argument `({ xScale, width, height})`
501
+ * that returns an array. This can also be a list of numbers or strings for scales with
502
+ * discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or
503
+ * [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales).
504
+ */
505
+ x1Range?: XRangeWithScale<XScale>;
506
+
507
+ /**
508
+ * Set the y1 range by setting an array or function with argument `({ yScale, width, height})`
509
+ * that returns an array. This can also be a list of numbers or strings for scales with
510
+ * discrete ranges like [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or
511
+ * [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales).
512
+ */
513
+ y1Range?: YRangeWithScale<YScale>;
514
+
515
+ /**
516
+ * Override the default y1 range of `[0, width]` by setting an array or function with argument
517
+ * `({ yScale, width, height})` that returns an array. Setting this prop overrides `x1Reverse`.
518
+ * This can also be a list of numbers or strings for scales with discrete ranges like
519
+ * [scaleThreshold](https://github.com/d3/d3-scale#threshold-scales) or
520
+ * [scaleQuantize](https://github.com/d3/d3-scale#quantize-scales).
521
+ */
164
522
  cRange?: string[] | readonly string[];
165
523
 
166
- /** Reverse the default x range. By default this is `false` and the range is `[0, width]`. Ignored if you set the xRange prop. @default false */
524
+ /**
525
+ * Reverse the default x range. By default this is `false` and the range is `[0, width]`.
526
+ * Ignored if you set the xRange prop.
527
+ * @default false
528
+ */
167
529
  xReverse?: boolean;
168
- /** Reverse the default y range. By default this is `true` and the range is `[height, 0]` unless using an ordinal scale with a `.bandwidth` method for `yScale`. Ignored if you set the `yRange` prop. @default true */
530
+
531
+ /**
532
+ * Reverse the default y range. By default this is `true` and the range is `[height, 0]` unless
533
+ * using an ordinal scale with a `.bandwidth` method for `yScale`.
534
+ * Ignored if you set the `yRange` prop.
535
+ * @default true
536
+ */
169
537
  yReverse?: boolean;
170
- /** Reverse the default z range. By default this is `false` and the range is `[0, width]`. Ignored if you set the zRange prop. @default false */
538
+
539
+ /**
540
+ * Reverse the default z range. By default this is `false` and the range is `[0, width]`.
541
+ * Ignored if you set the zRange prop.
542
+ * @default false
543
+ */
171
544
  zReverse?: boolean;
172
- /** Reverse the default r range. By default this is `false` and the range is `[1, 25]`. Ignored if you set the rRange prop. @default false */
545
+
546
+ /**
547
+ * Reverse the default r range. By default this is `false` and the range is `[1, 25]`.
548
+ * Ignored if you set the rRange prop.
549
+ * @default false
550
+ */
173
551
  rReverse?: boolean;
174
552
 
175
- /** Only used when scale is ordinal. Set whether the calculated unique items come back sorted. */
553
+ /**
554
+ * ***Only used when scale is ordinal.***
555
+ * Set whether the calculated unique items come back sorted.
556
+ *
557
+ * @default false
558
+ */
176
559
  xDomainSort?: boolean;
177
- /** Only used when scale is ordinal. Set whether the calculated unique items come back sorted. */
560
+
561
+ /**
562
+ * ***Only used when scale is ordinal.***
563
+ * Set whether the calculated unique items come back sorted.
564
+ *
565
+ * @default false
566
+ */
178
567
  yDomainSort?: boolean;
179
- /** Only used when scale is ordinal. Set whether the calculated unique items come back sorted. */
568
+
569
+ /**
570
+ * ***Only used when scale is ordinal.***
571
+ * Set whether the calculated unique items come back sorted.
572
+ *
573
+ * @default false
574
+ */
180
575
  zDomainSort?: boolean;
181
- /** Only used when scale is ordinal. Set whether the calculated unique items come back sorted. */
182
- rDomainSort?: boolean;
183
576
 
184
- /** The amount of padding to put around your chart. It operates like CSS box-sizing: border-box; where values are subtracted from the parent container's width and height, the same as a [D3 margin convention](https://bl.ocks.org/mbostock/3019563). */
185
- padding?: { top?: Number; right?: Number; bottom?: Number; left?: Number };
577
+ /**
578
+ * ***Only used when scale is ordinal.***
579
+ * Set whether the calculated unique items come back sorted.
580
+ *
581
+ * @default false
582
+ */
583
+ rDomainSort?: boolean;
186
584
 
187
- /** Manually set the extents of the x, y or r scale as a two-dimensional array of the min and max you want. Setting values here will skip any dynamic extent calculation of the data for that dimension. */
585
+ /**
586
+ * The amount of padding to put around your chart. It operates like CSS
587
+ * box-sizing: border-box; where values are subtracted from the parent container's width
588
+ * and height, the same as a [D3 margin convention](https://bl.ocks.org/mbostock/3019563).
589
+ *
590
+ * If a number is passed, it will be applied to all sides.
591
+ */
592
+ padding?: { top?: number; right?: number; bottom?: number; left?: number } | number;
593
+
594
+ /**
595
+ * Manually set the extents of the x, y or r scale as a two-dimensional array of the min and
596
+ * max you want. Setting values here will skip any dynamic extent calculation of the data for
597
+ * that dimension.
598
+ */
188
599
  extents?: {
189
- x?: [min: Number, max: Number];
190
- y?: [min: Number, max: Number];
191
- r?: [min: Number, max: Number];
192
- z?: [min: Number, max: Number];
600
+ x?: [min: number, max: number];
601
+ y?: [min: number, max: number];
602
+ r?: [min: number, max: number];
603
+ z?: [min: number, max: number];
193
604
  };
194
605
 
195
- /** Any extra configuration values you want available on the LayerCake context. This could be useful for color lookups or additional constants. */
196
- custom?: Record<string, any>;
197
-
198
- /** Enable debug printing to the console. Useful to inspect your scales and dimensions. */
606
+ /**
607
+ * Any extra configuration values you want available on the Chart context.
608
+ * This could be useful for color lookups or additional constants.
609
+ */
610
+ meta?: Record<string, any>;
611
+
612
+ /**
613
+ * Enable debug printing to the console.
614
+ * Useful to inspect your scales and dimensions.
615
+ *
616
+ * @default false
617
+ */
199
618
  debug?: boolean;
200
619
 
201
- /** Show warnings in the console. */
620
+ /**
621
+ * Show warnings in the console.
622
+ *
623
+ * @default true
624
+ */
202
625
  verbose?: boolean;
203
626
 
204
- /** x value guaranteed to be visible in xDomain. Useful with optional negative values since `xDomain={[0, null]}` would ignore negative values */
205
- xBaseline?: typeof xBaseline;
206
-
207
- /** y value guaranteed to be visible in yDomain. Useful with optional negative values since `yDomain={[0, null]}` would ignore negative values */
208
- yBaseline?: typeof xBaseline;
627
+ /**
628
+ * x value guaranteed to be visible in xDomain. Useful with optional negative values since
629
+ * `xDomain={[0, null]}` would ignore negative values
630
+ *
631
+ * @default null
632
+ */
633
+ xBaseline?: number | null;
634
+
635
+ /**
636
+ * y value guaranteed to be visible in yDomain. Useful with optional negative values since
637
+ * `yDomain={[0, null]}` would ignore negative values
638
+ *
639
+ * @default null
640
+ */
641
+ yBaseline?: number | null;
209
642
 
210
643
  /* Props passed to ChartContext */
211
- /** Use radial instead of cartesian coordinates, mapping `x` to `angle` and `y`` to radial. Radial lines are positioned relative to the origin, use transform (ex. `<Group center>`) to change the origin */
212
- radial?: typeof radial;
213
-
214
- /** Props passed to GeoContext */
215
- geo?: typeof geo;
216
-
217
- /** Exposed via bind: to support `bind:geoProjection` for external access */
218
- geoProjection?: typeof geoProjection;
219
-
220
- /** Props passed to TooltipContext */
221
- tooltip?: typeof tooltip;
222
-
223
- /** Exposed via bind: to support `bind:tooltipContext` for external access (ex. `tooltipContext.data) */
224
- tooltipContext?: typeof tooltipContext;
225
-
226
- /** Props passed to TransformContext */
227
- transform?: typeof transform;
228
644
 
229
- /** Expose to support `bind:transformContext` for imperative control (`transformContext.translate(...)`) */
230
- transformContext?: typeof transformContext;
645
+ /**
646
+ * Use radial instead of cartesian coordinates, mapping `x` to `angle` and `y`` to radial.
647
+ * Radial lines are positioned relative to the origin, use transform (ex. `<Group center>`)
648
+ * to change the origin
649
+ *
650
+ * @default false
651
+ */
652
+ radial?: boolean;
653
+
654
+ children?: Snippet<[{ context: ChartContextValue<T, XScale, YScale> }]>;
655
+
656
+ /**
657
+ * A bindable reference to the chart context.
658
+ */
659
+ context?: ChartContextValue<T, XScale, YScale>;
660
+
661
+ /**
662
+ * Props passed to GeoContext
663
+ */
664
+ geo?: Partial<ComponentProps<typeof GeoContext>>;
665
+
666
+ /**
667
+ * Props passed to the `TooltipContext` component.
668
+ */
669
+ tooltip?: Partial<ComponentProps<typeof TooltipContext>> | boolean;
670
+
671
+ /**
672
+ * Props passed to TransformContext
673
+ */
674
+ transform?: Partial<ComponentProps<typeof TransformContext>>;
231
675
 
232
676
  /** Props passed to BrushContext */
233
- brush?: typeof brush;
677
+ brush?: Partial<ComponentProps<typeof BrushContext>> | boolean;
234
678
 
235
- /** Exposed via bind: to support `bind:brushContext` for external access (ex. `brushContext.xDomain) */
236
- brushContext?: typeof brushContext;
237
-
238
- // ChartContext callback events
239
- onresize?: typeof onresize;
679
+ /**
680
+ * A callback function that is called when the chart is resized.
681
+ */
682
+ onResize?: (e: ChartResizeDetail) => void;
240
683
 
241
684
  // TransformContext callback events
242
- ondragstart?: typeof ondragstart;
243
- ondragend?: typeof ondragend;
244
- ontransform?: typeof ontransform;
245
- }
685
+ ondragstart?: ComponentProps<typeof TransformContext>['ondragstart'];
686
+ ondragend?: ComponentProps<typeof TransformContext>['ondragend'];
687
+ onTransform?: ComponentProps<typeof TransformContext>['onTransform'];
688
+ };
689
+ </script>
246
690
 
247
- export let data: TData[] | HierarchyNode<TData> | SankeyGraph<any, any> = [];
691
+ <script
692
+ lang="ts"
693
+ generics="TData = any, XScale extends AnyScale = AnyScale, YScale extends AnyScale = AnyScale"
694
+ >
695
+ let {
696
+ ssr = false,
697
+ pointerEvents = true,
698
+ position = 'relative',
699
+ percentRange = false,
700
+ ref: refProp = $bindable(),
701
+ x: xProp,
702
+ y: yProp,
703
+ z: zProp,
704
+ r: rProp,
705
+ data = [],
706
+ xDomain: xDomainProp,
707
+ yDomain: yDomainProp,
708
+ zDomain: zDomainProp,
709
+ rDomain: rDomainProp,
710
+ xNice = false,
711
+ yNice = false,
712
+ zNice = false,
713
+ rNice = false,
714
+ xPadding,
715
+ yPadding,
716
+ zPadding,
717
+ rPadding,
718
+ // @ts-expect-error shh
719
+ xScale: xScaleProp = scaleLinear(),
720
+ // @ts-expect-error shh
721
+ yScale: yScaleProp = scaleLinear(),
722
+ zScale: zScaleProp = scaleLinear(),
723
+ rScale: rScaleProp = scaleSqrt(),
724
+ flatData: flatDataProp,
725
+ padding: paddingProp = {},
726
+ verbose = true,
727
+ debug = false,
728
+ extents: extentsProp = {},
729
+ xDomainSort = false,
730
+ yDomainSort = false,
731
+ zDomainSort = false,
732
+ rDomainSort = false,
733
+ xReverse = false,
734
+ zReverse = false,
735
+ rReverse = false,
736
+ yRange: _yRangeProp,
737
+ zRange: zRangeProp,
738
+ rRange: rRangeProp,
739
+ xBaseline = null,
740
+ yBaseline = null,
741
+ meta = {},
742
+ children: _children,
743
+ radial = false,
744
+ xRange: _xRangeProp,
745
+ x1: x1Prop,
746
+ x1Domain: x1DomainProp,
747
+ x1Range: x1RangeProp,
748
+ x1Scale: x1ScaleProp,
749
+ y1: y1Prop,
750
+ y1Domain: y1DomainProp,
751
+ y1Range: y1RangeProp,
752
+ y1Scale: y1ScaleProp,
753
+ c: cProp,
754
+ cScale: cScaleProp,
755
+ cDomain: cDomainProp,
756
+ cRange: cRangeProp,
757
+ onResize,
758
+ geo,
759
+ context: contextProp = $bindable(),
760
+ tooltip,
761
+ transform,
762
+ onTransform,
763
+ ondragend,
764
+ ondragstart,
765
+ brush,
766
+ }: ChartPropsWithoutHTML<TData, XScale, YScale> = $props();
767
+
768
+ let ref = $state<HTMLElement>();
769
+
770
+ $effect.pre(() => {
771
+ refProp = ref;
772
+ });
248
773
 
249
- export let x: Accessor<TData> = undefined;
250
- export let xRange: $$Props['xRange'] = undefined;
774
+ const xRangeProp = $derived(_xRangeProp ? _xRangeProp : radial ? [0, 2 * Math.PI] : undefined);
251
775
 
252
- export let y: Accessor<TData> = undefined;
253
- export let yScale: AnyScale | undefined = undefined;
254
- export let yRange: $$Props['yRange'] = undefined;
776
+ let containerWidth = $state(100);
777
+ let containerHeight = $state(100);
255
778
 
256
- export let x1: $$Props['x1'] = undefined;
257
- export let x1Scale: $$Props['x1Scale'] = undefined;
258
- export let x1Domain: $$Props['x1Domain'] = undefined;
259
- export let x1Range: $$Props['x1Range'] = undefined;
779
+ const logDebug = useDebounce(printDebug, 200);
260
780
 
261
- export let y1: $$Props['y1'] = undefined;
262
- export let y1Scale: $$Props['y1Scale'] = undefined;
263
- export let y1Domain: $$Props['y1Domain'] = undefined;
264
- export let y1Range: $$Props['y1Range'] = undefined;
781
+ const _xDomain: DomainType | undefined = $derived.by(() => {
782
+ if (xDomainProp !== undefined) return xDomainProp;
783
+ if (xBaseline != null && Array.isArray(data)) {
784
+ const xValues = data.flatMap(accessor(xProp));
785
+ return [min([xBaseline, ...xValues]), max([xBaseline, ...xValues])];
786
+ }
787
+ });
265
788
 
266
- export let c: $$Props['c'] = undefined;
267
- export let cScale: $$Props['cScale'] = undefined;
268
- export let cDomain: $$Props['cDomain'] = undefined;
269
- export let cRange: $$Props['cRange'] = undefined;
789
+ const _yDomain: DomainType | undefined = $derived.by(() => {
790
+ if (yDomainProp !== undefined) return yDomainProp;
791
+ if (yBaseline != null && Array.isArray(data)) {
792
+ const yValues = data.flatMap(accessor(yProp));
793
+ return [min([yBaseline, ...yValues]), max([yBaseline, ...yValues])];
794
+ }
795
+ });
270
796
 
271
- /**
272
- * x value guaranteed to be visible in xDomain. Useful with optional negative values since `xDomain={[0, null]}` would ignore negative values
273
- */
274
- export let xBaseline: number | null = null;
797
+ const yRangeProp = $derived(
798
+ _yRangeProp ?? (radial ? ({ height }: { height: number }) => [0, height / 2] : undefined)
799
+ );
275
800
 
276
- let xDomain: [number, number] | undefined = undefined;
277
- $: if (xBaseline != null && Array.isArray(data)) {
278
- const xValues = data.flatMap(accessor(x));
279
- xDomain = [min([xBaseline, ...xValues]), max([xBaseline, ...xValues])];
280
- }
801
+ const yReverse = $derived(yScaleProp ? !isScaleBand(yScaleProp) : true);
281
802
 
282
- /**
283
- * y value guaranteed to be visible in yDomain. Useful with optional negative values since `yDomain={[0, null]}` would ignore negative values
284
- */
285
- export let yBaseline: number | null = null;
803
+ const x = $derived(makeAccessor(xProp));
804
+ const y = $derived(makeAccessor(yProp));
805
+ const z = $derived(makeAccessor(zProp));
806
+ const r = $derived(makeAccessor(rProp));
807
+ const c = $derived(accessor(cProp));
808
+ const x1 = $derived(accessor(x1Prop));
809
+ const y1 = $derived(accessor(y1Prop));
286
810
 
287
- let yDomain: [number, number] | undefined = undefined;
288
- $: if (yBaseline != null && Array.isArray(data)) {
289
- const yValues = data.flatMap(accessor(y));
290
- yDomain = [min([yBaseline, ...yValues]), max([yBaseline, ...yValues])];
291
- }
811
+ const flatData = $derived(flatDataProp ?? data) as TData[];
292
812
 
293
- /**
294
- * Reverse the default y range ([0, height] becomes [height, 0]). By default this is `true` unless using scaleBand y scale.
295
- * see: https://layercake.graphics/guide#yreverse
296
- * see: https://github.com/mhkeller/layercake/issues/83
297
- */
298
- $: yReverse = yScale ? !isScaleBand(yScale) : true;
813
+ const filteredExtents = $derived(filterObject($state.snapshot(extentsProp)));
299
814
 
300
- /** Use radial instead of cartesian coordinates, mapping `x` to `angle` and `y`` to radial. Radial lines are positioned relative to the origin, use transform (ex. `<Group center>`) to change the origin */
301
- export let radial = false;
815
+ const activeGetters = $derived({
816
+ x,
817
+ y,
818
+ z,
819
+ r,
820
+ });
302
821
 
303
- /** Props passed to GeoContext */
304
- export let geo: Partial<ComponentProps<GeoContext>> | undefined = undefined;
822
+ const padding = $derived.by(() => {
823
+ if (typeof paddingProp === 'number') {
824
+ return {
825
+ ...defaultPadding,
826
+ top: paddingProp,
827
+ right: paddingProp,
828
+ bottom: paddingProp,
829
+ left: paddingProp,
830
+ };
831
+ }
832
+ return { ...defaultPadding, ...paddingProp };
833
+ });
305
834
 
306
- /** Expose bound geo projection context */
307
- export let geoProjection: ComponentProps<GeoContext>['geo'] = undefined;
835
+ let isMounted = $state(false);
836
+
837
+ const box = $derived.by(() => {
838
+ const top = padding.top;
839
+ const right = containerWidth - padding.right;
840
+ const bottom = containerHeight - padding.bottom;
841
+ const left = padding.left;
842
+ const width = right - left;
843
+ const height = bottom - top;
844
+ if (verbose === true) {
845
+ if (width <= 0 && isMounted === true) {
846
+ console.warn(
847
+ '[LayerChart] Target div has zero or negative width. Did you forget to set an explicit width in CSS on the container?'
848
+ );
849
+ }
850
+ if (height <= 0 && isMounted === true) {
851
+ console.warn(
852
+ '[LayerChart] Target div has zero or negative height. Did you forget to set an explicit height in CSS on the container?'
853
+ );
854
+ }
855
+ }
856
+
857
+ return {
858
+ top,
859
+ left,
860
+ bottom,
861
+ right,
862
+ width,
863
+ height,
864
+ };
865
+ });
308
866
 
309
- /** Props passed to TooltipContext */
310
- export let tooltip: Partial<ComponentProps<TooltipContext>> | boolean | undefined = undefined;
867
+ const width = $derived(box.width);
868
+ const height = $derived(box.height);
311
869
 
312
- /** Expose bound tooltip context */
313
- export let tooltipContext: ComponentProps<TooltipContext>['tooltip'] = undefined;
870
+ interface ScaleEntry {
871
+ scale: AnyScale;
872
+ sort?: boolean;
873
+ }
314
874
 
315
- /** Props passed to TransformContext */
316
- export let transform: Partial<ComponentProps<TransformContext>> | undefined = undefined;
317
- // @ts-expect-error will only be undefined until bind:transformContext runs
318
- export let transformContext: TransformContext = undefined;
875
+ /* --------------------------------------------
876
+ * Calculate extents by taking the extent of the data
877
+ * and filling that in with anything set by the user
878
+ * Note that this is different from an "extent" passed
879
+ * in as a domain, which can be a partial domain
880
+ */
881
+ const extents: Extents = $derived.by(() => {
882
+ const scaleLookup: Record<string, ScaleEntry> = {
883
+ x: { scale: xScaleProp, sort: xDomainSort },
884
+ y: { scale: yScaleProp, sort: yDomainSort },
885
+ z: { scale: zScaleProp, sort: zDomainSort },
886
+ r: { scale: rScaleProp, sort: rDomainSort },
887
+ };
319
888
 
320
- /** Props passed to BrushContext */
321
- export let brush: Partial<ComponentProps<BrushContext>> | boolean | undefined = undefined;
889
+ const getters = filterObject(activeGetters, filteredExtents);
890
+ const activeScales: Record<string, ScaleEntry> = Object.fromEntries(
891
+ Object.keys(getters).map((k) => [k, scaleLookup[k]])
892
+ );
893
+
894
+ if (Object.keys(getters).length > 0) {
895
+ const calculatedExtents = calcScaleExtents(flatData, getters, activeScales);
896
+ return { ...calculatedExtents, ...filteredExtents };
897
+ } else {
898
+ return {};
899
+ }
900
+ });
322
901
 
323
- /** Expose bound brush context */
324
- export let brushContext: ComponentProps<BrushContext>['brush'] = undefined;
902
+ const xDomain = $derived(calcDomain('x', extents, _xDomain));
903
+ const yDomain = $derived(calcDomain('y', extents, _yDomain));
904
+ const zDomain = $derived(calcDomain('z', extents, zDomainProp));
905
+ const rDomain = $derived(calcDomain('r', extents, rDomainProp));
906
+
907
+ const x1Domain = $derived(x1DomainProp ?? extent(chartDataArray(data), x1));
908
+ const y1Domain = $derived(y1DomainProp ?? extent(chartDataArray(data), y1));
909
+ const cDomain = $derived(cDomainProp ?? unique(chartDataArray(data).map(c)));
910
+
911
+ const snappedPadding = $derived($state.snapshot(xPadding));
912
+ const snappedExtents = $derived($state.snapshot(extents));
913
+
914
+ const xScale = $derived(
915
+ createChartScale('x', {
916
+ scale: xScaleProp,
917
+ domain: xDomain,
918
+ padding: snappedPadding,
919
+ nice: xNice,
920
+ reverse: xReverse,
921
+ percentRange,
922
+ range: xRangeProp,
923
+ height,
924
+ width,
925
+ extents: snappedExtents,
926
+ })
927
+ );
928
+
929
+ const xGet = $derived(createGetter(x, xScale));
930
+
931
+ const yScale = $derived(
932
+ createChartScale('y', {
933
+ scale: yScaleProp,
934
+ domain: yDomain,
935
+ padding: yPadding,
936
+ nice: yNice,
937
+ reverse: yReverse,
938
+ percentRange,
939
+ range: yRangeProp,
940
+ height,
941
+ width,
942
+ extents: filteredExtents,
943
+ })
944
+ );
945
+
946
+ const yGet = $derived(createGetter(y, yScale));
947
+
948
+ const zScale = $derived(
949
+ createChartScale('z', {
950
+ scale: zScaleProp,
951
+ domain: zDomain,
952
+ padding: zPadding,
953
+ nice: zNice,
954
+ reverse: zReverse,
955
+ percentRange,
956
+ range: zRangeProp,
957
+ height,
958
+ width,
959
+ extents: filteredExtents,
960
+ })
961
+ );
962
+ const zGet = $derived(createGetter(z, zScale));
963
+
964
+ const rScale = $derived(
965
+ createChartScale('r', {
966
+ scale: rScaleProp,
967
+ domain: rDomain,
968
+ padding: rPadding,
969
+ nice: rNice,
970
+ reverse: rReverse,
971
+ percentRange,
972
+ range: rRangeProp,
973
+ height,
974
+ width,
975
+ extents: filteredExtents,
976
+ })
977
+ );
978
+
979
+ const rGet = $derived(createGetter(r, rScale));
980
+
981
+ const x1Scale = $derived(
982
+ x1ScaleProp && x1RangeProp
983
+ ? createScale(x1ScaleProp, x1Domain, x1RangeProp, {
984
+ xScale: xScale,
985
+ width,
986
+ height,
987
+ })
988
+ : null
989
+ );
990
+
991
+ const x1Get = $derived(createGetter(x1, x1Scale));
992
+
993
+ const y1Scale = $derived(
994
+ y1ScaleProp && y1RangeProp
995
+ ? createScale(y1ScaleProp, y1Domain, y1RangeProp, {
996
+ yScale: yScale,
997
+ width,
998
+ height,
999
+ })
1000
+ : null
1001
+ );
1002
+
1003
+ const y1Get = $derived(createGetter(y1, y1Scale));
1004
+
1005
+ const cScale = $derived(
1006
+ cRangeProp
1007
+ ? createScale(cScaleProp ?? scaleOrdinal(), cDomain, cRangeProp, { width, height })
1008
+ : null
1009
+ );
1010
+
1011
+ const cGet = $derived((d: any) => cScale?.(c(d)));
1012
+
1013
+ const xDomainPossiblyNice = $derived(xScale.domain());
1014
+ const yDomainPossiblyNice = $derived(yScale.domain());
1015
+ const zDomainPossiblyNice = $derived(zScale.domain());
1016
+ const rDomainPossiblyNice = $derived(rScale.domain());
1017
+
1018
+ const xRange = $derived(getRange(xScale));
1019
+ const yRange = $derived(getRange(yScale));
1020
+ const zRange = $derived(getRange(zScale));
1021
+ const rRange = $derived(getRange(rScale));
1022
+
1023
+ const aspectRatio = $derived(width / height);
1024
+
1025
+ const config: PreservedChartConfig<TData, XScale, YScale> = $derived({
1026
+ x: xProp,
1027
+ y: yProp,
1028
+ z: zProp,
1029
+ r: rProp,
1030
+ c: cProp,
1031
+ x1: x1Prop,
1032
+ y1: y1Prop,
1033
+ xDomain: _xDomain,
1034
+ yDomain: _yDomain,
1035
+ zDomain: zDomainProp,
1036
+ rDomain: rDomainProp,
1037
+ x1Domain: x1DomainProp,
1038
+ y1Domain: y1DomainProp,
1039
+ cDomain: cDomainProp,
1040
+ xRange: _xRangeProp,
1041
+ yRange: _yRangeProp,
1042
+ zRange: zRangeProp,
1043
+ rRange: rRangeProp,
1044
+ cRange: cRangeProp,
1045
+ x1Range: x1RangeProp,
1046
+ y1Range: y1RangeProp,
1047
+ });
325
1048
 
326
- export let onresize: ComponentProps<ChartContext<TData>>['onresize'] = undefined;
327
- export let ondragstart: ComponentProps<TransformContext>['ondragstart'] = undefined;
328
- export let ondragend: ComponentProps<TransformContext>['ondragend'] = undefined;
329
- export let ontransform: ComponentProps<TransformContext>['ontransform'] = undefined;
1049
+ let geoContext = $state<GeoContextValue>(null!);
1050
+ let transformContext = $state<TransformContextValue>(null!);
1051
+ let tooltipContext = $state<TooltipContextValue>(null!);
1052
+ let brushContext = $state<BrushContextValue>(null!);
1053
+
1054
+ const context: ChartContextValue<TData, XScale, YScale> = {
1055
+ get activeGetters() {
1056
+ return activeGetters;
1057
+ },
1058
+ get config() {
1059
+ return config;
1060
+ },
1061
+ get width() {
1062
+ return width;
1063
+ },
1064
+ get height() {
1065
+ return height;
1066
+ },
1067
+ get percentRange() {
1068
+ return percentRange;
1069
+ },
1070
+ get aspectRatio() {
1071
+ return aspectRatio;
1072
+ },
1073
+ get containerWidth() {
1074
+ return containerWidth;
1075
+ },
1076
+ get containerHeight() {
1077
+ return containerHeight;
1078
+ },
1079
+ get x() {
1080
+ return x;
1081
+ },
1082
+ get y() {
1083
+ return y;
1084
+ },
1085
+ get z() {
1086
+ return z;
1087
+ },
1088
+ get r() {
1089
+ return r;
1090
+ },
1091
+ get c() {
1092
+ return c;
1093
+ },
1094
+ get x1() {
1095
+ return x1;
1096
+ },
1097
+ get y1() {
1098
+ return y1;
1099
+ },
1100
+ get data() {
1101
+ return data;
1102
+ },
1103
+ get xNice() {
1104
+ return xNice;
1105
+ },
1106
+ get yNice() {
1107
+ return yNice;
1108
+ },
1109
+ get zNice() {
1110
+ return zNice;
1111
+ },
1112
+ get rNice() {
1113
+ return rNice;
1114
+ },
1115
+ get xDomainSort() {
1116
+ return xDomainSort;
1117
+ },
1118
+ get yDomainSort() {
1119
+ return yDomainSort;
1120
+ },
1121
+ get zDomainSort() {
1122
+ return zDomainSort;
1123
+ },
1124
+ get rDomainSort() {
1125
+ return rDomainSort;
1126
+ },
1127
+ get xReverse() {
1128
+ return xReverse;
1129
+ },
1130
+ get yReverse() {
1131
+ return yReverse;
1132
+ },
1133
+ get zReverse() {
1134
+ return zReverse;
1135
+ },
1136
+ get rReverse() {
1137
+ return rReverse;
1138
+ },
1139
+ get xPadding() {
1140
+ return xPadding;
1141
+ },
1142
+ get yPadding() {
1143
+ return yPadding;
1144
+ },
1145
+ get zPadding() {
1146
+ return zPadding;
1147
+ },
1148
+ get rPadding() {
1149
+ return rPadding;
1150
+ },
1151
+ get padding() {
1152
+ return padding;
1153
+ },
1154
+ get flatData() {
1155
+ return flatData;
1156
+ },
1157
+ get extents() {
1158
+ return extents;
1159
+ },
1160
+ get xDomain() {
1161
+ return xDomainPossiblyNice;
1162
+ },
1163
+ get yDomain() {
1164
+ return yDomainPossiblyNice;
1165
+ },
1166
+ get zDomain() {
1167
+ return zDomainPossiblyNice;
1168
+ },
1169
+ get rDomain() {
1170
+ return rDomainPossiblyNice;
1171
+ },
1172
+ get cDomain() {
1173
+ return cDomain;
1174
+ },
1175
+ get x1Domain() {
1176
+ return x1Domain;
1177
+ },
1178
+ get y1Domain() {
1179
+ return y1Domain;
1180
+ },
1181
+ get xRange() {
1182
+ return xRange;
1183
+ },
1184
+ get yRange() {
1185
+ return yRange;
1186
+ },
1187
+ get zRange() {
1188
+ return zRange;
1189
+ },
1190
+ get rRange() {
1191
+ return rRange;
1192
+ },
1193
+ get cRange() {
1194
+ return cRangeProp;
1195
+ },
1196
+ get x1Range() {
1197
+ return x1RangeProp;
1198
+ },
1199
+ get y1Range() {
1200
+ return y1RangeProp;
1201
+ },
1202
+ get meta() {
1203
+ return meta;
1204
+ },
1205
+ set meta(v: Record<string, any>) {
1206
+ meta = v;
1207
+ },
1208
+ get xScale() {
1209
+ return xScale;
1210
+ },
1211
+ get yScale() {
1212
+ return yScale;
1213
+ },
1214
+ get zScale() {
1215
+ return zScale;
1216
+ },
1217
+ get rScale() {
1218
+ return rScale;
1219
+ },
1220
+ get yGet() {
1221
+ return yGet;
1222
+ },
1223
+ get xGet() {
1224
+ return xGet;
1225
+ },
1226
+ get zGet() {
1227
+ return zGet;
1228
+ },
1229
+ get rGet() {
1230
+ return rGet;
1231
+ },
1232
+ get cGet() {
1233
+ return cGet;
1234
+ },
1235
+ get x1Get() {
1236
+ return x1Get;
1237
+ },
1238
+ get y1Get() {
1239
+ return y1Get;
1240
+ },
1241
+ get cScale() {
1242
+ return cScale;
1243
+ },
1244
+ get x1Scale() {
1245
+ return x1Scale;
1246
+ },
1247
+ get y1Scale() {
1248
+ return y1Scale;
1249
+ },
1250
+ get radial() {
1251
+ return radial;
1252
+ },
1253
+ get containerRef() {
1254
+ return ref;
1255
+ },
1256
+ get geo() {
1257
+ return geoContext;
1258
+ },
1259
+ get transform() {
1260
+ return transformContext;
1261
+ },
1262
+ get tooltip() {
1263
+ return tooltipContext;
1264
+ },
1265
+ get brush() {
1266
+ return brushContext;
1267
+ },
1268
+ };
1269
+
1270
+ contextProp = context;
1271
+
1272
+ setChartContext(context);
1273
+
1274
+ $effect(() => {
1275
+ isMounted = true;
1276
+ });
330
1277
 
331
- // Track when mounted since LayerCake initializes width/height with `100` until bound `clientWidth`/`clientWidth` can run
332
- // Useful to key/remount TransformContext with correct `initialTranslate` / `initialScale` values
333
- let isMounted = false;
334
1278
  onMount(() => {
335
- isMounted = true;
1279
+ if (box && debug === true && (ssr === true || typeof window !== 'undefined')) {
1280
+ logDebug({
1281
+ data,
1282
+ flatData: typeof flatData !== 'undefined' ? flatData : null,
1283
+ boundingBox: box,
1284
+ activeGetters,
1285
+ x: xProp,
1286
+ y: yProp,
1287
+ z: zProp,
1288
+ r: rProp,
1289
+ xScale,
1290
+ yScale,
1291
+ zScale,
1292
+ rScale,
1293
+ });
1294
+ }
336
1295
  });
337
1296
 
338
- // TODO: Hacks until LayerCake has better typings (`Accessor<TData>`)
339
- $: _x = x as LayerCakeProps['x'];
340
- $: _y = y as LayerCakeProps['y'];
341
- $: _yRange =
342
- yRange ??
343
- ((radial
344
- ? ({ height }: { height: number }) => [0, height / 2]
345
- : undefined) as LayerCakeProps['yRange']);
346
- </script>
1297
+ $effect(() => {
1298
+ if (!isMounted) return;
1299
+ onResize?.({
1300
+ width: context.width,
1301
+ height: context.height,
1302
+ containerWidth: context.containerWidth,
1303
+ containerHeight: context.containerHeight,
1304
+ });
1305
+ });
347
1306
 
348
- <!-- Remove domain sorting by default: https://github.com/mhkeller/layercake/issues/147 -->
349
- <LayerCake
350
- {data}
351
- x={_x}
352
- {xDomain}
353
- xRange={xRange ?? (radial ? [0, 2 * Math.PI] : undefined)}
354
- y={_y}
355
- {yScale}
356
- {yDomain}
357
- yRange={_yRange}
358
- {yReverse}
359
- xDomainSort={false}
360
- yDomainSort={false}
361
- zDomainSort={false}
362
- rDomainSort={false}
363
- {...$$restProps}
364
- let:aspectRatio
365
- let:containerHeight
366
- let:containerWidth
367
- let:height
368
- let:width
369
- let:element
370
- let:x
371
- let:xScale
372
- let:xGet
373
- let:y
374
- let:yScale
375
- let:yGet
376
- let:z
377
- let:zScale
378
- let:zGet
379
- let:r
380
- let:rScale
381
- let:rGet
382
- let:padding
383
- >
384
- <!-- Apply `fitGeojson` using TransformContext instead of GeoContext if `applyTransform` is used -->
385
- {@const initialTransform =
1307
+ const initialTransform = $derived(
386
1308
  geo?.applyTransform?.includes('translate') && geo?.fitGeojson && geo?.projection
387
1309
  ? geoFitObjectTransform(geo.projection(), [width, height], geo.fitGeojson)
388
- : undefined}
389
-
390
- <ChartContext
391
- {data}
392
- {radial}
393
- {x1}
394
- {x1Scale}
395
- {x1Domain}
396
- {x1Range}
397
- {y1}
398
- {y1Scale}
399
- {y1Domain}
400
- {y1Range}
401
- {c}
402
- {cScale}
403
- {cDomain}
404
- {cRange}
405
- let:data
406
- let:flatData
407
- let:config
408
- let:x1
409
- let:x1Scale
410
- let:x1Get
411
- let:y1
412
- let:y1Scale
413
- let:y1Get
414
- let:c
415
- let:cScale
416
- let:cGet
417
- {onresize}
1310
+ : undefined
1311
+ );
1312
+
1313
+ const processTranslate = $derived.by(() => {
1314
+ if (!geo) return undefined;
1315
+ return (x: number, y: number, deltaX: number, deltaY: number) => {
1316
+ if (geo.applyTransform?.includes('rotate') && geoContext?.projection) {
1317
+ // When applying transform to rotate, invert `y` values and reduce sensitivity based on projection scale
1318
+ // see: https://observablehq.com/@benoldenburg/simple-globe and https://observablehq.com/@michael-keith/draggable-globe-in-d3
1319
+ const projectionScale = geoContext.projection.scale() ?? 0;
1320
+ const sensitivity = 75;
1321
+ return {
1322
+ x: x + deltaX * (sensitivity / projectionScale),
1323
+ y: y + deltaY * (sensitivity / projectionScale) * -1,
1324
+ };
1325
+ } else {
1326
+ // Apply default TransformContext.processTransform (passing `undefined` below appears to not work when checking for `geo?.applyTransform` exists)
1327
+ return { x: x + deltaX, y: y + deltaY };
1328
+ }
1329
+ };
1330
+ });
1331
+
1332
+ const brushProps = $derived(typeof brush === 'object' ? brush : { disabled: !brush });
1333
+ const tooltipProps = $derived(typeof tooltip === 'object' ? tooltip : {});
1334
+ </script>
1335
+
1336
+ {#if ssr === true || typeof window !== 'undefined'}
1337
+ <div
1338
+ bind:this={ref}
1339
+ style:position
1340
+ style:top={position === 'absolute' ? '0' : null}
1341
+ style:right={position === 'absolute' ? '0' : null}
1342
+ style:bottom={position === 'absolute' ? '0' : null}
1343
+ style:left={position === 'absolute' ? '0' : null}
1344
+ style:pointer-events={pointerEvents === false ? 'none' : null}
1345
+ bind:clientWidth={containerWidth}
1346
+ bind:clientHeight={containerHeight}
1347
+ class={layerClass('root-container')}
418
1348
  >
419
1349
  {#key isMounted}
1350
+ <!-- svelte-ignore ownership_invalid_binding -->
420
1351
  <TransformContext
421
- bind:this={transformContext}
1352
+ bind:transformContext
422
1353
  mode={(transform?.mode ?? geo?.applyTransform?.length) ? 'manual' : 'none'}
423
1354
  initialTranslate={initialTransform?.translate}
424
1355
  initialScale={initialTransform?.scale}
425
- processTranslate={geo
426
- ? (x, y, deltaX, deltaY) => {
427
- if (geo.applyTransform?.includes('rotate')) {
428
- // When applying transform to rotate, invert `y` values and reduce sensitivity based on projection scale
429
- // see: https://observablehq.com/@benoldenburg/simple-globe and https://observablehq.com/@michael-keith/draggable-globe-in-d3
430
- // @ts-expect-error
431
- const projectionScale = $geoProjection.scale();
432
- const sensitivity = 75;
433
- return {
434
- x: x + deltaX * (sensitivity / projectionScale),
435
- y: y + deltaY * (sensitivity / projectionScale) * -1,
436
- };
437
- } else {
438
- // Apply default TransformContext.processTransform (passing `undefined` below appears to not work when checking for `geo?.applyTransform` exists)
439
- return { x: x + deltaX, y: y + deltaY };
440
- }
441
- }
442
- : undefined}
1356
+ {processTranslate}
443
1357
  {...transform}
444
- let:transform={_transform}
445
1358
  {ondragstart}
446
- {ontransform}
1359
+ {onTransform}
447
1360
  {ondragend}
448
1361
  >
449
- <GeoContext {...geo} bind:geo={geoProjection} let:projection>
450
- {@const brushProps = typeof brush === 'object' ? brush : { disabled: !brush }}
451
- <BrushContext {...brushProps} bind:brush={brushContext} let:brush>
452
- {@const tooltipProps = typeof tooltip === 'object' ? tooltip : {}}
453
- <TooltipContext {...tooltipProps} bind:tooltip={tooltipContext} let:tooltip>
454
- <slot
455
- {aspectRatio}
456
- {containerHeight}
457
- {containerWidth}
458
- {height}
459
- {width}
460
- {element}
461
- {projection}
462
- transform={_transform}
463
- {tooltip}
464
- {brush}
465
- {x}
466
- {xScale}
467
- {xGet}
468
- {y}
469
- {yScale}
470
- {yGet}
471
- {z}
472
- {zScale}
473
- {zGet}
474
- {r}
475
- {rScale}
476
- {rGet}
477
- {x1}
478
- {x1Scale}
479
- {x1Get}
480
- {y1}
481
- {y1Scale}
482
- {y1Get}
483
- {c}
484
- {cScale}
485
- {cGet}
486
- {padding}
487
- {data}
488
- {flatData}
489
- {config}
490
- />
1362
+ <!-- svelte-ignore ownership_invalid_binding -->
1363
+ <GeoContext {...geo} bind:geoContext>
1364
+ <!-- svelte-ignore ownership_invalid_binding -->
1365
+ <BrushContext {...brushProps} bind:brushContext>
1366
+ <!-- svelte-ignore ownership_invalid_binding -->
1367
+ <TooltipContext {...tooltipProps} bind:tooltipContext>
1368
+ {@render _children?.({
1369
+ context,
1370
+ })}
491
1371
  </TooltipContext>
492
1372
  </BrushContext>
493
1373
  </GeoContext>
494
1374
  </TransformContext>
495
1375
  {/key}
496
- </ChartContext>
497
- </LayerCake>
1376
+ </div>
1377
+ {/if}
1378
+
1379
+ <style>
1380
+ .lc-root-container,
1381
+ .lc-root-container :global(*) {
1382
+ box-sizing: border-box;
1383
+ }
1384
+ .lc-root-container {
1385
+ width: 100%;
1386
+ height: 100%;
1387
+ }
1388
+ </style>