utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 { CustomElement } from '@antv/g';
  13. import { Layout } from '@antv/gui';
  14. import { deepMix, upperFirst } from '@antv/util';
  15. import { select } from '../utils/selection';
  16. export function createComponent(descriptor) {
  17. return class extends CustomElement {
  18. constructor(config) {
  19. super(config);
  20. this.descriptor = descriptor;
  21. }
  22. connectedCallback() {
  23. var _a, _b;
  24. (_b = (_a = this.descriptor).render) === null || _b === void 0 ? void 0 : _b.call(_a, this.attributes, this);
  25. }
  26. update(cfg = {}) {
  27. var _a, _b;
  28. this.attr(deepMix({}, this.attributes, cfg));
  29. (_b = (_a = this.descriptor).render) === null || _b === void 0 ? void 0 : _b.call(_a, this.attributes, this);
  30. }
  31. };
  32. }
  33. export function maybeAppend(parent, selector, node) {
  34. if (!parent.querySelector(selector)) {
  35. return select(parent).append(node);
  36. }
  37. return select(parent).select(selector);
  38. }
  39. export function titleContent(field) {
  40. return Array.isArray(field) ? field.join(', ') : `${field || ''}`;
  41. }
  42. export function inferComponentLayout(position, userDefinitions) {
  43. const preset = {
  44. display: 'flex',
  45. flexDirection: 'row',
  46. justifyContent: 'flex-start',
  47. alignItems: 'center',
  48. };
  49. if (userDefinitions) {
  50. return Object.assign(Object.assign({}, preset), userDefinitions);
  51. }
  52. let { flexDirection, justifyContent, alignItems } = preset;
  53. const layout = {
  54. top: ['row', 'flex-start', 'center'],
  55. bottom: ['row', 'flex-start', 'center'],
  56. left: ['colunm', 'center', 'center'],
  57. right: ['colunm', 'center', 'center'],
  58. center: ['column', 'center', 'center'],
  59. };
  60. if (position in layout) {
  61. [flexDirection, justifyContent, alignItems] = layout[position];
  62. }
  63. return { display: 'flex', flexDirection, justifyContent, alignItems };
  64. }
  65. export class G2Layout extends Layout {
  66. get child() {
  67. var _a;
  68. return (_a = this.children) === null || _a === void 0 ? void 0 : _a[0];
  69. }
  70. update(options) {
  71. var _a;
  72. this.attr(options);
  73. const { subOptions } = options;
  74. (_a = this.child) === null || _a === void 0 ? void 0 : _a.update(subOptions);
  75. }
  76. }
  77. export class LegendCategoryLayout extends G2Layout {
  78. update(options) {
  79. var _a;
  80. const { subOptions } = options;
  81. (_a = this.child) === null || _a === void 0 ? void 0 : _a.update(subOptions);
  82. }
  83. }
  84. export function scaleOf(scales, type) {
  85. var _a;
  86. return (_a = scales.filter((s) => s.getOptions().name === type)) === null || _a === void 0 ? void 0 : _a[0];
  87. }
  88. export function isHorizontal(orientation) {
  89. return orientation === 'horizontal' || orientation === 0;
  90. }
  91. export function isVertical(orientation) {
  92. return orientation === 'vertical' || orientation === -Math.PI / 2;
  93. }
  94. export function inferComponentShape(value, options, component) {
  95. const { bbox } = value;
  96. const { position = 'top', size: userDefinedSize, length: userDefinedLength, } = options;
  97. const isHorizontal = ['top', 'bottom', 'center'].includes(position);
  98. const [bboxSize, bboxLength] = isHorizontal
  99. ? [bbox.height, bbox.width]
  100. : [bbox.width, bbox.height];
  101. const { defaultSize, defaultLength } = component.props;
  102. const size = userDefinedSize || defaultSize || bboxSize;
  103. const length = userDefinedLength || defaultLength || bboxLength;
  104. const orientation = isHorizontal ? 'horizontal' : 'vertical';
  105. const [width, height] = isHorizontal ? [length, size] : [size, length];
  106. return { orientation, width, height, size, length };
  107. }
  108. export function domainOf(scales) {
  109. // to get a available scale's domain
  110. return scales
  111. .find((scale) => scale.getOptions().domain.length > 0)
  112. .getOptions().domain;
  113. }
  114. export function adaptor(style) {
  115. const reservedKeys = [
  116. 'arrow',
  117. 'crosshairs',
  118. 'grid',
  119. 'handle',
  120. 'handleLabel',
  121. 'indicator',
  122. 'label',
  123. 'line',
  124. 'tick',
  125. 'tip',
  126. 'title',
  127. 'trunc',
  128. ];
  129. // @ts-ignore
  130. const { style: styles } = style, rest = __rest(style, ["style"]);
  131. const finalStyle = {};
  132. Object.entries(rest).forEach(([key, value]) => {
  133. if (reservedKeys.includes(key)) {
  134. finalStyle[`show${upperFirst(key)}`] = value;
  135. }
  136. else
  137. finalStyle[key] = value;
  138. });
  139. return Object.assign(Object.assign({}, finalStyle), styles);
  140. }
  141. //# sourceMappingURL=utils.js.map