svelteplot 0.10.3 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/dist/Mark.svelte +42 -25
  2. package/dist/Mark.svelte.d.ts +111 -32
  3. package/dist/Plot.svelte +21 -15
  4. package/dist/core/Facet.svelte +1 -1
  5. package/dist/core/FacetAxes.svelte +13 -8
  6. package/dist/core/FacetGrid.svelte +4 -4
  7. package/dist/core/Plot.svelte +41 -35
  8. package/dist/helpers/autoScales.d.ts +3 -3
  9. package/dist/helpers/autoScales.js +28 -18
  10. package/dist/helpers/autoTicks.js +2 -0
  11. package/dist/helpers/callWithProps.d.ts +1 -2
  12. package/dist/helpers/facets.js +0 -1
  13. package/dist/helpers/index.js +1 -1
  14. package/dist/helpers/mergeDeep.d.ts +1 -3
  15. package/dist/helpers/mergeDeep.js +15 -16
  16. package/dist/helpers/projection.d.ts +4 -3
  17. package/dist/helpers/projection.js +17 -5
  18. package/dist/helpers/reduce.d.ts +4 -4
  19. package/dist/helpers/reduce.js +6 -4
  20. package/dist/helpers/regressionLoess.js +2 -1
  21. package/dist/helpers/resolve.d.ts +6 -3
  22. package/dist/helpers/resolve.js +25 -16
  23. package/dist/helpers/scales.d.ts +10 -10
  24. package/dist/helpers/scales.js +43 -13
  25. package/dist/helpers/time.d.ts +10 -3
  26. package/dist/helpers/time.js +2 -1
  27. package/dist/hooks/index.d.ts +2 -0
  28. package/dist/hooks/index.js +2 -0
  29. package/dist/hooks/plotDefaults.d.ts +3 -1
  30. package/dist/hooks/plotDefaults.js +33 -1
  31. package/dist/hooks/usePlot.svelte.d.ts +10 -25
  32. package/dist/hooks/usePlot.svelte.js +8 -7
  33. package/dist/index.d.ts +1 -2
  34. package/dist/index.js +1 -3
  35. package/dist/marks/Area.svelte +24 -13
  36. package/dist/marks/Area.svelte.d.ts +118 -34
  37. package/dist/marks/AreaX.svelte +42 -8
  38. package/dist/marks/AreaX.svelte.d.ts +154 -71
  39. package/dist/marks/AreaY.svelte +42 -8
  40. package/dist/marks/AreaY.svelte.d.ts +154 -71
  41. package/dist/marks/Arrow.svelte +42 -23
  42. package/dist/marks/Arrow.svelte.d.ts +114 -35
  43. package/dist/marks/AxisX.svelte +43 -28
  44. package/dist/marks/AxisX.svelte.d.ts +125 -40
  45. package/dist/marks/AxisY.svelte +43 -26
  46. package/dist/marks/AxisY.svelte.d.ts +127 -40
  47. package/dist/marks/BarX.svelte +12 -10
  48. package/dist/marks/BarX.svelte.d.ts +104 -32
  49. package/dist/marks/BarY.svelte +11 -10
  50. package/dist/marks/BarY.svelte.d.ts +106 -34
  51. package/dist/marks/BollingerX.svelte +4 -7
  52. package/dist/marks/BollingerX.svelte.d.ts +105 -30
  53. package/dist/marks/BollingerY.svelte +3 -0
  54. package/dist/marks/BollingerY.svelte.d.ts +105 -30
  55. package/dist/marks/BoxX.svelte +3 -3
  56. package/dist/marks/BoxY.svelte +12 -9
  57. package/dist/marks/BoxY.svelte.d.ts +128 -53
  58. package/dist/marks/Brush.svelte +26 -21
  59. package/dist/marks/Brush.svelte.d.ts +119 -60
  60. package/dist/marks/Cell.svelte +13 -9
  61. package/dist/marks/Cell.svelte.d.ts +105 -30
  62. package/dist/marks/CellX.svelte +2 -1
  63. package/dist/marks/CellX.svelte.d.ts +105 -32
  64. package/dist/marks/CellY.svelte +2 -1
  65. package/dist/marks/CellY.svelte.d.ts +105 -32
  66. package/dist/marks/ColorLegend.svelte +24 -13
  67. package/dist/marks/ColorLegend.svelte.d.ts +1 -0
  68. package/dist/marks/CustomMark.svelte +16 -10
  69. package/dist/marks/CustomMark.svelte.d.ts +112 -31
  70. package/dist/marks/CustomMarkHTML.svelte +8 -2
  71. package/dist/marks/CustomMarkHTML.svelte.d.ts +8 -2
  72. package/dist/marks/DifferenceY.svelte +31 -20
  73. package/dist/marks/DifferenceY.svelte.d.ts +134 -55
  74. package/dist/marks/Dot.svelte +21 -11
  75. package/dist/marks/Dot.svelte.d.ts +117 -38
  76. package/dist/marks/DotX.svelte +2 -0
  77. package/dist/marks/DotX.svelte.d.ts +136 -62
  78. package/dist/marks/DotY.svelte +1 -0
  79. package/dist/marks/DotY.svelte.d.ts +135 -62
  80. package/dist/marks/Frame.svelte +47 -9
  81. package/dist/marks/Frame.svelte.d.ts +124 -41
  82. package/dist/marks/Geo.svelte +21 -12
  83. package/dist/marks/Geo.svelte.d.ts +105 -30
  84. package/dist/marks/Graticule.svelte +3 -0
  85. package/dist/marks/Graticule.svelte.d.ts +3 -0
  86. package/dist/marks/GridX.svelte +31 -16
  87. package/dist/marks/GridX.svelte.d.ts +108 -32
  88. package/dist/marks/GridY.svelte +30 -15
  89. package/dist/marks/GridY.svelte.d.ts +108 -32
  90. package/dist/marks/HTMLTooltip.svelte +14 -7
  91. package/dist/marks/HTMLTooltip.svelte.d.ts +7 -0
  92. package/dist/marks/Image.svelte +50 -25
  93. package/dist/marks/Image.svelte.d.ts +117 -35
  94. package/dist/marks/Line.svelte +67 -44
  95. package/dist/marks/Line.svelte.d.ts +119 -30
  96. package/dist/marks/LineX.svelte +2 -1
  97. package/dist/marks/LineX.svelte.d.ts +142 -69
  98. package/dist/marks/LineY.svelte +2 -1
  99. package/dist/marks/LineY.svelte.d.ts +142 -69
  100. package/dist/marks/Link.svelte +70 -46
  101. package/dist/marks/Link.svelte.d.ts +126 -41
  102. package/dist/marks/Pointer.svelte +24 -15
  103. package/dist/marks/Pointer.svelte.d.ts +7 -0
  104. package/dist/marks/Rect.svelte +13 -5
  105. package/dist/marks/Rect.svelte.d.ts +116 -35
  106. package/dist/marks/RectX.svelte +6 -3
  107. package/dist/marks/RectX.svelte.d.ts +158 -12
  108. package/dist/marks/RectY.svelte +6 -3
  109. package/dist/marks/RectY.svelte.d.ts +158 -12
  110. package/dist/marks/RegressionX.svelte +13 -6
  111. package/dist/marks/RegressionX.svelte.d.ts +8 -3
  112. package/dist/marks/RegressionY.svelte +13 -6
  113. package/dist/marks/RegressionY.svelte.d.ts +8 -3
  114. package/dist/marks/RuleX.svelte +18 -11
  115. package/dist/marks/RuleX.svelte.d.ts +112 -32
  116. package/dist/marks/RuleY.svelte +19 -12
  117. package/dist/marks/RuleY.svelte.d.ts +114 -34
  118. package/dist/marks/Spike.svelte +11 -5
  119. package/dist/marks/Spike.svelte.d.ts +146 -68
  120. package/dist/marks/Text.svelte +24 -7
  121. package/dist/marks/Text.svelte.d.ts +253 -75
  122. package/dist/marks/TickX.svelte +56 -48
  123. package/dist/marks/TickX.svelte.d.ts +114 -40
  124. package/dist/marks/TickY.svelte +59 -51
  125. package/dist/marks/TickY.svelte.d.ts +117 -43
  126. package/dist/marks/Trail.svelte +25 -13
  127. package/dist/marks/Trail.svelte.d.ts +116 -33
  128. package/dist/marks/Vector.svelte +20 -11
  129. package/dist/marks/Vector.svelte.d.ts +116 -35
  130. package/dist/marks/WaffleX.svelte +18 -16
  131. package/dist/marks/WaffleX.svelte.d.ts +131 -57
  132. package/dist/marks/WaffleY.svelte +16 -15
  133. package/dist/marks/WaffleY.svelte.d.ts +129 -56
  134. package/dist/marks/helpers/Anchor.svelte +17 -2
  135. package/dist/marks/helpers/Anchor.svelte.d.ts +16 -1
  136. package/dist/marks/helpers/AreaCanvas.svelte +8 -8
  137. package/dist/marks/helpers/BaseAxisX.svelte +38 -41
  138. package/dist/marks/helpers/BaseAxisX.svelte.d.ts +11 -17
  139. package/dist/marks/helpers/BaseAxisY.svelte +35 -35
  140. package/dist/marks/helpers/BaseAxisY.svelte.d.ts +12 -15
  141. package/dist/marks/helpers/Box.svelte +35 -28
  142. package/dist/marks/helpers/Box.svelte.d.ts +122 -50
  143. package/dist/marks/helpers/DotCanvas.svelte +11 -9
  144. package/dist/marks/helpers/GeoCanvas.svelte +7 -6
  145. package/dist/marks/helpers/LineCanvas.svelte +7 -7
  146. package/dist/marks/helpers/LinearGradientX.svelte +2 -2
  147. package/dist/marks/helpers/LinearGradientX.svelte.d.ts +1 -1
  148. package/dist/marks/helpers/LinearGradientY.svelte +2 -2
  149. package/dist/marks/helpers/LinearGradientY.svelte.d.ts +1 -1
  150. package/dist/marks/helpers/Marker.svelte +2 -2
  151. package/dist/marks/helpers/MarkerPath.svelte +15 -12
  152. package/dist/marks/helpers/MarkerPath.svelte.d.ts +105 -32
  153. package/dist/marks/helpers/MultilineText.svelte +24 -17
  154. package/dist/marks/helpers/MultilineText.svelte.d.ts +1 -1
  155. package/dist/marks/helpers/RectCanvas.svelte +31 -26
  156. package/dist/marks/helpers/RectPath.svelte +2 -2
  157. package/dist/marks/helpers/Regression.svelte +176 -86
  158. package/dist/marks/helpers/Regression.svelte.d.ts +20 -8
  159. package/dist/marks/helpers/RuleCanvas.svelte +9 -6
  160. package/dist/marks/helpers/TextCanvas.svelte +13 -9
  161. package/dist/marks/helpers/TextCanvas.svelte.d.ts +6 -6
  162. package/dist/marks/helpers/TickCanvas.svelte +6 -5
  163. package/dist/marks/helpers/TrailCanvas.svelte +16 -18
  164. package/dist/marks/helpers/TrailCanvas.svelte.d.ts +3 -5
  165. package/dist/marks/helpers/canvas.js +16 -9
  166. package/dist/marks/helpers/events.d.ts +2 -2
  167. package/dist/marks/helpers/events.js +14 -7
  168. package/dist/marks/helpers/waffle.d.ts +3 -3
  169. package/dist/marks/helpers/waffle.js +6 -4
  170. package/dist/regression/polynomial.d.ts +1 -1
  171. package/dist/regression/polynomial.js +5 -5
  172. package/dist/regression/utils/determination.d.ts +1 -1
  173. package/dist/regression/utils/determination.js +1 -1
  174. package/dist/regression/utils/geometry.d.ts +1 -1
  175. package/dist/regression/utils/interpose.d.ts +1 -1
  176. package/dist/regression/utils/interpose.js +1 -1
  177. package/dist/regression/utils/points.d.ts +1 -1
  178. package/dist/transforms/bin.d.ts +3 -3
  179. package/dist/transforms/bin.js +29 -20
  180. package/dist/transforms/bollinger.d.ts +8 -0
  181. package/dist/transforms/bollinger.js +9 -1
  182. package/dist/transforms/centroid.d.ts +4 -0
  183. package/dist/transforms/centroid.js +4 -0
  184. package/dist/transforms/density.d.ts +4 -4
  185. package/dist/transforms/density.js +20 -13
  186. package/dist/transforms/dodge.d.ts +12 -1
  187. package/dist/transforms/dodge.js +15 -6
  188. package/dist/transforms/group.d.ts +141 -4
  189. package/dist/transforms/group.js +4 -1
  190. package/dist/transforms/interval.d.ts +204 -60
  191. package/dist/transforms/jitter.d.ts +421 -4
  192. package/dist/transforms/jitter.js +10 -1
  193. package/dist/transforms/map.d.ts +412 -4
  194. package/dist/transforms/map.js +3 -3
  195. package/dist/transforms/normalize.d.ts +276 -5
  196. package/dist/transforms/normalize.js +5 -3
  197. package/dist/transforms/recordize.d.ts +17 -5
  198. package/dist/transforms/recordize.js +13 -9
  199. package/dist/transforms/rename.d.ts +11 -4
  200. package/dist/transforms/rename.js +7 -2
  201. package/dist/transforms/select.d.ts +722 -210
  202. package/dist/transforms/select.js +13 -1
  203. package/dist/transforms/shift.d.ts +8 -0
  204. package/dist/transforms/shift.js +20 -6
  205. package/dist/transforms/sort.d.ts +13 -258
  206. package/dist/transforms/sort.js +13 -10
  207. package/dist/transforms/stack.d.ts +58 -9
  208. package/dist/transforms/stack.js +27 -11
  209. package/dist/transforms/window.d.ts +221 -66
  210. package/dist/transforms/window.js +8 -2
  211. package/dist/types/axes.d.ts +43 -0
  212. package/dist/types/axes.js +1 -0
  213. package/dist/types/channel.d.ts +30 -2
  214. package/dist/types/data.d.ts +14 -1
  215. package/dist/types/facet.d.ts +5 -0
  216. package/dist/types/index.d.ts +33 -8
  217. package/dist/types/index.js +11 -7
  218. package/dist/types/mark.d.ts +124 -35
  219. package/dist/types/plot.d.ts +118 -16
  220. package/dist/types/scale.d.ts +125 -8
  221. package/package.json +178 -175
  222. package/dist/helpers/autoTicks.d.ts +0 -12
