svelteplot 0.3.11-pr-153.0 → 0.3.11-pr-153.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.
@@ -59,6 +59,9 @@
59
59
  {#each fyValues as facetY, j (j)}
60
60
  <g
61
61
  class="facet"
62
+ data-facet-x={i}
63
+ data-facet-y={j}
64
+ data-facet={i * fyValues.length + j}
62
65
  fill="currentColor"
63
66
  style:display={emptyFacets.get(facetX)?.get(facetY) ? 'none' : 'block'}
64
67
  transform="translate({useFacetX ? facetXScale(facetX) : 0}, {useFacetY
@@ -3,4 +3,4 @@ import type { Channels, DataRecord } from '../types/index.js';
3
3
  * Groups the data by the fx, fy and z channels and calls the reduce function
4
4
  * for each group. Returns the new channels to be added in the transform.
5
5
  */
6
- export declare function groupFacetsAndZ(items: DataRecord[], channels: Channels, reduce: (items: DataRecord[]) => any): any;
6
+ export declare function groupFacetsAndZ<T>(items: T[], channels: Channels, reduce: (items: DataRecord[]) => any): any;
@@ -8,6 +8,8 @@
8
8
  x?: ChannelAccessor<Datum>;
9
9
  y?: ChannelAccessor<Datum>;
10
10
  r?: ChannelAccessor<Datum>;
11
+ fx?: ChannelAccessor<Datum>;
12
+ fy?: ChannelAccessor<Datum>;
11
13
  children: Snippet<[{ datum: Datum }]>;
12
14
  }
13
15
  import { getContext, type Snippet } from 'svelte';
@@ -19,18 +21,33 @@
19
21
  import { resolveChannel } from '../helpers/resolve.js';
20
22
  import { quadtree } from 'd3-quadtree';
21
23
  import { projectX, projectY } from '../helpers/scales.js';
24
+ import { groupFacetsAndZ } from '../helpers/group.js';
22
25
 
23
- let { data, x, y, r, children }: HTMLTooltipMarkProps = $props();
26
+ let { data, x, y, r, fx, fy, children }: HTMLTooltipMarkProps = $props();
24
27
 
25
28
  let datum = $state(false);
26
29
  let tooltipX = $state();
27
30
  let tooltipY = $state();
28
31
 
32
+ let facetOffsetX = $state(0);
33
+ let facetOffsetY = $state(0);
34
+
29
35
  function onPointerMove(evt: MouseEvent) {
30
36
  const plotRect = plot.body.getBoundingClientRect();
31
- let relativeX = evt.clientX - plotRect.left;
32
- let relativeY = evt.clientY - plotRect.top;
33
- const pt = tree.find(relativeX, relativeY, 25);
37
+ let facetEl = evt.target as SVGElement;
38
+ while (facetEl && !facetEl.classList.contains('facet')) {
39
+ facetEl = facetEl.parentElement;
40
+ }
41
+ const facetIndex = +(facetEl.dataset?.facet ?? 0);
42
+ const facetRect = (facetEl?.firstChild ?? plot.body).getBoundingClientRect();
43
+
44
+ facetOffsetX = facetRect.left - plotRect.left - plot.options.marginLeft;
45
+ facetOffsetY = facetRect.top - plotRect.top - plot.options.marginTop;
46
+
47
+ const relativeX = evt.clientX - facetRect.left + (plot.options.marginLeft ?? 0);
48
+ const relativeY = evt.clientY - facetRect.top + (plot.options.marginTop ?? 0);
49
+
50
+ const pt = trees[facetIndex].find(relativeX, relativeY, 25);
34
51
  if (pt) {
35
52
  tooltipX = resolveChannel('x', pt, { x, y, r });
36
53
  tooltipY = resolveChannel('y', pt, { x, y, r });
@@ -54,18 +71,26 @@
54
71
  };
55
72
  });
56
73
 
