svelteplot 0.3.11-pr-175.1 → 0.3.11-pr-153.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/FacetGrid.svelte +3 -0
- package/dist/helpers/group.d.ts +1 -1
- package/dist/marks/HTMLTooltip.svelte +38 -12
- package/dist/marks/HTMLTooltip.svelte.d.ts +2 -0
- package/dist/transforms/index.d.ts +1 -1
- package/dist/transforms/index.js +1 -1
- package/dist/transforms/stack.d.ts +26 -1
- package/dist/transforms/stack.js +37 -2
- package/package.json +1 -1
|
@@ -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
|
package/dist/helpers/group.d.ts
CHANGED
|
@@ -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:
|
|
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
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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:
|
|
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;
|
|
@@ -12,5 +12,5 @@ export { renameChannels, replaceChannels } from './rename.js';
|
|
|
12
12
|
export { select, selectFirst, selectLast, selectMaxX, selectMaxY, selectMinX, selectMinY } from './select.js';
|
|
13
13
|
export { shiftX, shiftY } from './shift.js';
|
|
14
14
|
export { sort, shuffle, reverse } from './sort.js';
|
|
15
|
-
export { stackX, stackY } from './stack.js';
|
|
15
|
+
export { stackX, stackY, stackMarimekko } from './stack.js';
|
|
16
16
|
export { windowX, windowY } from './window.js';
|
package/dist/transforms/index.js
CHANGED
|
@@ -12,5 +12,5 @@ export { renameChannels, replaceChannels } from './rename.js';
|
|
|
12
12
|
export { select, selectFirst, selectLast, selectMaxX, selectMaxY, selectMinX, selectMinY } from './select.js';
|
|
13
13
|
export { shiftX, shiftY } from './shift.js';
|
|
14
14
|
export { sort, shuffle, reverse } from './sort.js';
|
|
15
|
-
export { stackX, stackY } from './stack.js';
|
|
15
|
+
export { stackX, stackY, stackMarimekko } from './stack.js';
|
|
16
16
|
export { windowX, windowY } from './window.js';
|
|
@@ -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,3 +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<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[];
|
|
27
|
+
x: symbol;
|
|
28
|
+
x1: symbol;
|
|
29
|
+
x2: symbol;
|
|
30
|
+
y: symbol;
|
|
31
|
+
y1: symbol;
|
|
32
|
+
y2: symbol;
|
|
33
|
+
fx?: ChannelAccessor<T>;
|
|
34
|
+
fy?: ChannelAccessor<T>;
|
|
35
|
+
};
|
package/dist/transforms/stack.js
CHANGED
|
@@ -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
|
-
import { index, union, groups as d3Groups } from 'd3-array';
|
|
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 = {
|
|
@@ -110,3 +111,37 @@ function applyDefaults(opts) {
|
|
|
110
111
|
}
|
|
111
112
|
return { ...DEFAULT_STACK_OPTIONS, ...opts };
|
|
112
113
|
}
|
|
114
|
+
const X = Symbol('x'), X1 = Symbol('x1'), X2 = Symbol('x2');
|
|
115
|
+
const Y = Symbol('y'), Y1 = Symbol('y1'), Y2 = Symbol('y2');
|
|
116
|
+
export function stackMarimekko({ data, x, y, value, ...rest }, { x: xOpt, y: yOpt } = {}) {
|
|
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
|
+
});
|
|
143
|
+
});
|
|
144
|
+
out.push(...grouped);
|
|
145
|
+
});
|
|
146
|
+
return { ...rest, data: out, x: X, x1: X1, x2: X2, y: Y, y1: Y1, y2: Y2 };
|
|
147
|
+
}
|