@@ -14,6 +14,7 @@
14
14
 
15
15
  import type {
16
16
  PlotOptions,
17
+ ResolvedPlotOptions,
17
18
  GenericMarkOptions,
18
19
  Mark,
19
20
  PlotScales,
@@ -59,6 +60,11 @@
59
60
 
60
61
  // default settings in the plot and marks can be overwritten by
61
62
  // defining the svelteplot/defaults context outside of Plot
63
+ const asGridDefaults = (opts: PlotDefaults['grid'] | undefined) =>
64
+ opts === true ? { implicit: true } : opts == null ? {} : opts;
65
+ const isImplicit = (opts: { implicit?: boolean } | true | undefined) =>
66
+ opts === true ? true : (opts?.implicit ?? false);
67
+
62
68
  const DEFAULTS: PlotDefaults = {
63
69
  height: 350,
64
70
  initialWidth: 500,
@@ -88,20 +94,20 @@
88
94
  axisY: {
89
95
  anchor: 'left',
90
96
  implicit: true,
91
- ...USER_DEFAULTS.axis,
92
- ...USER_DEFAULTS.axisY
97
+ ...(USER_DEFAULTS.axis as any),
98
+ ...(USER_DEFAULTS.axisY as any)
93
99
  },
94
100
  gridX: {
95
101
  implicit: false,
96
- ...USER_DEFAULTS.grid,
97
- ...USER_DEFAULTS.gridX
102
+ ...asGridDefaults(USER_DEFAULTS.grid),
103
+ ...asGridDefaults(USER_DEFAULTS.gridX)
98
104
  },
99
105
  gridY: {
100
106
  implicit: false,
101
- ...USER_DEFAULTS.grid,
102
- ...USER_DEFAULTS.gridY
107
+ ...asGridDefaults(USER_DEFAULTS.grid),
108
+ ...asGridDefaults(USER_DEFAULTS.gridY)
103
109
  }
104
- };
110
+ } as PlotDefaults;
105
111
 
