stackEnter.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.StackEnter = 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. /**
  8. * Group marks by channels into groups and stacking their enterDelay
  9. * to make marks show up groups by groups.
  10. * It will update enterDelay channel for each mark by its enterDuration and group.
  11. * @todo Support orderBy.
  12. * @todo Sort among groups(e.g. reverse).
  13. * @todo Stack enter in groups rather than between groups?
  14. * @todo Auto inter this statistic for scaleInY animation in stacked interval?
  15. * @todo All the groups shared the enterDuration?
  16. */
  17. const StackEnter = (options) => {
  18. const { groupBy = ['x'], reducer = (I, V) => V[I[0]], orderBy = null, reverse = false, duration, } = options;
  19. return (I, mark) => {
  20. const { encode } = mark;
  21. // Extract group information by each specified channel,
  22. // and skip if all values of channels are empty.
  23. const by = Array.isArray(groupBy) ? groupBy : [groupBy];
  24. const groupEntries = by.map((k) => [k, (0, helper_1.columnOf)(encode, k)[0]]);
  25. if (groupEntries.length === 0)
  26. return [I, mark];
  27. // Nest group index and flatten them in right order among timeline.
  28. // [[1, 2, 3, 4, 5, 6]] ->
  29. // [[1, 2, 3], [4, 5, 6]] ->
  30. // [[1], [2], [3], [4], [5], [6]]
  31. let groups = [I];
  32. for (const [, V] of groupEntries) {
  33. const newGroups = [];
  34. for (const I of groups) {
  35. const G = Array.from((0, d3_array_1.group)(I, (i) => V[i]).values());
  36. // @todo sort by x.
  37. newGroups.push(...G);
  38. }
  39. groups = newGroups;
  40. }
  41. // const {color} = encode;
  42. if (orderBy) {
  43. const [V] = (0, helper_1.columnOf)(encode, orderBy);
  44. if (V)
  45. groups.sort((I, J) => reducer(I, V) - reducer(J, V));
  46. if (reverse)
  47. groups.reverse();
  48. }
  49. // Stack delay for each group.
  50. const t = (duration || 3000) / groups.length;
  51. const [ED] = duration
  52. ? [(0, helper_1.constant)(I, t)] // If specified duration, generate enter duration for each.
  53. : (0, helper_1.maybeColumnOf)(encode, 'enterDuration', (0, helper_1.constant)(I, t));
  54. const [EDL] = (0, helper_1.maybeColumnOf)(encode, 'enterDelay', (0, helper_1.constant)(I, 0));
  55. const newEnterDelay = new Array(I.length);
  56. for (let i = 0, pd = 0; i < groups.length; i++) {
  57. const I = groups[i];
  58. const maxDuration = (0, d3_array_1.max)(I, (i) => +ED[i]);
  59. for (const j of I)
  60. newEnterDelay[j] = +EDL[j] + pd;
  61. pd += maxDuration;
  62. }
  63. return [
  64. I,
  65. (0, util_1.deepMix)({}, mark, {
  66. encode: {
  67. enterDuration: (0, helper_1.visualColumn)(ED),
  68. enterDelay: (0, helper_1.visualColumn)(newEnterDelay),
  69. },
  70. }),
  71. ];
  72. };
  73. };
  74. exports.StackEnter = StackEnter;
  75. exports.StackEnter.props = {};
  76. //# sourceMappingURL=stackEnter.js.map