57
- let tree = $derived(
58
- quadtree()
59
- .x((d) => projectX('x', plot.scales, resolveChannel('x', d, { x, y, r })))
60
- .y((d) => projectY('y', plot.scales, resolveChannel('y', d, { x, y, r })))
61
- .addAll(data)
74
+ const groups = $derived.by(() => {
75
+ const groups: Datum[][] = [];
76
+ groupFacetsAndZ(data, { fx, fy }, (d) => groups.push(d));
77
+ return groups;
78
+ });
79
+
80
+ const trees = $derived(
81
+ groups.map((items) =>
82
+ quadtree()
83
+ .x((d) => projectX('x', plot.scales, resolveChannel('x', d, { x, y, r })))
84
+ .y((d) => projectY('y', plot.scales, resolveChannel('y', d, { x, y, r })))
85
+ .addAll(items)
86
+ )
62
87
  );
63
88
  </script>
64
89
 
65
90
  <div
66
91
  class={['tooltip', { hide: !datum }]}
67
- style:left="{tooltipX ? projectX('x', plot.scales, tooltipX) : 0}px"
68
- style:top="{tooltipY ? projectY('y', plot.scales, tooltipY) : 0}px">
92
+ style:left="{tooltipX ? facetOffsetX + projectX('x', plot.scales, tooltipX) : 0}px"
93
+ style:top="{tooltipY ? facetOffsetY + projectY('y', plot.scales, tooltipY) : 0}px">
69
94
  <div class="tooltip-body">
70
95
  {@render children({ datum })}
71
96
  </div>
@@ -77,9 +102,10 @@
77
102
  background: var(--svelteplot-tooltip-bg);
78
103
  border: 1px solid #ccc;
79
104
  border-color: var(--svelteplot-tooltip-border);
80
- font-size: 13px;
105
+ font-size: 12px;
81
106
  padding: 1ex 1em;
82
107
  border-radius: 3px;
108
+ line-height: 1.2;
83
109
  box-shadow:
84
110
  rgba(50, 50, 93, 0.25) 0px 2px 5px -1px,
85
111
  rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
@@ -6,6 +6,8 @@ declare class __sveltets_Render<Datum = DataRow> {
6
6
  x?: ChannelAccessor<Datum>;
7
7
  y?: ChannelAccessor<Datum>;
8
8
  r?: ChannelAccessor<Datum>;
9
+ fx?: ChannelAccessor<Datum>;
10
+ fy?: ChannelAccessor<Datum>;
9
11
  children: Snippet<[{
10
12
  datum: Datum;
11
13
  }]>;
@@ -1,4 +1,4 @@
1
- import type { TransformArg } from '../types/index.js';
1
+ import type { ChannelAccessor, TransformArg } from '../types/index.js';
2
2
  export type StackOrder = 'none' | 'appearance' | 'inside-out' | 'sum';
3
3
  export type StackOffset = 'none' | 'wiggle' | 'center' | 'normalize' | 'diverging';
4
4
  export type StackOptions = {
@@ -8,18 +8,28 @@ export type StackOptions = {
8
8
  };
9
9
  export declare function stackY<T>({ data, ...channels }: T, opts?: Partial<StackOptions>): T;
10
10
  export declare function stackX({ data, ...channels }: TransformArg, opts?: Partial<StackOptions>): TransformArg;
11
- export declare function stackMarimekko({ data, x, y, value, ...rest }: {
12
- [x: string]: any;
13
- data: any;
14
- x: any;
15
- y: any;
16
- value: any;
17
- }, { x: xOpt, y: yOpt }?: {}): {
18
- data: any[];
11
+ export declare function stackMarimekko<T>({ data, x, y, value, ...rest }: {
12
+ data: T[];
13
+ x: ChannelAccessor<T>;
14
+ y: ChannelAccessor<T>;
15
+ value: ChannelAccessor<T>;
16
+ fx?: ChannelAccessor<T>;
17
+ fy?: ChannelAccessor<T>;
18
+ }, { x: xOpt, y: yOpt }?: {
19
+ x?: {
20
+ percent?: boolean;
21
+ };
22
+ y?: {
23
+ percent?: boolean;
24
+ };
25
+ }): {
26
+ data: T[];
19
27
  x: symbol;
20
28
  x1: symbol;
21
29
  x2: symbol;
22
30
  y: symbol;
23
31
  y1: symbol;
24
32
  y2: symbol;
33
+ fx?: ChannelAccessor<T>;
34
+ fy?: ChannelAccessor<T>;
25
35
  };
@@ -1,7 +1,8 @@
1
1
  import isDataRecord from '../helpers/isDataRecord.js';
2
- import { resolveChannel } from '../helpers/resolve.js';
2
+ import { resolveChannel, resolveProp } from '../helpers/resolve.js';
3
3
  import { stack, stackOffsetExpand, stackOffsetSilhouette, stackOffsetWiggle, stackOrderAppearance, stackOrderAscending, stackOrderInsideOut, stackOrderNone, stackOffsetDiverging } from 'd3-shape';
4
4
  import { index, union, sum, groups as d3Groups } from 'd3-array';
5
+ import { groupFacetsAndZ } from '../helpers/group';
5
6
  const GROUP = Symbol('group');
6
7
  const FACET = Symbol('group');
7
8
  const DEFAULT_STACK_OPTIONS = {
@@ -113,30 +114,34 @@ function applyDefaults(opts) {
113
114
  const X = Symbol('x'), X1 = Symbol('x1'), X2 = Symbol('x2');
114
115
  const Y = Symbol('y'), Y1 = Symbol('y1'), Y2 = Symbol('y2');
115
116
  export function stackMarimekko({ data, x, y, value, ...rest }, { x: xOpt, y: yOpt } = {}) {
116
- const total = sum(data, (d) => d[value]);
117
- let xPos = 0;
118
- const grouped = d3Groups(data, (d) => d[x]).flatMap(([k, items]) => {
119
- const groupValue = sum(items, (d) => d[value]);
120
- const x1 = xPos, x2 = xPos + groupValue;
121
- xPos = x2;
122
- let yPos = 0;
123
- return items.map((d) => {
124
- const y1 = yPos, y2 = yPos + d[value];
125
- yPos = y2;
126
- const normX1 = xOpt?.percent ? x1 / total : x1;
127
- const normX2 = xOpt?.percent ? x2 / total : x2;
128
- const normY1 = yOpt?.percent ? y1 / groupValue : y1;
129
- const normY2 = yOpt?.percent ? y2 / groupValue : y2;
130
- return {
131
- ...d,
132
- [X1]: normX1,
133
- [X2]: normX2,
134
- [Y1]: normY1,
135
- [Y2]: normY2,
136
- [X]: (normX1 + normX2) / 2,
137
- [Y]: (normY1 + normY2) / 2
138
- };
117
+ const out = [];
118
+ groupFacetsAndZ(data, { ...rest }, (data) => {
119
+ const total = sum(data, (d) => d[value]);
120
+ let xPos = 0;
121
+ const grouped = d3Groups(data, (d) => resolveProp(d[x], d)).flatMap(([k, items]) => {
122
+ const groupValue = sum(items, (d) => resolveProp(d[value], d));
123
+ const x1 = xPos, x2 = xPos + groupValue;
124
+ xPos = x2;
125
+ let yPos = 0;
126
+ return items.map((d) => {
127
+ const y1 = yPos, y2 = yPos + resolveProp(d[value], d);
128
+ yPos = y2;
129
+ const normX1 = xOpt?.percent ? x1 / total : x1;
130
+ const normX2 = xOpt?.percent ? x2 / total : x2;
131
+ const normY1 = yOpt?.percent ? y1 / groupValue : y1;
132
+ const normY2 = yOpt?.percent ? y2 / groupValue : y2;
133
+ return {
134
+ ...d,
135
+ [X1]: normX1,
136
+ [X2]: normX2,
137
+ [Y1]: normY1,
138
+ [Y2]: normY2,
139
+ [X]: (normX1 + normX2) / 2,
140
+ [Y]: (normY1 + normY2) / 2
141
+ };
142
+ });
139
143
  });
144
+ out.push(...grouped);
140
145
  });
141
- return { ...rest, data: grouped, x: X, x1: X1, x2: X2, y: Y, y1: Y1, y2: Y2 };
146
+ return { ...rest, data: out, x: X, x1: X1, x2: X2, y: Y, y1: Y1, y2: Y2 };
142
147
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelteplot",
3
- "version": "0.3.11-pr-153.0",
3
+ "version": "0.3.11-pr-153.2",
4
4
  "license": "ISC",
5
5
  "author": {
6
6
  "name": "Gregor Aisch",