symmetryY.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 { extent } from 'd3-array';
  14. import { columnOf, column } from './utils/helper';
  15. import { createGroups } from './utils/order';
  16. /**
  17. * The SymmetryY transform apply offset for y channels, say to transform
  18. * them to be symmetry.
  19. */
  20. export const SymmetryY = (options = {}) => {
  21. const { groupBy = 'x' } = options;
  22. return (I, mark) => {
  23. const { encode } = mark;
  24. const { x } = encode, rest = __rest(encode, ["x"]);
  25. // Extract and create new channels starts with y, such as y, y1.
  26. const Yn = Object.entries(rest)
  27. .filter(([k]) => k.startsWith('y'))
  28. .map(([k]) => [k, columnOf(encode, k)[0]]);
  29. const newYn = Yn.map(([k]) => [k, new Array(I.length)]);
  30. // Group marks into series by specified keys.
  31. const groups = createGroups(groupBy, I, mark);
  32. const MY = new Array(groups.length);
  33. for (let i = 0; i < groups.length; i++) {
  34. const I = groups[i];
  35. const Y = I.flatMap((i) => Yn.map(([, V]) => +V[i]));
  36. const [minY, maxY] = extent(Y);
  37. MY[i] = (minY + maxY) / 2;
  38. }
  39. const maxMiddleY = Math.max(...MY);
  40. for (let m = 0; m < groups.length; m++) {
  41. const offset = maxMiddleY - MY[m];
  42. const I = groups[m];
  43. for (const i of I) {
  44. for (let j = 0; j < Yn.length; j++) {
  45. const [, V] = Yn[j];
  46. const [, newV] = newYn[j];
  47. newV[i] = +V[i] + offset;
  48. }
  49. }
  50. }
  51. return [
  52. I,
  53. deepMix({}, mark, {
  54. encode: Object.fromEntries(newYn.map(([k, v]) => [k, column(v, columnOf(encode, k)[1])])),
  55. }),
  56. ];
  57. };
  58. };
  59. SymmetryY.props = {};
  60. //# sourceMappingURL=symmetryY.js.map