106
112
  let {
107
113
  header,
@@ -165,7 +171,7 @@
165
171
  );
166
172
 
167
173
  const explicitDomains = $derived(
168
- new Set(SCALES.filter((scale) => !!initialOptions[scale]?.domain))
174
+ new Set(SCALES.filter((scale) => !!(initialOptions as any)[scale]?.domain))
169
175
  );
170
176
 
171
177
  // one-dimensional plots have different automatic margins and heights
@@ -178,7 +184,7 @@
178
184
  explicitScales,
179
185
  explicitDomains,
180
186
  hasProjection: !!initialOptions.projection,
181
- margin: initialOptions.margin,
187
+ margin: initialOptions.margin as number | 'auto' | undefined,
182
188
  inset: initialOptions.inset
183
189
  })
184
190
  );
@@ -211,7 +217,7 @@
211
217
 
212
218
  const defaultPointScaleHeight = $derived(
213
219
  explicitScales.has('r') && plotOptions.r.range
214
- ? plotOptions.r.range[1] * 2
220
+ ? (plotOptions.r.range[1] as number) * 2
215
221
  : DEFAULTS.pointScaleHeight
216
222
  );
217
223
 
@@ -227,8 +233,8 @@
227
233
  ? plotOptions.height(plotWidth)
228
234
  : maybeNumber(plotOptions.height) === null || plotOptions.height === 'auto'
