repeatMatrix.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.RepeatMatrix = void 0;
  15. const util_1 = require("@antv/util");
  16. const container_1 = require("../utils/container");
  17. const vector_1 = require("../utils/vector");
  18. const array_1 = require("../utils/array");
  19. const facetRect_1 = require("./facetRect");
  20. const utils_1 = require("./utils");
  21. const setScale = (0, utils_1.useDefaultAdaptor)((options) => {
  22. return {
  23. scale: {
  24. x: { guide: null, paddingOuter: 0, paddingInner: 0.1 },
  25. y: { guide: null, range: [0, 1], paddingOuter: 0, paddingInner: 0.1 },
  26. },
  27. };
  28. });
  29. const setChildren = (0, utils_1.useOverrideAdaptor)((options) => {
  30. const { data, children, x: originX = 0, y: originY = 0, key: viewKey, } = options;
  31. const createChildren = (visualData, scale, layout) => {
  32. const { x: scaleX, y: scaleY } = scale;
  33. const { paddingLeft, paddingTop } = layout;
  34. const { domain: domainX } = scaleX.getOptions();
  35. const { domain: domainY } = scaleY.getOptions();
  36. const index = (0, array_1.indexOf)(visualData);
  37. const bboxs = visualData.map(({ points }) => (0, vector_1.calcBBox)(points));
  38. const values = visualData.map(({ x, y }) => [
  39. scaleX.invert(x),
  40. scaleY.invert(y),
  41. ]);
  42. const facets = values.map(([fx, fy]) => ({
  43. columnField: fx,
  44. columnIndex: domainX.indexOf(fx),
  45. columnValue: fx,
  46. columnValuesLength: domainX.length,
  47. rowField: fy,
  48. rowIndex: domainY.indexOf(fy),
  49. rowValue: fy,
  50. rowValuesLength: domainY.length,
  51. }));
  52. const normalizedChildren = facets.map((facet) => {
  53. if (Array.isArray(children))
  54. return children;
  55. return [children(facet)].flat(1);
  56. });
  57. return index.flatMap((i) => {
  58. const [left, top, width, height] = bboxs[i];
  59. const [fx, fy] = values[i];
  60. const facet = facets[i];
  61. const children = normalizedChildren[i];
  62. return children.map((d) => {
  63. var _a, _b;
  64. const { scale, key, encode, axis } = d, rest = __rest(d, ["scale", "key", "encode", "axis"]);
  65. const guideY = (_a = scale === null || scale === void 0 ? void 0 : scale.y) === null || _a === void 0 ? void 0 : _a.guide;
  66. const guideX = (_b = scale === null || scale === void 0 ? void 0 : scale.x) === null || _b === void 0 ? void 0 : _b.guide;
  67. const defaultScale = {
  68. // Do not sync position scales among facets by default.
  69. x: { facet: false },
  70. // Do not sync position scales among facets by default.
  71. y: { facet: false },
  72. };
  73. const newAxis = {
  74. x: createGuideX(guideX)(facet, data),
  75. y: createGuideY(guideY)(facet, data),
  76. };
  77. const defaultAxis = {
  78. x: { tickCount: 5 },
  79. y: { tickCount: 5 },
  80. };
  81. return Object.assign({ data, parentKey: viewKey, key: `${key}-${i}`, x: left + paddingLeft + originX, y: top + paddingTop + originY, width,
  82. height, paddingLeft: 0, paddingRight: 0, paddingTop: 0, paddingBottom: 0, frame: true, scale: (0, util_1.deepMix)(defaultScale, scale), axis: (0, util_1.deepMix)(defaultAxis, axis, newAxis),
  83. // Hide all legends for child mark by default,
  84. // they are displayed in the top-level.
  85. legend: false, encode: (0, util_1.deepMix)({}, encode, {
  86. x: fx,
  87. y: fy,
  88. }) }, rest);
  89. });
  90. });
  91. };
  92. return {
  93. children: createChildren,
  94. };
  95. });
  96. /**
  97. * @todo Use transform instead of override data directly.
  98. */
  99. const setData = (0, utils_1.useOverrideAdaptor)((options) => {
  100. const { encode } = options, rest = __rest(options, ["encode"]);
  101. const { position: P = [], x: X = P, y: Y = [...P].reverse() } = encode, restEncode = __rest(encode, ["position", "x", "y"]);
  102. const data = [];
  103. for (const $x of [X].flat(1)) {
  104. for (const $y of [Y].flat(1)) {
  105. data.push({ $x, $y });
  106. }
  107. }
  108. return Object.assign(Object.assign({}, rest), { data, encode: Object.assign(Object.assign({}, restEncode), { x: '$x', y: '$y' }), scale: Object.assign(Object.assign({}, ([X].flat(1).length === 1 && { x: { paddingInner: 0 } })), ([Y].flat(1).length === 1 && { y: { paddingInner: 0 } })) });
  109. });
  110. function createGuideX(guideX) {
  111. if (typeof guideX === 'function')
  112. return guideX;
  113. if (guideX === null)
  114. return () => null;
  115. return (facet, data) => {
  116. const { rowIndex, rowValuesLength } = facet;
  117. // Only the bottom-most facet show axisX.
  118. if (rowIndex !== rowValuesLength - 1)
  119. return (0, facetRect_1.createInnerGuide)(guideX, data);
  120. };
  121. }
  122. function createGuideY(guideY) {
  123. if (typeof guideY === 'function')
  124. return guideY;
  125. if (guideY === null)
  126. return () => null;
  127. return (facet, data) => {
  128. const { columnIndex } = facet;
  129. // Only the left-most facet show axisY.
  130. if (columnIndex !== 0)
  131. return (0, facetRect_1.createInnerGuide)(guideY, data);
  132. };
  133. }
  134. /**
  135. * @todo Layout mode: layer, row, col...
  136. * @todo Specify show axis or not.
  137. */
  138. const RepeatMatrix = () => {
  139. return (options) => {
  140. const newOptions = container_1.Container.of(options)
  141. .call(facetRect_1.toCell)
  142. .call(facetRect_1.inferColor)
  143. .call(setChildren)
  144. .call(setData)
  145. .call(facetRect_1.setAnimation)
  146. .call(facetRect_1.setStyle)
  147. .call(setScale)
  148. .value();
  149. return [newOptions];
  150. };
  151. };
  152. exports.RepeatMatrix = RepeatMatrix;
  153. //# sourceMappingURL=repeatMatrix.js.map