UNPKG

3.5 kBJavaScriptView Raw
1"use strict";
2var __rest = (this && this.__rest) || function (s, e) {
3 var t = {};
4 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5 t[p] = s[p];
6 if (s != null && typeof Object.getOwnPropertySymbols === "function")
7 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9 t[p[i]] = s[p[i]];
10 }
11 return t;
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14exports.NormalizeY = void 0;
15const util_1 = require("@antv/util");
16const d3_array_1 = require("d3-array");
17const helper_1 = require("../utils/helper");
18const helper_2 = require("./utils/helper");
19const order_1 = require("./utils/order");
20function normalizeBasis(basis) {
21 if (typeof basis === 'function')
22 return basis;
23 const registry = {
24 min: (I, Y) => (0, d3_array_1.min)(I, (i) => Y[+i]),
25 max: (I, Y) => (0, d3_array_1.max)(I, (i) => Y[+i]),
26 first: (I, Y) => Y[I[0]],
27 last: (I, Y) => Y[I[I.length - 1]],
28 mean: (I, Y) => (0, d3_array_1.mean)(I, (i) => Y[+i]),
29 median: (I, Y) => (0, d3_array_1.median)(I, (i) => Y[+i]),
30 sum: (I, Y) => (0, d3_array_1.sum)(I, (i) => Y[+i]),
31 deviation: (I, Y) => (0, d3_array_1.deviation)(I, (i) => Y[+i]),
32 };
33 return registry[basis] || d3_array_1.max;
34}
35/**
36 * Group marks into series by specified channels, and then transform
37 * each series's value, say to transform them relative to some basis
38 * to apply a moving average.
39 */
40const NormalizeY = (options = {}) => {
41 const { groupBy = 'x', basis = 'max' } = options;
42 return (I, mark) => {
43 const { encode, tooltip } = mark;
44 const { x } = encode, rest = __rest(encode, ["x"]);
45 // Extract and create new channels starts with y, such as y, y1.
46 const Yn = Object.entries(rest)
47 .filter(([k]) => k.startsWith('y'))
48 .map(([k]) => [k, (0, helper_2.columnOf)(encode, k)[0]]);
49 const [, Y] = Yn.find(([k]) => k === 'y');
50 const newYn = Yn.map(([k]) => [k, new Array(I.length)]);
51 // Group marks into series by specified keys.
52 const groups = (0, order_1.createGroups)(groupBy, I, mark);
53 // Transform y channels for each group based on basis.
54 const basisFunction = normalizeBasis(basis);
55 for (const I of groups) {
56 // Compute basis only base on y.
57 const basisValue = basisFunction(I, Y);
58 for (const i of I) {
59 for (let j = 0; j < Yn.length; j++) {
60 const [, V] = Yn[j];
61 const [, newV] = newYn[j];
62 newV[i] = +V[i] / basisValue;
63 }
64 }
65 }
66 const specifiedTooltip = (0, helper_1.isUnset)(tooltip) || ((tooltip === null || tooltip === void 0 ? void 0 : tooltip.items) && (tooltip === null || tooltip === void 0 ? void 0 : tooltip.items.length) !== 0);
67 return [
68 I,
69 (0, util_1.deepMix)({}, mark, Object.assign({ encode: Object.fromEntries(newYn.map(([k, v]) => [k, (0, helper_2.column)(v, (0, helper_2.columnOf)(encode, k)[1])])) }, (!specifiedTooltip &&
70 encode.y0 && {
71 tooltip: { items: [{ channel: 'y0' }] },
72 }))),
73 ];
74 };
75};
76exports.NormalizeY = NormalizeY;
77exports.NormalizeY.props = {};
78//# sourceMappingURL=normalizeY.js.map
\No newline at end of file