normalizeY.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { deepMix } from '@antv/util';
  13. import { mean, deviation, median, sum, max, min } from 'd3-array';
  14. import { isUnset } from '../utils/helper';
  15. import { column, columnOf } from './utils/helper';
  16. import { createGroups } from './utils/order';
  17. function normalizeBasis(basis) {
  18. if (typeof basis === 'function')
  19. return basis;
  20. const registry = {
  21. min: (I, Y) => min(I, (i) => Y[+i]),
  22. max: (I, Y) => max(I, (i) => Y[+i]),
  23. first: (I, Y) => Y[I[0]],
  24. last: (I, Y) => Y[I[I.length - 1]],
  25. mean: (I, Y) => mean(I, (i) => Y[+i]),
  26. median: (I, Y) => median(I, (i) => Y[+i]),
  27. sum: (I, Y) => sum(I, (i) => Y[+i]),
  28. deviation: (I, Y) => deviation(I, (i) => Y[+i]),
  29. };
  30. return registry[basis] || max;
  31. }
  32. /**
  33. * Group marks into series by specified channels, and then transform
  34. * each series's value, say to transform them relative to some basis
  35. * to apply a moving average.
  36. */
  37. export const NormalizeY = (options = {}) => {
  38. const { groupBy = 'x', basis = 'max' } = options;
  39. return (I, mark) => {
  40. const { encode, tooltip } = mark;
  41. const { x } = encode, rest = __rest(encode, ["x"]);
  42. // Extract and create new channels starts with y, such as y, y1.
  43. const Yn = Object.entries(rest)
  44. .filter(([k]) => k.startsWith('y'))
  45. .map(([k]) => [k, columnOf(encode, k)[0]]);
  46. const [, Y] = Yn.find(([k]) => k === 'y');
  47. const newYn = Yn.map(([k]) => [k, new Array(I.length)]);
  48. // Group marks into series by specified keys.
  49. const groups = createGroups(groupBy, I, mark);
  50. // Transform y channels for each group based on basis.
  51. const basisFunction = normalizeBasis(basis);
  52. for (const I of groups) {
  53. // Compute basis only base on y.
  54. const basisValue = basisFunction(I, Y);
  55. for (const i of I) {
  56. for (let j = 0; j < Yn.length; j++) {
  57. const [, V] = Yn[j];
  58. const [, newV] = newYn[j];
  59. newV[i] = +V[i] / basisValue;
  60. }
  61. }
  62. }
  63. const specifiedTooltip = isUnset(tooltip) || ((tooltip === null || tooltip === void 0 ? void 0 : tooltip.items) && (tooltip === null || tooltip === void 0 ? void 0 : tooltip.items.length) !== 0);
  64. return [
  65. I,
  66. deepMix({}, mark, Object.assign({ encode: Object.fromEntries(newYn.map(([k, v]) => [k, column(v, columnOf(encode, k)[1])])) }, (!specifiedTooltip &&
  67. encode.y0 && {
  68. tooltip: { items: [{ channel: 'y0' }] },
  69. }))),
  70. ];
  71. };
  72. };
  73. NormalizeY.props = {};
  74. //# sourceMappingURL=normalizeY.js.map