svelteplot 0.3.11-pr-153.11 → 0.3.11-pr-153.12

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.
@@ -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, stackMosaic } from './stack.js';
15
+ export { stackX, stackY, stackMosaicX, stackMosaicY } from './stack.js';
16
16
  export { windowX, windowY } from './window.js';
@@ -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, stackMosaic } from './stack.js';
15
+ export { stackX, stackY, stackMosaicX, stackMosaicY } from './stack.js';
16
16
  export { windowX, windowY } from './window.js';
@@ -1,4 +1,4 @@
1
- import type { ChannelAccessor, TransformArg } from '../types/index.js';
1
+ import type { 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,21 @@ 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 stackMosaic<T>(args: {
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
- }): any;
11
+ export declare function stackMosaicX<T>(args: any, opts: any): {
12
+ data: unknown[];
13
+ x: symbol;
14
+ x1: symbol;
15
+ x2: symbol;
16
+ y: symbol;
17
+ y1: symbol;
18
+ y2: symbol;
19
+ };
20
+ export declare function stackMosaicY<T>(args: any, opts: any): {
21
+ data: unknown[];
22
+ x: symbol;
23
+ x1: symbol;
24
+ x2: symbol;
25
+ y: symbol;
26
+ y1: symbol;
27
+ y2: symbol;
28
+ };
@@ -119,49 +119,63 @@ const X2 = Symbol('x2');
119
119
  const Y = Symbol('y');
120
120
  const Y1 = Symbol('y1');
121
121
  const Y2 = Symbol('y2');
122
- export function stackMosaic(args, { x: xOpt, y: yOpt } = {}) {
122
+ function stackMosaic({ data, x, y, value, fx, fy, ...rest }, { outer, inner }, { x: xOpt, y: yOpt } = {}) {
123
123
  const out = [];
124
- const { data, x, y, value, ...rest } = sort(filter(args));
125
- if (data == null)
124
+ const { data: filtered, ...restArgs } = sort(filter({ data, x, y, value, fx, fy, ...rest }));
125
+ if (!filtered)
126
126
  throw new Error('stackMosaic: missing data');
127
- if (x == null)
127
+ if (!x)
128
128
  throw new Error('stackMosaic: missing x channel');
129
- if (y == null)
129
+ if (!y)
130
130
  throw new Error('stackMosaic: missing y channel');
131
- if (value == null)
131
+ if (!value)
132
132
  throw new Error('stackMosaic: missing value channel');
133
- if (min(data, (d) => resolveProp(d[value], d)) < 0)
133
+ if (min(filtered, (d) => resolveProp(d[value], d)) < 0)
134
134
  throw new Error('stackMosaic: negative values not supported');
135
- groupFacetsAndZ(data, { ...rest }, (data) => {
136
- const total = sum(data, (d) => d[value]);
137
- let xPos = 0;
138
- const grouped = d3Groups(data, (d) => resolveProp(d[x], d));
139
- const yOrder = new Map(grouped[0][1].map((d, i) => [d[y], i]));
140
- const flat = grouped.flatMap(([k, items], i) => {
135
+ groupFacetsAndZ(filtered, restArgs, (data) => {
136
+ const total = sum(data, (d) => resolveProp(d[value], d));
137
+ let outerPos = 0;
138
+ const outerChannel = outer === 'x' ? x : y;
139
+ const innerChannel = inner === 'x' ? x : y;
140
+ const outerSym1 = outer === 'x' ? X1 : Y1;
141
+ const outerSym2 = outer === 'x' ? X2 : Y2;
142
+ const innerSym1 = inner === 'x' ? X1 : Y1;
143
+ const innerSym2 = inner === 'x' ? X2 : Y2;
144
+ const outerOpt = outer === 'x' ? xOpt : yOpt;
145
+ const innerOpt = inner === 'x' ? xOpt : yOpt;
146
+ const grouped = d3Groups(data, (d) => resolveProp(d[outerChannel], d));
147
+ const innerOrder = new Map(grouped[0][1].map((d, i) => [d[innerChannel], i]));
148
+ grouped.forEach(([k, items], i) => {
141
149
  const groupValue = sum(items, (d) => resolveProp(d[value], d));
142
- const x1 = xPos;
143
- const x2 = xPos + groupValue;
144
- xPos = x2;
145
- let yPos = 0;
146
- return (!i ? items : items.sort((a, b) => yOrder.get(a[y]) - yOrder.get(b[y]))).map((d) => {
147
- const y1 = yPos, y2 = yPos + resolveProp(d[value], d);
148
- yPos = y2;
149
- const normX1 = xOpt?.percent ? x1 / total : x1;
150
- const normX2 = xOpt?.percent ? x2 / total : x2;
151
- const normY1 = yOpt?.percent ? y1 / groupValue : y1;
152
- const normY2 = yOpt?.percent ? y2 / groupValue : y2;
153
- return {
154
- ...d,
155
- [X1]: normX1,
156
- [X2]: normX2,
157
- [Y1]: normY1,
158
- [Y2]: normY2,
159
- [X]: (normX1 + normX2) / 2,
160
- [Y]: (normY1 + normY2) / 2
161
- };
150
+ const o1 = outerPos, o2 = outerPos + groupValue;
151
+ outerPos = o2;
152
+ let innerPos = 0;
153
+ (i
154
+ ? items.sort((a, b) => innerOrder.get(a[innerChannel]) - innerOrder.get(b[innerChannel]))
155
+ : items).forEach((d) => {
156
+ const iv = resolveProp(d[value], d);
157
+ const i1 = innerPos, i2 = innerPos + iv;
158
+ innerPos = i2;
159
+ const normO1 = outerOpt?.percent ? o1 / total : o1;
160
+ const normO2 = outerOpt?.percent ? o2 / total : o2;
161
+ const normI1 = innerOpt?.percent ? i1 / groupValue : i1;
162
+ const normI2 = innerOpt?.percent ? i2 / groupValue : i2;
163
+ const result = { ...d };
164
+ result[outerSym1] = normO1;
165
+ result[outerSym2] = normO2;
166
+ result[innerSym1] = normI1;
167
+ result[innerSym2] = normI2;
168
+ result[X] = (result[X1] + result[X2]) / 2;
169
+ result[Y] = (result[Y1] + result[Y2]) / 2;
170
+ out.push(result);
162
171
  });
163
172
  });
164
- out.push(...flat);
165
173
  });
166
174
  return { ...rest, data: out, x: X, x1: X1, x2: X2, y: Y, y1: Y1, y2: Y2 };
167
175
  }
176
+ export function stackMosaicX(args, opts) {
177
+ return stackMosaic(args, { outer: 'x', inner: 'y' }, opts);
178
+ }
179
+ export function stackMosaicY(args, opts) {
180
+ return stackMosaic(args, { outer: 'y', inner: 'x' }, opts);
181
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelteplot",
3
- "version": "0.3.11-pr-153.11",
3
+ "version": "0.3.11-pr-153.12",
4
4
  "license": "ISC",
5
5
  "author": {
6
6
  "name": "Gregor Aisch",