UNPKG

4.51 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.GroupN = void 0;
15const util_1 = require("@antv/util");
16const d3_array_1 = require("d3-array");
17const array_1 = require("../utils/array");
18const helper_1 = require("./utils/helper");
19function builtinFormatter(summary) {
20 return (d) => (d === null ? summary : `${summary} of ${d}`);
21}
22function normalizeReducer(reducer) {
23 if (typeof reducer === 'function')
24 return [reducer, null];
25 const registry = { mean, max, count, first, last, sum, min, median };
26 const reducerFunction = registry[reducer];
27 if (!reducerFunction)
28 throw new Error(`Unknown reducer: ${reducer}.`);
29 return reducerFunction();
30}
31function mean() {
32 const reducer = (I, V) => (0, d3_array_1.mean)(I, (i) => +V[i]);
33 const formatter = builtinFormatter('mean');
34 return [reducer, formatter];
35}
36function median() {
37 const reducer = (I, V) => (0, d3_array_1.median)(I, (i) => +V[i]);
38 const formatter = builtinFormatter('median');
39 return [reducer, formatter];
40}
41function max() {
42 const reducer = (I, V) => (0, d3_array_1.max)(I, (i) => +V[i]);
43 const formatter = builtinFormatter('max');
44 return [reducer, formatter];
45}
46function min() {
47 const reducer = (I, V) => (0, d3_array_1.min)(I, (i) => +V[i]);
48 const formatter = builtinFormatter('min');
49 return [reducer, formatter];
50}
51function count() {
52 const reducer = (I, V) => I.length;
53 const formatter = builtinFormatter('count');
54 return [reducer, formatter];
55}
56function sum() {
57 const reducer = (I, V) => (0, d3_array_1.sum)(I, (i) => +V[i]);
58 const formatter = builtinFormatter('sum');
59 return [reducer, formatter];
60}
61function first() {
62 const reducer = (I, V) => V[I[0]];
63 const formatter = builtinFormatter('first');
64 return [reducer, formatter];
65}
66function last() {
67 const reducer = (I, V) => V[I[I.length - 1]];
68 const formatter = builtinFormatter('last');
69 return [reducer, formatter];
70}
71/**
72 * The Group transform group data by x and y channels, and aggregate.
73 */
74const GroupN = (options = {}) => {
75 const { groupBy } = options, rest = __rest(options, ["groupBy"]);
76 return (I, mark) => {
77 const { data, encode } = mark;
78 const groups = groupBy(I, mark);
79 if (!groups)
80 return [I, mark];
81 // Extract field from from channel
82 // x1 from x, y1 from y, etc,.
83 const maybeFrom = (field, reducer) => {
84 if (field)
85 return field;
86 const { from } = reducer;
87 if (!from)
88 return field;
89 const [, field1] = (0, helper_1.columnOf)(encode, from);
90 return field1;
91 };
92 const outputs = Object.entries(rest).map(([channel, reducer]) => {
93 const [reducerFunction, formatter] = normalizeReducer(reducer);
94 const [V, field] = (0, helper_1.columnOf)(encode, channel);
95 const field1 = maybeFrom(field, reducer);
96 const RV = groups.map((I) => reducerFunction(I, V !== null && V !== void 0 ? V : data));
97 return [
98 channel,
99 Object.assign(Object.assign({}, (0, helper_1.nonConstantColumn)(RV, (formatter === null || formatter === void 0 ? void 0 : formatter(field1)) || field1)), { aggregate: true }),
100 ];
101 });
102 const reducedColumns = Object.keys(encode).map((key) => {
103 const [V, fv] = (0, helper_1.columnOf)(encode, key);
104 const GV = groups.map((I) => V[I[0]]);
105 return [key, (0, helper_1.column)(GV, fv)];
106 });
107 const GD = groups.map((I) => data[I[0]]);
108 const GI = (0, array_1.indexOf)(groups);
109 return [
110 GI,
111 (0, util_1.deepMix)({}, mark, {
112 data: GD,
113 encode: Object.fromEntries([...reducedColumns, ...outputs]),
114 }),
115 ];
116 };
117};
118exports.GroupN = GroupN;
119exports.GroupN.props = {};
120//# sourceMappingURL=groupN.js.map
\No newline at end of file