flexX.js 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import { deepMix } from '@antv/util';
  2. import { rollups, sum } from 'd3-array';
  3. import { columnOf } from './utils/helper';
  4. function valueOf(data, field) {
  5. if (typeof field === 'string')
  6. return data.map((d) => d[field]);
  7. return data.map(field);
  8. }
  9. function createReducer(reducer, V) {
  10. if (typeof reducer === 'function')
  11. return (GI) => reducer(GI, V);
  12. if (reducer === 'sum')
  13. return (GI) => sum(GI, (i) => +V[i]);
  14. throw new Error(`Unknown reducer: ${reducer}`);
  15. }
  16. /**
  17. * Produce flex options from data for x scale.
  18. */
  19. export const FlexX = (options = {}) => {
  20. const { field, channel = 'y', reducer = 'sum' } = options;
  21. return (I, mark) => {
  22. const { data, encode } = mark;
  23. const [x] = columnOf(encode, 'x');
  24. const V = field ? valueOf(data, field) : columnOf(encode, channel)[0];
  25. const reducerFunction = createReducer(reducer, V);
  26. const flex = rollups(I, reducerFunction, (i) => x[i]).map((d) => d[1]);
  27. return [I, deepMix({}, mark, { scale: { x: { flex } } })];
  28. };
  29. };
  30. FlexX.props = {};
  31. //# sourceMappingURL=flexX.js.map