UNPKG

3.98 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.StackY = void 0;
4const util_1 = require("@antv/util");
5const helper_1 = require("./utils/helper");
6const order_1 = require("./utils/order");
7/**
8 * The stack transform group marks into series by color channel,
9 * and then produce new y channel for each series by specified order,
10 * say to form vertical "stacks" by specified channels.
11 */
12const StackY = (options = {}) => {
13 const { groupBy = 'x', orderBy = null, reverse = false, y: fromY = 'y', y1: fromY1 = 'y1', series = true, } = options;
14 return (I, mark) => {
15 var _a;
16 const { data, encode, style = {} } = mark;
17 const [Y, fy] = (0, helper_1.columnOf)(encode, 'y');
18 const [Y1, fy1] = (0, helper_1.columnOf)(encode, 'y1');
19 const [S] = series
20 ? (0, helper_1.maybeColumnOf)(encode, 'series', 'color')
21 : (0, helper_1.columnOf)(encode, 'color');
22 // Create groups and apply specified order for each group.
23 const groups = (0, order_1.createGroups)(groupBy, I, mark);
24 const createComparator = (_a = (0, order_1.normalizeComparator)(orderBy)) !== null && _a !== void 0 ? _a : (() => null);
25 const comparator = createComparator(data, Y, S);
26 if (comparator)
27 (0, order_1.applyOrder)(groups, comparator);
28 // Stack y channels to produce new y and y1 channel.
29 const newY = new Array(I.length);
30 const newY1 = new Array(I.length);
31 const TY = new Array(I.length);
32 const F = [];
33 const L = [];
34 for (const G of groups) {
35 if (reverse)
36 G.reverse();
37 // For range interval with specified y and y1.
38 const start = Y1 ? +Y1[G[0]] : 0;
39 // Split positive indices of Y and negative Y.
40 const PG = [];
41 const NG = [];
42 for (const i of G) {
43 const y = (TY[i] = +Y[i] - start);
44 if (y < 0)
45 NG.push(i);
46 else if (y >= 0)
47 PG.push(i);
48 }
49 // Store the first and last layer.
50 const FG = PG.length > 0 ? PG : NG;
51 const LG = NG.length > 0 ? NG : PG;
52 let i = PG.length - 1;
53 let j = 0;
54 // Find the last non-zero index.
55 while (i > 0 && Y[FG[i]] === 0)
56 i--;
57 // Find the first non-zero index.
58 while (j < LG.length - 1 && Y[LG[j]] === 0)
59 j++;
60 F.push(FG[i]);
61 L.push(LG[j]);
62 // Stack negative y in reverse order.
63 let ny = start;
64 for (const i of NG.reverse()) {
65 const y = TY[i];
66 ny = newY[i] = (newY1[i] = ny) + y;
67 }
68 // Stack positive y in input order.
69 let py = start;
70 for (const i of PG) {
71 const y = TY[i];
72 if (y > 0)
73 py = newY[i] = (newY1[i] = py) + y;
74 else
75 newY[i] = newY1[i] = py;
76 }
77 }
78 // Only set top radius for the first layer,
79 // and set bottom radius for the last layer.
80 const FS = new Set(F);
81 const LS = new Set(L);
82 // Choose new y or y1 channel as the new y channel.
83 const V = fromY === 'y' ? newY : newY1;
84 const V1 = fromY1 === 'y' ? newY : newY1;
85 return [
86 I,
87 (0, util_1.deepMix)({}, mark, {
88 encode: {
89 y0: (0, helper_1.inferredColumn)(Y, fy),
90 y: (0, helper_1.column)(V, fy),
91 y1: (0, helper_1.column)(V1, fy1),
92 },
93 style: Object.assign({ first: (_, i) => FS.has(i), last: (_, i) => LS.has(i) }, style),
94 }),
95 ];
96 };
97};
98exports.StackY = StackY;
99exports.StackY.props = {};
100//# sourceMappingURL=stackY.js.map
\No newline at end of file