layerchart 0.42.0 → 0.43.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 (114) hide show
  1. package/dist/components/Arc.svelte +27 -20
  2. package/dist/components/Arc.svelte.d.ts +2 -2
  3. package/dist/components/Area.svelte +11 -7
  4. package/dist/components/Area.svelte.d.ts +3 -3
  5. package/dist/components/AreaStack.svelte +3 -2
  6. package/dist/components/Axis.svelte +38 -41
  7. package/dist/components/Axis.svelte.d.ts +5 -4
  8. package/dist/components/Bar.svelte +6 -5
  9. package/dist/components/Bar.svelte.d.ts +3 -2
  10. package/dist/components/Bars.svelte +5 -4
  11. package/dist/components/Bars.svelte.d.ts +3 -2
  12. package/dist/components/Bounds.svelte +7 -5
  13. package/dist/components/Bounds.svelte.d.ts +8 -8
  14. package/dist/components/Brush.svelte +8 -6
  15. package/dist/components/Brush.svelte.d.ts +2 -2
  16. package/dist/components/Calendar.svelte +4 -3
  17. package/dist/components/Chart.svelte +71 -85
  18. package/dist/components/Chart.svelte.d.ts +216 -24
  19. package/dist/components/ChartClipPath.svelte +6 -6
  20. package/dist/components/ChartContext.svelte +19 -0
  21. package/dist/components/ChartContext.svelte.d.ts +89 -0
  22. package/dist/components/Circle.svelte +1 -0
  23. package/dist/components/ClipPath.svelte +1 -0
  24. package/dist/components/ForceSimulation.svelte +4 -3
  25. package/dist/components/ForceSimulation.svelte.d.ts +1 -2
  26. package/dist/components/Frame.svelte +8 -7
  27. package/dist/components/Frame.svelte.d.ts +1 -1
  28. package/dist/components/GeoCircle.svelte.d.ts +1 -2
  29. package/dist/components/GeoContext.svelte +2 -1
  30. package/dist/components/GeoContext.svelte.d.ts +5 -5
  31. package/dist/components/GeoEdgeFade.svelte +2 -5
  32. package/dist/components/GeoPath.svelte +9 -5
  33. package/dist/components/GeoPath.svelte.d.ts +7 -4
  34. package/dist/components/GeoPoint.svelte +0 -1
  35. package/dist/components/GeoSpline.svelte +2 -11
  36. package/dist/components/GeoTile.svelte +3 -1
  37. package/dist/components/Group.svelte +4 -2
  38. package/dist/components/Highlight.svelte +16 -10
  39. package/dist/components/Highlight.svelte.d.ts +16 -15
  40. package/dist/components/HitCanvas.svelte +5 -2
  41. package/dist/components/HitCanvas.svelte.d.ts +1 -1
  42. package/dist/components/Hull.svelte +6 -3
  43. package/dist/components/Labels.svelte +5 -4
  44. package/dist/components/Labels.svelte.d.ts +0 -1
  45. package/dist/components/Legend.svelte +4 -4
  46. package/dist/components/Legend.svelte.d.ts +2 -2
  47. package/dist/components/Line.svelte +1 -0
  48. package/dist/components/Link.svelte +3 -1
  49. package/dist/components/Pack.svelte +4 -3
  50. package/dist/components/Partition.svelte +3 -3
  51. package/dist/components/Partition.svelte.d.ts +2 -1
  52. package/dist/components/Pie.svelte +7 -6
  53. package/dist/components/Pie.svelte.d.ts +2 -4
  54. package/dist/components/Point.svelte +2 -2
  55. package/dist/components/Points.svelte +4 -3
  56. package/dist/components/RadialGradient.svelte +2 -2
  57. package/dist/components/RadialGradient.svelte.d.ts +0 -1
  58. package/dist/components/Rect.svelte +1 -0
  59. package/dist/components/Rule.svelte +3 -3
  60. package/dist/components/Sankey.svelte +9 -3
  61. package/dist/components/Sankey.svelte.d.ts +2 -2
  62. package/dist/components/Spline.svelte +9 -6
  63. package/dist/components/Spline.svelte.d.ts +2 -2
  64. package/dist/components/Text.svelte +1 -2
  65. package/dist/components/Threshold.svelte +2 -2
  66. package/dist/components/Threshold.svelte.d.ts +2 -2
  67. package/dist/components/TileImage.svelte +4 -1
  68. package/dist/components/Tooltip.svelte +3 -3
  69. package/dist/components/TooltipContext.svelte +46 -15
  70. package/dist/components/TooltipContext.svelte.d.ts +1 -1
  71. package/dist/components/TooltipItem.svelte.d.ts +1 -1
  72. package/dist/components/TransformContext.svelte +17 -6
  73. package/dist/components/TransformControls.svelte +1 -0
  74. package/dist/components/Tree.svelte +4 -3
  75. package/dist/components/Tree.svelte.d.ts +3 -3
  76. package/dist/components/Treemap.svelte +11 -12
  77. package/dist/components/Treemap.svelte.d.ts +6 -6
  78. package/dist/components/Voronoi.svelte +8 -3
  79. package/dist/components/layout/Canvas.svelte +5 -3
  80. package/dist/components/layout/Html.svelte +3 -3
  81. package/dist/components/layout/Svg.svelte +6 -3
  82. package/dist/docs/Code.svelte +2 -3
  83. package/dist/docs/GeoDebug.svelte +4 -4
  84. package/dist/docs/Link.svelte +2 -4
  85. package/dist/docs/Link.svelte.d.ts +14 -5
  86. package/dist/docs/Preview.svelte +1 -1
  87. package/dist/docs/TilesetField.svelte +1 -1
  88. package/dist/docs/TilesetField.svelte.d.ts +1 -1
  89. package/dist/docs/ViewSourceButton.svelte +4 -4
  90. package/dist/docs/ViewSourceButton.svelte.d.ts +5 -3
  91. package/dist/stores/motionStore.d.ts +2 -2
  92. package/dist/stores/motionStore.js +4 -2
  93. package/dist/utils/array.d.ts +5 -0
  94. package/dist/utils/array.js +7 -0
  95. package/dist/utils/common.d.ts +2 -0
  96. package/dist/utils/common.js +18 -0
  97. package/dist/utils/genData.d.ts +23 -23
  98. package/dist/utils/genData.js +3 -2
  99. package/dist/utils/geo.d.ts +3 -3
  100. package/dist/utils/geo.js +2 -0
  101. package/dist/utils/graph.d.ts +1 -1
  102. package/dist/utils/graph.js +3 -3
  103. package/dist/utils/hierarchy.d.ts +1 -1
  104. package/dist/utils/hierarchy.js +1 -0
  105. package/dist/utils/rect.d.ts +5 -3
  106. package/dist/utils/rect.js +7 -11
  107. package/dist/utils/scales.d.ts +21 -12
  108. package/dist/utils/scales.js +3 -1
  109. package/dist/utils/stack.d.ts +5 -2
  110. package/dist/utils/stack.js +20 -7
  111. package/dist/utils/threshold.js +1 -0
  112. package/dist/utils/treemap.d.ts +5 -2
  113. package/dist/utils/treemap.js +1 -1
  114. package/package.json +5 -2
