UNPKG

3.74 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.Sort = void 0;
15const util_1 = require("@antv/util");
16const d3_array_1 = require("d3-array");
17const helper_1 = require("./utils/helper");
18function createReducer(channel, options, encode) {
19 const { by = channel, reducer = 'max' } = options;
20 const [V] = (0, helper_1.columnOf)(encode, by);
21 if (typeof reducer === 'function')
22 return (GI) => reducer(GI, V);
23 if (reducer === 'max')
24 return (GI) => (0, d3_array_1.max)(GI, (i) => +V[i]);
25 if (reducer === 'min')
26 return (GI) => (0, d3_array_1.min)(GI, (i) => +V[i]);
27 if (reducer === 'sum')
28 return (GI) => (0, d3_array_1.sum)(GI, (i) => +V[i]);
29 if (reducer === 'median')
30 return (GI) => (0, d3_array_1.median)(GI, (i) => +V[i]);
31 if (reducer === 'mean')
32 return (GI) => (0, d3_array_1.mean)(GI, (i) => +V[i]);
33 if (reducer === 'first')
34 return (GI) => V[GI[0]];
35 if (reducer === 'last')
36 return (GI) => V[GI[GI.length - 1]];
37 throw new Error(`Unknown reducer: ${reducer}`);
38}
39function sortQuantitative(I, mark, options) {
40 const { reverse, channel } = options;
41 const { encode } = mark;
42 const [V] = (0, helper_1.columnOf)(encode, channel);
43 const sortedI = (0, d3_array_1.sort)(I, (i) => V[i]);
44 if (reverse)
45 sortedI.reverse();
46 // const s = typeof slice === 'number' ? [0, slice] : slice;
47 return [sortedI, mark];
48}
49// If domain is specified, only sort data in the domain.
50function filterIndex(I, values, specifiedDomain) {
51 if (!Array.isArray(specifiedDomain))
52 return I;
53 const domain = new Set(specifiedDomain);
54 return I.filter((i) => domain.has(values[i]));
55}
56function sortOrdinal(I, mark, options) {
57 var _a;
58 const { reverse, slice, channel } = options, rest = __rest(options, ["reverse", "slice", "channel"]);
59 const { encode, scale = {} } = mark;
60 const domain = (_a = scale[channel]) === null || _a === void 0 ? void 0 : _a.domain;
61 const [T] = (0, helper_1.columnOf)(encode, channel);
62 const normalizeReducer = createReducer(channel, rest, encode);
63 const SI = filterIndex(I, T, domain);
64 const sortedDomain = (0, d3_array_1.groupSort)(SI, normalizeReducer, (i) => T[i]);
65 if (reverse)
66 sortedDomain.reverse();
67 const s = typeof slice === 'number' ? [0, slice] : slice;
68 const slicedDomain = slice ? sortedDomain.slice(...s) : sortedDomain;
69 return [
70 I,
71 (0, util_1.deepMix)(mark, {
72 scale: {
73 [channel]: {
74 domain: slicedDomain,
75 },
76 },
77 }),
78 ];
79}
80/**
81 * Sort marks groups by groups.
82 */
83const Sort = (options = {}) => {
84 const { reverse = false, slice, channel, ordinal = true } = options, rest = __rest(options, ["reverse", "slice", "channel", "ordinal"]);
85 return (I, mark) => {
86 if (!ordinal) {
87 return sortQuantitative(I, mark, Object.assign({ reverse,
88 slice,
89 channel }, rest));
90 }
91 return sortOrdinal(I, mark, Object.assign({ reverse, slice, channel }, rest));
92 };
93};
94exports.Sort = Sort;
95exports.Sort.props = {};
96//# sourceMappingURL=sort.js.map
\No newline at end of file