229
235
  ? Math.round(
230
- preScales.projection && preScales.projection.aspectRatio
231
- ? ((plotWidth * preScales.projection.aspectRatio) / xFacetCount) *
236
+ preScales.projection && (preScales.projection as any).aspectRatio
237
+ ? ((plotWidth * (preScales.projection as any).aspectRatio) / xFacetCount) *
232
238
  yFacetCount +
233
239
  plotOptions.marginTop +
234
240
  plotOptions.marginBottom
@@ -254,7 +260,7 @@
254
260
  : maybeNumber(plotOptions.height)
255
261
  );
256
262
 
257
- const plotHeight = $derived(height - plotOptions.marginTop - plotOptions.marginBottom);
263
+ const plotHeight = $derived((height ?? 0) - plotOptions.marginTop - plotOptions.marginBottom);
258
264
 
259
265
  // TODO: check if there's still a reason to store and expose the plot body element
260
266
  let plotBody: HTMLDivElement | null = $state(null);
@@ -274,7 +280,7 @@
274
280
  const scales = computeScales(
275
281
  plotOptions,
276
282
  facetWidth || width,
277
- facetHeight || height,
283
+ facetHeight ?? height ?? 0,
278
284
  hasFilledDotMarks,
279
285
  marks,
280
286
  DEFAULTS
@@ -287,9 +293,9 @@
287
293
  return {
288
294
  options: plotOptions,
289
295
  width,
290
- height,
291
- facetWidth,
292
- facetHeight,
296
+ height: height ?? 0,
297
+ facetWidth: facetWidth ?? undefined,
298
+ facetHeight: facetHeight ?? undefined,
293
299
  plotHeight,
294
300
  plotWidth,
295
301
  scales,
@@ -297,7 +303,7 @@
297
303
  hasFilledDotMarks,
298
304
  body: plotBody,
299
305
  css
300
- };
306
+ } as any;
301
307
  }
302
308
 
303
309
  setContext('svelteplot', {
@@ -357,11 +363,11 @@
357
363
  const xDomainExtent =
358
364
  x.type === 'band' || x.type === 'point'
359
365
  ? x.domain.length
360
- : Math.abs(x.domain[1] - x.domain[0]);
366
+ : Math.abs((x.domain[1] as number) - (x.domain[0] as number));
361
367
  const yDomainExtent =
362
368
  y.type === 'band' || y.type === 'point'
363
369
  ? y.domain.length
364
- : Math.abs(y.domain[1] - y.domain[0]);
370
+ : Math.abs((y.domain[1] as number) - (y.domain[0] as number));
365
371
  return (
366
372
  ((plotWidth / xDomainExtent) * yDomainExtent) / aspectRatio + marginTop + marginBottom
367
373
  );
@@ -370,13 +376,13 @@
370
376
  function extendPlotOptions(
371
377
  initialOpts: Partial<PlotOptions>,
372
378
  opts: PlotOptionsParameters
373
- ): PlotOptions {
379
+ ): ResolvedPlotOptions {
374
380
  return mergeDeep<PlotOptions>(
375
- {},
376
- { sortOrdinalDomains: DEFAULTS.sortOrdinalDomains },
377
- smartDefaultPlotOptions(opts),
378
- initialOptions
379
- );
381
+ {} as Partial<PlotOptions>,
382
+ { sortOrdinalDomains: DEFAULTS.sortOrdinalDomains } as Partial<PlotOptions>,
383
+ smartDefaultPlotOptions(opts) as any,
384
+ initialOptions as any
385
+ ) as ResolvedPlotOptions;
380
386
  }
381
387
 
382
388
  function maybeMargin(
@@ -419,7 +425,7 @@
419
425
  explicitDomains,
420
426
  hasProjection,
421
427
  margin
422
- }: PlotOptionsParameters): PlotOptions {
428
+ }: PlotOptionsParameters): ResolvedPlotOptions {
423
429
  const autoXAxis = explicitScales.has('x') || explicitDomains.has('x');
424
430
  const autoYAxis = explicitScales.has('y') || explicitDomains.has('y');
425
431
  const isOneDimensional = autoXAxis !== autoYAxis;
@@ -444,9 +450,9 @@
444
450
  marginTop: maybeMargin(margin, 'top', DEFAULTS.margin, autoMargins),
445
451
  marginBottom: maybeMargin(margin, 'bottom', DEFAULTS.margin, autoMargins),
446
452
  inset: isOneDimensional ? 10 : DEFAULTS.inset,
447
- grid: (DEFAULTS.gridX?.implicit ?? false) && (DEFAULTS.gridY?.implicit ?? false),
453
+ grid: isImplicit(DEFAULTS.gridX) && isImplicit(DEFAULTS.gridY),
448
454
  axes: (DEFAULTS.axisX?.implicit ?? false) && (DEFAULTS.axisY?.implicit ?? false),
449
- frame: DEFAULTS.frame?.implicit ?? false,
455
+ frame: isImplicit(DEFAULTS.frame),
450
456
  projection: null,
451
457
  aspectRatio: null,
452
458
  facet: {},
@@ -464,7 +470,7 @@
464
470
  align: 0.5,
465
471
  tickSpacing: DEFAULTS.axisX.tickSpacing ?? 80,
466
472
  tickFormat: 'auto',
467
- grid: DEFAULTS.gridX.implicit ?? false
473
+ grid: isImplicit(DEFAULTS.gridX)
468
474
  },