@@ -1,6 +1,7 @@
1
1
  import { derived } from 'svelte/store';
2
2
  import { max, min } from 'd3-array';
3
3
  import { groupScaleBand, isScaleBand } from './scales.js';
4
+ import { accessor } from './common.js';
4
5
  // TOOD: Pass in overrides for `x` and `y` accessors
5
6
  export function createDimensionGetter(context, options) {
6
7
  const { flatData, xGet, yGet, xScale, yScale, x: xAccessor, y: yAccessor } = context;
@@ -10,19 +11,17 @@ export function createDimensionGetter(context, options) {
10
11
  // Use `xscale.domain()` instead of `$xDomain` to include `nice()` being applied
11
12
  const [minXDomain, maxXDomain] = $xScale.domain();
12
13
  const [minYDomain, maxYDomain] = $yScale.domain();
14
+ // @ts-expect-error
13
15
  return function getter(item) {
14
16
  if (isScaleBand($yScale)) {
15
17
  // Horizontal band
16
18
  const y1Scale = groupBy
17
19
  ? groupScaleBand($yScale, $flatData, groupBy, options?.groupPadding)
18
20
  : null;
21
+ // @ts-expect-error
19
22
  const y = firstValue($yGet(item)) + (y1Scale ? y1Scale(item[groupBy]) : 0) + inset / 2;
20
23
  const height = Math.max(0, $yScale.bandwidth ? (y1Scale ? y1Scale.bandwidth() : $yScale.bandwidth()) - inset : 0);
21
- const _x = options?.x
22
- ? typeof options.x === 'string'
23
- ? (d) => d[options.x]
24
- : options?.x
25
- : $xAccessor;
24
+ const _x = accessor(options?.x ?? $xAccessor);
26
25
  const xValue = _x(item);
27
26
  let left = 0;
28
27
  let right = 0;
@@ -55,16 +54,13 @@ export function createDimensionGetter(context, options) {
55
54
  }
56
55
  else {
57
56
  // Vertical band or linear
58
- const x1Scale = groupBy
57
+ const x1Scale = groupBy && isScaleBand($xScale)
59
58
  ? groupScaleBand($xScale, $flatData, groupBy, options?.groupPadding)
60
59
  : null;
60
+ // @ts-expect-error
61
61
  const x = firstValue($xGet(item)) + (x1Scale ? x1Scale(item[groupBy]) : 0) + inset / 2;
62
62
  const width = Math.max(0, $xScale.bandwidth ? (x1Scale ? x1Scale.bandwidth() : $xScale.bandwidth()) - inset : 0);
63
- const _y = options?.y
64
- ? typeof options.y === 'string'
65
- ? (d) => d[options.y]
66
- : options?.y
67
- : $yAccessor;
63
+ const _y = accessor(options?.y ?? $yAccessor);
68
64
  const yValue = _y(item);
69
65
  let top = 0;
70
66
  let bottom = 0;
@@ -1,6 +1,15 @@
1
1
  import { tweened, spring } from 'svelte/motion';
2
2
  import { type MotionOptions } from '../stores/motionStore.js';
3
3
  import { type ScaleBand } from 'd3-scale';
4
+ export type AnyScale<Domain = any, Range = any, Input = Domain, Output = any> = {
5
+ (value: Input): Output;
6
+ invert?: (value: Output) => Input;
7
+ domain(): Domain[];
8
+ range(): Range[];
9
+ bandwidth?: Function;
10
+ ticks?: Function;
11
+ tickFormat?: Function;
12
+ };
4
13
  /**
5
14
  * Implemenation for missing `scaleBand().invert()`
6
15
  *
@@ -11,13 +20,13 @@ import { type ScaleBand } from 'd3-scale';
11
20
  * https://github.com/d3/d3-scale/blob/11777dac7d4b0b3e229d658aee3257ea67bd5ffa/src/band.js#L32
12
21
  * https://gist.github.com/LuisSevillano/d53a1dc529eef518780c6df99613e2fd
13
22
  */
14
- export declare function scaleBandInvert(scale: any): (value: any) => any;
15
- export declare function isScaleBand(scale: any): boolean;
23
+ export declare function scaleBandInvert(scale: ScaleBand<any>): (value: number) => any;
24
+ export declare function isScaleBand(scale: AnyScale<any, any>): scale is ScaleBand<any>;
16
25
  /**
17
26
  * Generic way to invert a scale value, handling scaleBand and continuous scales (linear, time, etc).
18
27
  * Useful to map mouse event location (x,y) to domain value
19
28
  */
20
- export declare function scaleInvert(scale: any, value: number): any;
29
+ export declare function scaleInvert(scale: AnyScale<any, any>, value: number): any;
21
30
  /** Create a `scaleBand()` within another scaleBand()'s bandwidth (typically a x1 of an x0 scale, used for grouping) */
22
31
  export declare function groupScaleBand<Domain extends {
23
32
  toString(): string;
@@ -28,24 +37,24 @@ export declare function groupScaleBand<Domain extends {
28
37
  /**
29
38
  * Animate d3-scale as domain and/or range are updated using tweened store
30
39
  */
31
- export declare function tweenedScale(scale: any, tweenedOptions?: Parameters<typeof tweened>[1]): {
40
+ export declare function tweenedScale<Domain, Range>(scale: any, tweenedOptions?: Parameters<typeof tweened<any>>[1]): {
32
41
  subscribe: (this: void, run: import("svelte/store").Subscriber<any>, invalidate?: import("svelte/store").Invalidator<any> | undefined) => import("svelte/store").Unsubscriber;
33
- domain: (values: any) => Promise<void>;
34
- range: (values: any) => Promise<void>;
42
+ domain: (values: Domain) => Promise<void>;
43
+ range: (values: Range) => Promise<void>;
35
44
  };
36
45
  /**
37
46
  * Animate d3-scale as domain and/or range are updated using spring store
38
47
  */
39
- export declare function springScale(scale: any, springOptions?: Parameters<typeof spring>[1]): {
48
+ export declare function springScale<Domain, Range>(scale: AnyScale, springOptions?: Parameters<typeof spring>[1]): {
40
49
  subscribe: (this: void, run: import("svelte/store").Subscriber<any>, invalidate?: import("svelte/store").Invalidator<any> | undefined) => import("svelte/store").Unsubscriber;
41
- domain: (values: any) => Promise<void>;
42
- range: (values: any) => Promise<void>;
50
+ domain: (values: Domain) => Promise<void>;
51
+ range: (values: Range) => Promise<void>;
43
52
  };
44
53
  /**
45
54
  * Create a store wrapper around a d3-scale which interpolates the domain and/or range using `tweened()` or `spring()` stores. Fallbacks to `writable()` store if not interpolating
46
55
  */
47
- export declare function motionScale(scale: any, options: MotionOptions): {
56
+ export declare function motionScale<Domain, Range>(scale: AnyScale, options: MotionOptions): {
48
57
  subscribe: (this: void, run: import("svelte/store").Subscriber<any>, invalidate?: import("svelte/store").Invalidator<any> | undefined) => import("svelte/store").Unsubscriber;
49
- domain: (values: any) => void | Promise<void>;
50
- range: (values: any) => void | Promise<void>;
58
+ domain: (values: Domain) => void | Promise<void>;
59
+ range: (values: Range) => void | Promise<void>;
51
60
  };
@@ -35,7 +35,7 @@ export function scaleInvert(scale, value) {
35
35
  return scaleBandInvert(scale)(value);
36
36
  }
37
37
  else {
38
- return scale.invert(value);
38
+ return scale.invert?.(value);
39
39
  }
40
40
  }
41
41
  /** Create a `scaleBand()` within another scaleBand()'s bandwidth (typically a x1 of an x0 scale, used for grouping) */
@@ -82,6 +82,7 @@ export function springScale(scale, springOptions = {}) {
82
82
  const domainStore = spring(undefined, springOptions);
83
83
  const rangeStore = spring(undefined, springOptions);
84
84
  const tweenedScale = derived([domainStore, rangeStore], ([domain, range]) => {
85
+ // @ts-expect-error
85
86
  const scaleInstance = scale.domain ? scale : scale(); // support `scaleLinear` or `scaleLinear()` (which could have `.interpolate()` and others set)
86
87
  if (domain) {
87
88
  scaleInstance.domain(domain);
@@ -104,6 +105,7 @@ export function motionScale(scale, options) {
104
105
  const domainStore = motionStore(undefined, options);
105
106
  const rangeStore = motionStore(undefined, options);
106
107
  const tweenedScale = derived([domainStore, rangeStore], ([domain, range]) => {
108
+ // @ts-expect-error
107
109
  const scaleInstance = scale.domain ? scale : scale(); // support `scaleLinear` or `scaleLinear()` (which could have `.interpolate()` and others set)
108
110
  if (domain) {
109
111
  scaleInstance.domain(domain);
@@ -1,13 +1,16 @@
1
1
  import { stackOffsetNone, stackOrderNone } from 'd3-shape';
2
2
  type OrderType = typeof stackOrderNone;
3
3
  type OffsetType = typeof stackOffsetNone;
4
- export declare function createStackData(data: any[], options: {
4
+ export declare function createStackData<TData>(data: TData[], options: {
5
5
  xKey: string;
6
6
  groupBy?: string;
7
7
  stackBy?: string;
8
8
  order?: OrderType;
9
9
  offset?: OffsetType;
10
- }): any[];
10
+ }): {
11
+ keys: (string | number)[];
12
+ values: number[];
13
+ }[];
11
14
  /**
12
15
  * Function to offset each layer by the maximum of the previous layer
13
16
  * - see: https://observablehq.com/@mkfreeman/separated-bar-chart
@@ -4,16 +4,21 @@ import { pivotWider } from './pivot.js';
4
4
  export function createStackData(data, options) {
5
5
  if (options.groupBy) {
6
6
  // Group then Stack (if needed)
7
- const groupedData = flatGroup(data, (d) => d[options.xKey], (d) => d[options.groupBy]);
7
+ const groupedData = flatGroup(data,
8
+ // @ts-expect-error
9
+ (d) => d[options.xKey],
10
+ // @ts-expect-error
11
+ (d) => d[options.groupBy ?? '']);
8
12
  const result = groupedData.flatMap((d, i) => {
9
13
  const groupKeys = d.slice(0, -1); // all but last item
10
14
  const itemData = d.slice(-1)[0]; // last item
11
- const pivotData = pivotWider(itemData, options.xKey, options.stackBy, 'value');
12
- const stackKeys = [...new Set(itemData.map((d) => d[options.stackBy]))];
15
+ const pivotData = pivotWider(itemData, options.xKey, options.stackBy ?? '', 'value');
16
+ const stackKeys = [
17
+ ...new Set(itemData.map((d) => d[options.stackBy ?? ''])),
18
+ ];
19
+ // @ts-expect-error
13
20
  const stackData = stack().keys(stackKeys).order(options.order).offset(options.offset)(pivotData);
14
- //console.log({ pivotData, stackData })
15
21
  return stackData.flatMap((series) => {
16
- //console.log({ series })
17
22
  return series.flatMap((s) => {
18
23
  return {
19
24
  ...itemData[0], // TODO: More than one should use stacks or aggregate values?
@@ -28,7 +33,9 @@ export function createStackData(data, options) {
28
33
  else if (options.stackBy) {
29
34
  // Stack only
30
35
  const pivotData = pivotWider(data, options.xKey, options.stackBy, 'value');
31
- const stackKeys = [...new Set(data.map((d) => d[options.stackBy]))];
36
+ // @ts-expect-error
37
+ const stackKeys = [...new Set(data.map((d) => d[options.stackBy ?? '']))];
38
+ // @ts-expect-error
32
39
  const stackData = stack().keys(stackKeys).order(options.order).offset(options.offset)(pivotData);
33
40
  const result = stackData.flatMap((series) => {
34
41
  return series.flatMap((s) => {
@@ -45,10 +52,14 @@ export function createStackData(data, options) {
45
52
  // No grouping or stacking. Aggregate based on `xKey`
46
53
  return Array.from(rollup(data, (items) => {
47
54
  return {
55
+ // @ts-expect-error
48
56
  keys: [items[0][options.xKey]],
57
+ // @ts-expect-error
49
58
  values: [0, sum(items, (d) => d.value)],
50
59
  };
51
- }, (d) => d[options.xKey]).values());
60
+ },
61
+ // @ts-expect-error
62
+ (d) => d[options.xKey]).values());
52
63
  }
53
64
  }
54
65
  /**
@@ -56,6 +67,7 @@ export function createStackData(data, options) {
56
67
  * - see: https://observablehq.com/@mkfreeman/separated-bar-chart
57
68
  */
58
69
  // TODO: Try to find way to support separated with createStackData() (which has isolated stacked per group)
70
+ // @ts-expect-error
59
71
  export function stackOffsetSeparated(series, order) {
60
72
  const gap = 200; // TODO: Determine way to pass in as option (curry?)
61
73
  if (!((n = series.length) > 1))
@@ -63,6 +75,7 @@ export function stackOffsetSeparated(series, order) {
63
75
  // Standard series
64
76
  for (var i = 1, s0, s1 = series[order[0]], n, m = s1.length; i < n; ++i) {
65
77
  (s0 = s1), (s1 = series[order[i]]);
78
+ // @ts-expect-error
66
79
  let base = max(s0, (d) => d[1]) + gap; // here is where you calculate the maximum of the previous layer
67
80
  for (var j = 0; j < m; ++j) {
68
81
  // Set the height based on the data values, shifted up by the previous layer
@@ -6,6 +6,7 @@ import { scaleTime } from 'd3-scale';
6
6
  */
7
7
  export function thresholdTime(n) {
8
8
  // TODO: Unable to satisfy `ThresholdNumberArrayGenerator<Value extends number>` with `Date`
9
+ // @ts-expect-error
9
10
  return (data, min, max) => {
10
11
  return scaleTime().domain([min, max]).ticks(n);
11
12
  };
@@ -1,10 +1,13 @@
1
+ import type { HierarchyNode, HierarchyRectangularNode } from 'd3-hierarchy';
2
+ type TileFunc = (node: HierarchyRectangularNode<any>, x0: number, y0: number, x1: number, y1: number) => void;
1
3
  /**
2
4
  * This custom tiling function adapts the tiling function
3
5
  * for the appropriate aspect ratio when the treemap is zoomed-in.
4
6
  * see: https://observablehq.com/@d3/zoomable-treemap#tile and https://observablehq.com/@d3/stretched-treemap
5
7
  */
6
- export declare function aspectTile(tile: any, width: any, height: any): (node: any, x0: any, y0: any, x1: any, y1: any) => void;
8
+ export declare function aspectTile(tile: TileFunc, width: number, height: number): TileFunc;
7
9
  /**
8
10
  * Show if the node (a) is a child of the selected (b), or any parent above selected
9
11
  */
10
- export declare function isNodeVisible(a: any, b: any): boolean;
12
+ export declare function isNodeVisible(a: HierarchyNode<any>, b: HierarchyNode<any> | null): boolean;
13
+ export {};
@@ -6,7 +6,7 @@
6
6
  export function aspectTile(tile, width, height) {
7
7
  return (node, x0, y0, x1, y1) => {
8
8
  tile(node, 0, 0, width, height);
9
- for (const child of node.children) {
9
+ for (const child of node.children ?? []) {
10
10
  child.x0 = x0 + (child.x0 / width) * (x1 - x0);
11
11
  child.x1 = x0 + (child.x1 / width) * (x1 - x0);
12
12
  child.y0 = y0 + (child.y0 / height) * (y1 - y0);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "author": "Sean Lynch <techniq35@gmail.com>",
5
5
  "license": "MIT",
6
6
  "repository": "techniq/layerchart",
7
- "version": "0.42.0",
7
+ "version": "0.43.0",
8
8
  "devDependencies": {
9
9
  "@changesets/cli": "^2.27.5",
10
10
  "@mdi/js": "^7.4.47",
@@ -16,6 +16,7 @@
16
16
  "@svitejs/changesets-changelog-github-compact": "^1.1.0",
17
17
  "@tailwindcss/typography": "^0.5.13",
18
18
  "@types/d3-array": "^3.2.1",
19
+ "@types/d3-color": "^3.1.3",
19
20
  "@types/d3-delaunay": "^6.0.4",
20
21
  "@types/d3-dsv": "^3.0.7",
21
22
  "@types/d3-force": "^3.0.10",
@@ -23,6 +24,7 @@
23
24
  "@types/d3-hierarchy": "^3.1.7",
24
25
  "@types/d3-interpolate": "^3.0.4",
25
26
  "@types/d3-interpolate-path": "^2.0.3",
27
+ "@types/d3-path": "^3.1.0",
26
28
  "@types/d3-quadtree": "^3.0.6",
27
29
  "@types/d3-random": "^3.0.3",
28
30
  "@types/d3-sankey": "^0.12.4",
@@ -35,6 +37,7 @@
35
37
  "@types/shapefile": "^0.6.4",
36
38
  "@types/topojson-client": "^3.1.4",
37
39
  "@types/topojson-simplify": "^3.0.3",
40
+ "@types/topojson-specification": "^1.0.5",
38
41
  "autoprefixer": "^10.4.19",
39
42
  "marked": "^13.0.0",
40
43
  "mdsvex": "^0.11.2",
@@ -87,7 +90,7 @@
87
90
  "lodash-es": "^4.17.21",
88
91
  "posthog-js": "^1.140.1",
89
92
  "shapefile": "^0.6.6",
90
- "svelte-ux": "^0.71.0",
93
+ "svelte-ux": "^0.71.1",
91
94
  "topojson-client": "^3.1.0"
92
95
  },
93
96
  "peerDependencies": {