svelteplot 0.3.11-pr-153.6 → 0.3.11-pr-153.8
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/transforms/stack.d.ts +3 -13
- package/dist/transforms/stack.js +21 -10
- package/package.json +1 -1
|
@@ -8,10 +8,10 @@ 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>(
|
|
11
|
+
export declare function stackMarimekko<T>(args: {
|
|
12
12
|
data: T[];
|
|
13
13
|
x: ChannelAccessor<T>;
|
|
14
|
-
y
|
|
14
|
+
y: ChannelAccessor<T>;
|
|
15
15
|
value: ChannelAccessor<T>;
|
|
16
16
|
fx?: ChannelAccessor<T>;
|
|
17
17
|
fy?: ChannelAccessor<T>;
|
|
@@ -22,14 +22,4 @@ export declare function stackMarimekko<T>({ data, x, y, value, ...rest }: {
|
|
|
22
22
|
y?: {
|
|
23
23
|
percent?: boolean;
|
|
24
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
|
-
};
|
|
25
|
+
}): any;
|
package/dist/transforms/stack.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import isDataRecord from '../helpers/isDataRecord.js';
|
|
2
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, sum, groups as d3Groups } from 'd3-array';
|
|
4
|
+
import { index, union, sum, groups as d3Groups, extent, min } from 'd3-array';
|
|
5
5
|
import { groupFacetsAndZ } from '../helpers/group';
|
|
6
6
|
import { filter } from './filter.js';
|
|
7
|
+
import { sort } from './sort.js';
|
|
7
8
|
const GROUP = Symbol('group');
|
|
8
9
|
const FACET = Symbol('group');
|
|
9
10
|
const DEFAULT_STACK_OPTIONS = {
|
|
@@ -114,21 +115,31 @@ function applyDefaults(opts) {
|
|
|
114
115
|
}
|
|
115
116
|
const X = Symbol('x'), X1 = Symbol('x1'), X2 = Symbol('x2');
|
|
116
117
|
const Y = Symbol('y'), Y1 = Symbol('y1'), Y2 = Symbol('y2');
|
|
117
|
-
export function stackMarimekko(
|
|
118
|
+
export function stackMarimekko(args, { x: xOpt, y: yOpt } = {}) {
|
|
118
119
|
const out = [];
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
120
|
+
const { data, x, y, value, ...rest } = sort(filter(args));
|
|
121
|
+
if (data == null)
|
|
122
|
+
throw new Error('stackMarimekko: missing data');
|
|
123
|
+
if (x == null)
|
|
124
|
+
throw new Error('stackMarimekko: missing x channel');
|
|
125
|
+
if (y == null)
|
|
126
|
+
throw new Error('stackMarimekko: missing y channel');
|
|
127
|
+
if (value == null)
|
|
128
|
+
throw new Error('stackMarimekko: missing value channel');
|
|
129
|
+
if (min(data, (d) => resolveProp(d[value], d)) < 0)
|
|
130
|
+
throw new Error('stackMarimekko: negative values not supported');
|
|
123
131
|
groupFacetsAndZ(data, { ...rest }, (data) => {
|
|
124
132
|
const total = sum(data, (d) => d[value]);
|
|
125
133
|
let xPos = 0;
|
|
126
|
-
const grouped = d3Groups(data, (d) => resolveProp(d[x], d))
|
|
134
|
+
const grouped = d3Groups(data, (d) => resolveProp(d[x], d));
|
|
135
|
+
const yOrder = new Map(grouped[0][1].map((d, i) => [d[y], i]));
|
|
136
|
+
const flat = grouped.flatMap(([k, items], i) => {
|
|
127
137
|
const groupValue = sum(items, (d) => resolveProp(d[value], d));
|
|
128
|
-
const x1 = xPos
|
|
138
|
+
const x1 = xPos;
|
|
139
|
+
const x2 = xPos + groupValue;
|
|
129
140
|
xPos = x2;
|
|
130
141
|
let yPos = 0;
|
|
131
|
-
return items.map((d) => {
|
|
142
|
+
return (!i ? items : items.sort((a, b) => yOrder.get(a[y]) - yOrder.get(b[y]))).map((d) => {
|
|
132
143
|
const y1 = yPos, y2 = yPos + resolveProp(d[value], d);
|
|
133
144
|
yPos = y2;
|
|
134
145
|
const normX1 = xOpt?.percent ? x1 / total : x1;
|
|
@@ -146,7 +157,7 @@ export function stackMarimekko({ data, x, y, value, ...rest }, { x: xOpt, y: yOp
|
|
|
146
157
|
};
|
|
147
158
|
});
|
|
148
159
|
});
|
|
149
|
-
out.push(...
|
|
160
|
+
out.push(...flat);
|
|
150
161
|
});
|
|
151
162
|
return { ...rest, data: out, x: X, x1: X1, x2: X2, y: Y, y1: Y1, y2: Y2 };
|
|
152
163
|
}
|