flexX.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FlexX = void 0;
  4. const util_1 = require("@antv/util");
  5. const d3_array_1 = require("d3-array");
  6. const helper_1 = require("./utils/helper");
  7. function valueOf(data, field) {
  8. if (typeof field === 'string')
  9. return data.map((d) => d[field]);
  10. return data.map(field);
  11. }
  12. function 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. */
  22. const 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. };
  33. exports.FlexX = FlexX;
  34. exports.FlexX.props = {};
  35. //# sourceMappingURL=flexX.js.map