UNPKG

2.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Filter = void 0;
4const util_1 = require("@antv/util");
5const helper_1 = require("../utils/helper");
6const helper_2 = require("./utils/helper");
7function normalizeValue(value) {
8 if (typeof value === 'object')
9 return [value.value, value.ordinal];
10 else
11 return [value, true];
12}
13/**
14 * The Filter transform filter channels.
15 */
16const Filter = (options = {}) => {
17 return (I, mark) => {
18 const { encode, data } = mark;
19 const filters = Object.entries(options)
20 .map(([key, v]) => {
21 const [V] = (0, helper_2.columnOf)(encode, key);
22 // Skip empty channel.
23 if (!V)
24 return null;
25 const [value, ordinal = true] = normalizeValue(v);
26 if (typeof value === 'function')
27 return (i) => value(V[i]);
28 if (ordinal) {
29 const expectedValues = Array.isArray(value) ? value : [value];
30 // Skip empty expected values.
31 if (expectedValues.length === 0)
32 return null;
33 return (i) => expectedValues.includes(V[i]);
34 }
35 else {
36 const [start, end] = value;
37 return (i) => V[i] >= start && V[i] <= end;
38 }
39 })
40 .filter(helper_1.defined);
41 // Skip empty filters.
42 if (filters.length === 0)
43 return [I, mark];
44 // Filter index and channels.
45 const totalFilter = (i) => filters.every((f) => f(i));
46 const FI = I.filter(totalFilter);
47 const newIndex = FI.map((_, i) => i);
48 const newEncodes = Object.entries(encode).map(([key, encode]) => {
49 return [
50 key,
51 Object.assign(Object.assign({}, encode), { value: newIndex
52 .map((i) => encode.value[FI[i]])
53 .filter((v) => v !== undefined) }),
54 ];
55 });
56 return [
57 newIndex,
58 (0, util_1.deepMix)({}, mark, {
59 encode: Object.fromEntries(newEncodes),
60 // Filter data for tooltip item.
61 data: FI.map((i) => data[i]),
62 }),
63 ];
64 };
65};
66exports.Filter = Filter;
67exports.Filter.props = {};
68//# sourceMappingURL=filter.js.map
\No newline at end of file