UNPKG

1.31 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.FlexX = void 0;
4const util_1 = require("@antv/util");
5const d3_array_1 = require("d3-array");
6const helper_1 = require("./utils/helper");
7function valueOf(data, field) {
8 if (typeof field === 'string')
9 return data.map((d) => d[field]);
10 return data.map(field);
11}
12function createReducer(reducer, V) {
13 if (typeof reducer === 'function')
14 return (GI) => reducer(GI, V);
15 if (reducer === 'sum')
16 return (GI) => (0, d3_array_1.sum)(GI, (i) => +V[i]);
17 throw new Error(`Unknown reducer: ${reducer}`);
18}
19/**
20 * Produce flex options from data for x scale.
21 */
22const FlexX = (options = {}) => {
23 const { field, channel = 'y', reducer = 'sum' } = options;
24 return (I, mark) => {
25 const { data, encode } = mark;
26 const [x] = (0, helper_1.columnOf)(encode, 'x');
27 const V = field ? valueOf(data, field) : (0, helper_1.columnOf)(encode, channel)[0];
28 const reducerFunction = createReducer(reducer, V);
29 const flex = (0, d3_array_1.rollups)(I, reducerFunction, (i) => x[i]).map((d) => d[1]);
30 return [I, (0, util_1.deepMix)({}, mark, { scale: { x: { flex } } })];
31 };
32};
33exports.FlexX = FlexX;
34exports.FlexX.props = {};
35//# sourceMappingURL=flexX.js.map
\No newline at end of file