469
475
  y: {
470
476
  type: 'auto',
@@ -479,7 +485,7 @@
479
485
  align: 0.5,
480
486
  tickSpacing: DEFAULTS.axisY.tickSpacing ?? 50,
481
487
  tickFormat: 'auto',
482
- grid: DEFAULTS.gridY.implicit ?? false
488
+ grid: isImplicit(DEFAULTS.gridY)
483
489
  },
484
490
  opacity: {
485
491
  type: 'linear',
@@ -504,14 +510,14 @@
504
510
  padding: 0,
505
511
  align: 0
506
512
  },
507
- color: { type: 'auto', unknown: DEFAULTS.unknown },
513
+ color: { type: 'auto' as any, unknown: DEFAULTS.unknown },
508
514
  length: { type: 'linear' },
509
515
  symbol: { type: 'ordinal' },
510
516
  fx: { type: 'band', axis: 'top' },
511
517
  fy: { type: 'band', axis: 'right' },
512
518
  locale: DEFAULTS.locale,
513
519
  css: DEFAULTS.css
514
- };
520
+ } as ResolvedPlotOptions;
515
521
  }
516
522
 
517
523
  const mapXY = $derived((x: RawValue, y: RawValue) => {
@@ -542,7 +548,7 @@
542
548
  {#if children}
543
549
  {@render children({
544
550
  width,
545
- height,
551
+ height: height ?? 0,
546
552
  options: plotOptions,
547
553
  scales: plotState.scales,
548
554
  mapXY,
@@ -558,7 +564,7 @@
558
564
  {#if overlay}<div class="plot-overlay">
559
565
  {@render overlay?.({
560
566
  width,
561
- height,
567
+ height: height ?? 0,
562
568
  options: plotOptions,
563
569
  scales: plotState.scales,
564
570
  mapXY,
@@ -1,5 +1,5 @@
1
1
  import type { ColorScaleOptions, PlotDefaults, PlotOptions, RawValue, ScaleName, ScaleOptions, ScaleType } from '../types/index.js';
2
- export declare function autoScale({ name, type, domain, scaleOptions, plotOptions, plotWidth, plotHeight, plotHasFilledDotMarks, plotDefaults }: {
2
+ export declare function autoScale({ name, type, domain, scaleOptions, plotOptions, plotWidth, plotHeight, plotHasFilledDotMarks, plotDefaults: _plotDefaults }: {
3
3
  name: ScaleName;
4
4
  type: ScaleType;
5
5
  domain: RawValue[];
@@ -9,8 +9,8 @@ export declare function autoScale({ name, type, domain, scaleOptions, plotOption
9
9
  plotHeight: number;
10
10
  plotHasFilledDotMarks: boolean;
11
11
  plotDefaults: PlotDefaults;
12
- }): (val: any) => any;
13
- export declare function autoScaleColor({ type, domain, scaleOptions, plotOptions, plotWidth, plotHeight, plotHasFilledDotMarks, plotDefaults }: {
12
+ }): any;
13
+ export declare function autoScaleColor({ type, domain, scaleOptions, plotOptions: _plotOptions, plotWidth: _plotWidth, plotHeight: _plotHeight, plotHasFilledDotMarks: _plotHasFilledDotMarks, plotDefaults }: {
14
14
  name: ScaleName;
15
15
  type: ScaleType;
16
16
  domain: RawValue[];
@@ -41,7 +41,7 @@ const ThresholdScales = {
41
41
  quantize: scaleQuantize,
42
42
  quantile: scaleQuantile
43
43
  };
44
- export function autoScale({ name, type, domain, scaleOptions, plotOptions, plotWidth, plotHeight, plotHasFilledDotMarks, plotDefaults }) {
44
+ export function autoScale({ name, type, domain, scaleOptions, plotOptions, plotWidth, plotHeight, plotHasFilledDotMarks, plotDefaults: _plotDefaults }) {
45
45
  let fn;
46
46
  let range;
47
47
  range =
@@ -55,7 +55,7 @@ export function autoScale({ name, type, domain, scaleOptions, plotOptions, plotW
55
55
  const scaleProps = {
56
56
  domain,
57
57
  range,
58
- ...((type === 'linear' || type === 'log') && scaleOptions.nice
58
+ ...((type === 'linear' || type === 'log' || type === 'time') && scaleOptions.nice
59
59
  ? {
60
60
  nice: scaleOptions.nice ? niceTickCount : true
61
61
  }
@@ -92,6 +92,8 @@ export function autoScale({ name, type, domain, scaleOptions, plotOptions, plotW
92
92
  }
93
93
  : {})
94
94
  };
95
+ // The scale function is augmented with custom ticks() methods below,
96
+ // so we type it broadly to allow property assignments
95
97
  fn = callWithProps(Scales[type], [], scaleProps);
96
98
  if (type === 'band' || type === 'point') {
97
99
  fn.ticks = () => domain;
@@ -102,13 +104,13 @@ export function autoScale({ name, type, domain, scaleOptions, plotOptions, plotW
102
104
  else if (type === 'symlog') {
103
105
  const maxabs = Math.max(Math.abs(domain[0]), Math.abs(domain[1]));
104
106
  fn.ticks = (count) => {
105
- const ticks = getLogTicks([scaleProps.constant + 1, maxabs], count / 2);
107
+ const ticks = getLogTicks([(scaleProps.constant ?? 1) + 1, maxabs], count / 2);
106
108
  return [...ticks.map((t) => -t).reverse(), 0, ...ticks];
107
109
  };
108
110
  }
109
111
  return fn;
110
112
  }
111
- export function autoScaleColor({ type, domain, scaleOptions, plotOptions, plotWidth, plotHeight, plotHasFilledDotMarks, plotDefaults }) {
113
+ export function autoScaleColor({ type, domain, scaleOptions, plotOptions: _plotOptions, plotWidth: _plotWidth, plotHeight: _plotHeight, plotHasFilledDotMarks: _plotHasFilledDotMarks, plotDefaults }) {
112
114
  let fn;
113
115
  let range;
114
116
  // special treatment for color scales
@@ -117,12 +119,14 @@ export function autoScaleColor({ type, domain, scaleOptions, plotOptions, plotWi
117
119
  // categorical
118
120
  let scheme_ = scheme || plotDefaults.categoricalColorScheme;
119
121
  if (isPlainObject(scheme_)) {
120
- const newScheme = Object.values(scheme_);
121
- const newDomain = Object.keys(scheme_);
122
+ const schemeObj = scheme_;
123
+ const newScheme = Object.values(schemeObj);
124
+ const newDomain = Object.keys(schemeObj);
122
125
  // for every value in domain that's not part of the scheme, map to unknown
123
126
  for (const v of domain) {
124
- if (scheme_[v] == null) {
125
- newDomain.push(v);
127
+ const key = String(v);
128
+ if (schemeObj[key] == null) {
129
+ newDomain.push(key);
126
130
  newScheme.push(unknown);
127
131
  }
128
132
  }
@@ -133,11 +137,13 @@ export function autoScaleColor({ type, domain, scaleOptions, plotOptions, plotWi
133
137
  range = Array.isArray(scheme_)
134
138
  ? scheme_
135
139
  : isCategoricalScheme(scheme_)
136
- ? categoricalSchemes.get(scheme_)
140
+ ? (categoricalSchemes.get(scheme_) ?? [])
137
141
  : ordinalScheme(scheme_)(domain.length);
138
- fn = scaleOrdinal().domain(domain).range(range);
142
+ fn = scaleOrdinal()
143
+ .domain(domain)
144
+ .range(range);
139
145
  }
140
- else if (!!ThresholdScales[type]) {
146
+ else if (ThresholdScales[type]) {
141
147
  const scheme_ = scheme || plotDefaults.colorScheme;
142
148
  range =
143
149
  Array.isArray(scheme_) && (scaleOptions.n == null || scaleOptions.n === scheme_.length)
@@ -146,7 +152,7 @@ export function autoScaleColor({ type, domain, scaleOptions, plotOptions, plotWi
146
152
  ? // interpolate n colors from custom colors
147
153
  d3Range(n)
148
154
  .map((i) => i / (n - 1))
149
- .map(scaleLinear(scheme_.map((c, i) => i / (scheme_.length - 1)), scheme_).interpolate(interpolateLab))
155
+ .map(scaleLinear(scheme_.map((_c, i) => i / (scheme_.length - 1)), scheme_).interpolate(interpolateLab))
150
156
  : interpolate
151
157
  ? d3Range(n).map((i) => interpolate(i / (n - 1)))
152
158
  : isOrdinalScheme(scheme_)
@@ -157,11 +163,15 @@ export function autoScaleColor({ type, domain, scaleOptions, plotOptions, plotWi
157
163
  }
158
164
  if (scaleOptions.reverse)
159
165
  range = range.toReversed();
160
- fn = ThresholdScales[type]().domain(domain).range(range);
166
+ fn = ThresholdScales[type]()
167
+ .domain(domain)
168
+ .range(range);
161
169
  }
162
- else if (!!SequentialScales[type] || !!DivergingScales[type]) {
170
+ else if (SequentialScales[type] ||
171
+ DivergingScales[type]) {
163
172
  // continuous color scale
164
- const scale = SequentialScales[type] || DivergingScales[type];
173
+ const scale = (SequentialScales[type] ||
174
+ DivergingScales[type]);
165
175
  const scheme_ = scheme || plotDefaults.colorScheme;
166
176
  if (interpolate) {
167
177
  // user-defined interpolation function [0, 1] -> color
@@ -172,14 +182,14 @@ export function autoScaleColor({ type, domain, scaleOptions, plotOptions, plotWi
172
182
  const step = 1 / (scheme_.length - 1);
173
183
  fn = scale(domain, (type === 'linear' ? scaleLinear : scaleLog)(d3Range(0, 1 + step / 2, step), scheme_).interpolate(interpolateLab));
174
184
  }
175
- else if (!!DivergingScales[type] ||
185
+ else if (DivergingScales[type] ||
176
186
  (scaleOptions.type === 'auto' && isDivergingScheme(scheme_))) {
177
187
  // diverging color scheme, explicit or auto-detected
178
188
  const maxabs = Math.max(Math.abs(domain[0]), Math.abs(domain[1]));
179
189
  const domain_ = pivot != null ? [domain[0], pivot, domain[1]] : [-maxabs, 0, maxabs];
180
190
  fn = scale(domain_, quantitativeScheme(scheme_));
181
191
  }
182
- else if (!!SequentialScales[type] ||
192
+ else if (SequentialScales[type] ||
183
193
  (scaleOptions.type === 'auto' && isQuantitativeScheme(scheme_))) {
184
194
  // sequential
185
195
  fn = scale(domain, quantitativeScheme(scheme_));
@@ -191,7 +201,7 @@ export function autoScaleColor({ type, domain, scaleOptions, plotOptions, plotWi
191
201
  if (!fn) {
192
202
  console.error('color problem', type);
193
203
  // problem
194
- fn = () => 'red';
204
+ fn = (() => 'red');
195
205
  fn.range = () => ['red'];
196
206
  }
197
207
  return fn;
@@ -37,6 +37,8 @@ export function autoTicks(type, ticks, interval, domain, scaleFn, count) {
37
37
  if (lo == null || hi == null)
38
38
  return [];
39
39
  const I = maybeInterval(interval);
40
+ if (!I)
41
+ return [];
40
42
  return I.range(lo, I.offset(hi)).filter((d) => d >= lo && d <= hi);
41
43
  }
42
44
  return typeof scaleFn.ticks === 'function' ? scaleFn.ticks(count) : [];
@@ -1,6 +1,5 @@
1
- import type { RawValue } from '../types/index.js';
2
1
  /**
3
2
  * Helper function to call a D3 "function class" while also calling
4
3
  * property setter functions on the result.
5
4
  */
6
- export default function <T extends object>(d3func: (...args: RawValue[]) => T, args?: RawValue[], props?: Record<string, RawValue>): T;
5
+ export default function <T extends object>(d3func: (...args: any[]) => T, args?: any[], props?: Record<string, any>): T;
@@ -46,4 +46,3 @@ export function getEmptyFacets(marks, fxValues, fyValues) {
46
46
  }
47
47
  return out;
48
48
  }
49
- // TODO: write unit tests
@@ -12,7 +12,7 @@ export function coalesce(...args) {
12
12
  return null; // Return null if all arguments are null or undefined
13
13
  }
14
14
  export function testFilter(datum, options) {
15
- return options.filter == null || resolveProp(options.filter, datum);
15
+ return (options.filter == null || resolveProp(options.filter, datum));
16
16
  }
17
17
  export function randomId() {
18
18
  return Math.ceil(1e9 + Math.random() * 1e9).toString(36);
@@ -1,5 +1,3 @@
1
- type ObjectType = {
2
- [key: string]: any;
3
- };
1
+ type ObjectType = Record<string, unknown>;
4
2
  export default function mergeDeep<T extends ObjectType>(target: Partial<T>, ...sources: Partial<T>[]): T;
5
3
  export {};
@@ -1,25 +1,24 @@
1
1
  function isObject(item) {
2
- return item && typeof item === 'object' && !Array.isArray(item);
2
+ return item != null && typeof item === 'object' && !Array.isArray(item);
3
3
  }
4
4
  export default function mergeDeep(target, ...sources) {
5
- if (!sources.length)
6
- return target;
7
- const source = sources.shift();
8
- if (isObject(target) && isObject(source)) {
9
- for (const key in source) {
10
- if (isObject(source[key])) {
11
- if (!target[key]) {
12
- Object.assign(target, { [key]: {} });
5
+ for (const source of sources) {
6
+ if (isObject(target) && isObject(source)) {
7
+ for (const key in source) {
8
+ if (isObject(source[key])) {
9
+ if (!target[key]) {
10
+ Object.assign(target, { [key]: {} });
11
+ }
12
+ else {
13
+ target[key] = Object.assign({}, target[key]);
14
+ }
15
+ mergeDeep(target[key], source[key]);
13
16
  }
14
- else {
15
- target[key] = Object.assign({}, target[key]);
17
+ else if (source[key] !== null) {
18
+ Object.assign(target, { [key]: source[key] });
16
19
  }
17
- mergeDeep(target[key], source[key]);
18
- }
19
- else if (source[key] !== null) {
20
- Object.assign(target, { [key]: source[key] });
21
20
  }
22
21
  }
23
22
  }
24
- return mergeDeep(target, ...sources);
23
+ return target;
25
24
  }
@@ -1,3 +1,4 @@
1
+ import { type GeoStream } from 'd3-geo';
1
2
  export type Clip = boolean | null | number | 'frame';
2
3
  type ProjectionOptions = {
3
4
  type: string;
@@ -25,9 +26,9 @@ export declare function createProjection({ projOptions, inset: globalInset, inse
25
26
  insetRight?: number;
26
27
  insetBottom?: number;
27
28
  insetLeft?: number;
28
- } | undefined, dimensions: Dimensions): string | ProjectionOptions | {
29
+ } | undefined, dimensions: Dimensions): ProjectionOptions | {
29
30
  aspectRatio: number;
30
- invert([x, y]: [any, any]): any;
31
- stream: (s: any) => any;
31
+ invert([x, y]: [number, number]): any;
32
+ stream: (s: GeoStream) => any;
32
33
  } | undefined;
33
34
  export {};
@@ -7,7 +7,7 @@ export function createProjection({ projOptions, inset: globalInset = 2, insetTop
7
7
  if (projOptions == null)
8
8
  return;
9
9
  // projection function passed as projection option
10
- if (typeof projOptions.stream === 'function')
10
+ if (typeof projOptions !== 'string' && typeof projOptions.stream === 'function')
11
11
  return projOptions; // d3 projection
12
12
  let options;
13
13
  let domain;
@@ -34,7 +34,16 @@ export function createProjection({ projOptions, inset: globalInset = 2, insetTop
34
34
  }
35
35
  // let projFactory;
36
36
  let aspectRatio = defaultAspectRatio;
37
- const projFactory = projOptions;
37
+ let projFactory;
38
+ if (typeof projOptions === 'function') {
39
+ // After destructuring, projOptions may be reassigned from an object `type` property,
40
+ // which can be a projection initializer function.
41
+ projFactory = projOptions;
42
+ }
43
+ else if (typeof projOptions === 'string') {
44
+ // String projection types are not handled here; treat as no projection.
45
+ return;
46
+ }
38
47
  // Compute the frame dimensions and invoke the projection initializer.
39
48
  const { width, height, marginLeft, marginRight, marginTop, marginBottom } = dimensions;
40
49
  const dx = width - marginLeft - marginRight - insetLeft - insetRight;
@@ -43,7 +52,7 @@ export function createProjection({ projOptions, inset: globalInset = 2, insetTop
43
52
  // The projection initializer might decide to not use a projection.
44
53
  if (projInstance == null)
45
54
  return;
46
- clip = maybePostClip(clip, marginLeft, marginTop, width - marginRight, height - marginBottom);
55
+ const postClip = maybePostClip(clip, marginLeft, marginTop, width - marginRight, height - marginBottom);
47
56
  // Translate the origin to the top-left corner, respecting margins and insets.
48
57
  let tx = marginLeft + insetLeft;
49
58
  let ty = marginTop + insetTop;
@@ -63,7 +72,10 @@ export function createProjection({ projOptions, inset: globalInset = 2, insetTop
63
72
  this.stream.point(x * k + tx, y * k + ty);
64
73
  }
65
74
  });
66
- invertTransform = ([x, y]) => [(x - tx) / k, (y - ty) / k];
75
+ invertTransform = ([x, y]) => [
76
+ (x - tx) / k,
77
+ (y - ty) / k
78
+ ];
67
79
  }
68
80
  else {
69
81
  // eslint-disable-next-line no-console
@@ -88,7 +100,7 @@ export function createProjection({ projOptions, inset: globalInset = 2, insetTop
88
100
  invert([x, y]) {
89
101
  return projInstance.invert(invertTransform([x, y]));
90
102
  },
91
- stream: (s) => projInstance.stream(transform.stream(clip(s)))
103
+ stream: (s) => projInstance.stream(transform.stream(postClip(s)))
92
104
  };
93
105
  }
94
106
  function maybePostClip(clip, x1, y1, x2, y2) {
@@ -1,10 +1,10 @@
1
- import type { ChannelName, Channels, DataRecord, DataRow, RawValue } from '../types/index.js';
2
- type ReducerFunc = (group: Iterable<DataRow>) => RawValue;
1
+ import type { ChannelName, Channels, DataRecord } from '../types/index.js';
2
+ type ReducerFunc = (group: Iterable<any>, ...rest: any[]) => any;
3
3
  type ReducerOption = ReducerName | ReducerFunc;
4
4
  type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
5
5
  export type ReducerPercentile = (`p${Digit}${Digit}` & Record<never, never>) | 'p25' | 'p50' | 'p75';
6
6
  export type ReducerName = 'count' | 'deviation' | 'difference' | 'first' | 'last' | 'max' | 'mean' | 'median' | 'min' | 'mode' | 'ratio' | 'sum' | 'variance' | ReducerPercentile;
7
- export declare const Reducer: Record<ReducerName, ReducerFunc>;
7
+ export declare const Reducer: Record<string, ReducerFunc>;
8
8
  export declare function mayberReducer(r: ReducerOption): ReducerFunc;
9
- export declare function reduceOutputs(newDatum: DataRecord, data: DataRecord[], options: Record<ChannelName, ReducerOption>, outputs: Iterable<ChannelName>, channels: Channels, newChannels: Channels): void;
9
+ export declare function reduceOutputs(newDatum: DataRecord, data: DataRecord[], options: Record<ChannelName, ReducerOption>, outputs: Iterable<ChannelName>, channels: Channels<any>, newChannels: Channels<any>): void;
10
10
  export {};
@@ -7,6 +7,8 @@ const niceReduceNames = {
7
7
  deviation: 'Standard Deviation',
8
8
  mean: 'Average'
9
9
  };
10
+ // Typed as Record<string, ...> because the Proxy wrapper adds virtual
11
+ // percentile keys (p00–p99) that don't exist on the static object
10
12
  const StaticReducer = {
11
13
  count: (d) => Array.from(d).length,
12
14
  min,
@@ -18,10 +20,10 @@ const StaticReducer = {
18
20
  identity: (d) => d,
19
21
  variance,
20
22
  deviation,
21
- first: (d) => d[0],
22
- last: (d) => d.at(-1),
23
- difference: (d) => d.at(-1) - d[0],
24
- ratio: (d) => d.at(-1) / d[0]
23
+ first: ((d) => d[0]),
24
+ last: ((d) => d.at(-1)),
25
+ difference: ((d) => d.at(-1) - d[0]),
26
+ ratio: ((d) => d.at(-1) / d[0])
25
27
  // TODO: proportion
26
28
  // TODO: proportion-facet
27
29
  // TODO: min-index
@@ -1,6 +1,7 @@
1
+ // @ts-expect-error loess has no type declarations
1
2
  import Loess from 'loess';
2
3
  function toNumber(d) {
3
- if (typeof d.getTime === 'function')
4
+ if (d instanceof Date)
4
5
  return d.getTime();
5
6
  return d;
6
7
  }
@@ -11,11 +11,14 @@ type ChannelOptions = {
11
11
  };
12
12
  export declare function toChannelOption(name: ScaledChannelName, channel: ChannelAccessor | ChannelAlias): ChannelOptions;
13
13
  export declare function resolveChannel<T>(channel: ChannelName, datum: DataRow<T>, channels: Partial<Record<ChannelName, ChannelAccessor<T> | ChannelAlias>>): RawValue;
14
- export declare function resolveScaledStyleProps(datum: DataRecord, channels: Partial<Record<ScaledChannelName, ChannelAccessor>>, useScale: Record<ScaledChannelName, boolean>, plot: PlotState, defaultColorProp?: 'fill' | 'stroke' | null): any;
15
- export declare function resolveScaledStyles(datum: DataRecord, channels: Partial<Record<ScaledChannelName, ChannelAccessor> & {
14
+ export declare function resolveScaledStyleProps(datum: DataRecord, channels: Partial<Record<ScaledChannelName | MarkStyleProps, ChannelAccessor>>, useScale: Record<ScaledChannelName, boolean>, plot: PlotState, defaultColorProp?: 'fill' | 'stroke' | null): {
15
+ fill: string;
16
+ stroke: string;
17
+ };
18
+ export declare function resolveScaledStyles(datum: DataRecord, channels: Partial<Record<ScaledChannelName | MarkStyleProps, ChannelAccessor> & {
16
19
  style: string;
17
20
  }>, useScale: Record<ScaledChannelName, boolean>, plot: PlotState, defaultColorProp?: 'fill' | 'stroke' | null): string;
18
- export declare function resolveStyles(plot: PlotState, datum: ScaledDataRecord, channels: Partial<Record<ChannelName & MarkStyleProps, ChannelAccessor> & {
21
+ export declare function resolveStyles(plot: PlotState, datum: ScaledDataRecord, channels: Partial<Record<ScaledChannelName | MarkStyleProps, ChannelAccessor> & {
19
22
  style: string;
20
23
  }>, defaultColorProp: "fill" | "stroke" | null | undefined, useScale: Record<ScaledChannelName, boolean>, recomputeChannels?: boolean): [string | null, string | null];
21
24
  export {};