repeatMatrix.js 6.1 KB

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