data-marker.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { __assign, __extends, __rest } from "tslib";
  2. import { get } from '@antv/util';
  3. import GroupComponent from '../abstract/group-component';
  4. import { renderTag } from '../util/graphic';
  5. import { applyTranslate } from '../util/matrix';
  6. import Theme from '../util/theme';
  7. var DataMarkerAnnotation = /** @class */ (function (_super) {
  8. __extends(DataMarkerAnnotation, _super);
  9. function DataMarkerAnnotation() {
  10. return _super !== null && _super.apply(this, arguments) || this;
  11. }
  12. /**
  13. * 默认的配置项
  14. * @returns {object} 默认的配置项
  15. */
  16. DataMarkerAnnotation.prototype.getDefaultCfg = function () {
  17. var cfg = _super.prototype.getDefaultCfg.call(this);
  18. return __assign(__assign({}, cfg), { name: 'annotation', type: 'dataMarker', locationType: 'point', x: 0, y: 0, point: {}, line: {}, text: {}, direction: 'upward', autoAdjust: true, coordinateBBox: null, defaultCfg: {
  19. point: {
  20. display: true,
  21. style: {
  22. r: 3,
  23. fill: '#FFFFFF',
  24. stroke: '#1890FF',
  25. lineWidth: 2,
  26. },
  27. },
  28. line: {
  29. display: true,
  30. length: 20,
  31. style: {
  32. stroke: Theme.lineColor,
  33. lineWidth: 1,
  34. },
  35. },
  36. text: {
  37. content: '',
  38. display: true,
  39. style: {
  40. fill: Theme.textColor,
  41. opacity: 0.65,
  42. fontSize: 12,
  43. textAlign: 'start',
  44. fontFamily: Theme.fontFamily,
  45. },
  46. },
  47. } });
  48. };
  49. DataMarkerAnnotation.prototype.renderInner = function (group) {
  50. if (get(this.get('line'), 'display')) {
  51. this.renderLine(group);
  52. }
  53. if (get(this.get('text'), 'display')) {
  54. this.renderText(group);
  55. }
  56. if (get(this.get('point'), 'display')) {
  57. this.renderPoint(group);
  58. }
  59. if (this.get('autoAdjust')) {
  60. this.autoAdjust(group);
  61. }
  62. };
  63. DataMarkerAnnotation.prototype.applyOffset = function () {
  64. this.moveElementTo(this.get('group'), {
  65. x: this.get('x') + this.get('offsetX'),
  66. y: this.get('y') + this.get('offsetY'),
  67. });
  68. };
  69. DataMarkerAnnotation.prototype.renderPoint = function (group) {
  70. var point = this.getShapeAttrs().point;
  71. this.addShape(group, {
  72. type: 'circle',
  73. id: this.getElementId('point'),
  74. name: 'annotation-point',
  75. attrs: point,
  76. });
  77. };
  78. DataMarkerAnnotation.prototype.renderLine = function (group) {
  79. var line = this.getShapeAttrs().line;
  80. this.addShape(group, {
  81. type: 'path',
  82. id: this.getElementId('line'),
  83. name: 'annotation-line',
  84. attrs: line,
  85. });
  86. };
  87. DataMarkerAnnotation.prototype.renderText = function (group) {
  88. var textAttrs = this.getShapeAttrs().text;
  89. var x = textAttrs.x, y = textAttrs.y, text = textAttrs.text, style = __rest(textAttrs, ["x", "y", "text"]);
  90. var _a = this.get('text'), background = _a.background, maxLength = _a.maxLength, autoEllipsis = _a.autoEllipsis, isVertival = _a.isVertival, ellipsisPosition = _a.ellipsisPosition;
  91. var tagCfg = {
  92. x: x,
  93. y: y,
  94. id: this.getElementId('text'),
  95. name: 'annotation-text',
  96. content: text,
  97. style: style,
  98. background: background,
  99. maxLength: maxLength,
  100. autoEllipsis: autoEllipsis,
  101. isVertival: isVertival,
  102. ellipsisPosition: ellipsisPosition,
  103. };
  104. renderTag(group, tagCfg);
  105. };
  106. DataMarkerAnnotation.prototype.autoAdjust = function (group) {
  107. var direction = this.get('direction');
  108. var x = this.get('x');
  109. var y = this.get('y');
  110. var lineLength = get(this.get('line'), 'length', 0);
  111. var coordinateBBox = this.get('coordinateBBox');
  112. var _a = group.getBBox(), minX = _a.minX, maxX = _a.maxX, minY = _a.minY, maxY = _a.maxY;
  113. var textGroup = group.findById(this.getElementId('text-group'));
  114. var textShape = group.findById(this.getElementId('text'));
  115. var lineShape = group.findById(this.getElementId('line'));
  116. if (!coordinateBBox) {
  117. return;
  118. }
  119. if (textGroup) {
  120. var translateX = textGroup.attr('x'), translateY = textGroup.attr('y');
  121. var _b = textShape.getCanvasBBox(), width = _b.width, height = _b.height;
  122. var xFactor = 0, yFactor = 0;
  123. if (x + minX <= coordinateBBox.minX) {
  124. // 左侧超出
  125. if (direction === 'leftward') {
  126. xFactor = 1;
  127. }
  128. else {
  129. var overflow = coordinateBBox.minX - (x + minX);
  130. translateX = textGroup.attr('x') + overflow;
  131. }
  132. }
  133. else if (x + maxX >= coordinateBBox.maxX) {
  134. // 右侧超出
  135. if (direction === 'rightward') {
  136. xFactor = -1;
  137. }
  138. else {
  139. var overflow = x + maxX - coordinateBBox.maxX;
  140. translateX = textGroup.attr('x') - overflow;
  141. }
  142. }
  143. if (!!xFactor) {
  144. if (lineShape) {
  145. lineShape.attr('path', [
  146. ['M', 0, 0],
  147. ['L', lineLength * xFactor, 0],
  148. ]);
  149. }
  150. translateX = (lineLength + 2 + width) * xFactor;
  151. }
  152. if (y + minY <= coordinateBBox.minY) {
  153. // 上方超出
  154. if (direction === 'upward') {
  155. yFactor = 1;
  156. }
  157. else {
  158. var overflow = coordinateBBox.minY - (y + minY);
  159. translateY = textGroup.attr('y') + overflow;
  160. }
  161. }
  162. else if (y + maxY >= coordinateBBox.maxY) {
  163. // 下方超出
  164. if (direction === 'downward') {
  165. yFactor = -1;
  166. }
  167. else {
  168. var overflow = y + maxY - coordinateBBox.maxY;
  169. translateY = textGroup.attr('y') - overflow;
  170. }
  171. }
  172. if (!!yFactor) {
  173. if (lineShape) {
  174. lineShape.attr('path', [
  175. ['M', 0, 0],
  176. ['L', 0, lineLength * yFactor],
  177. ]);
  178. }
  179. translateY = (lineLength + 2 + height) * yFactor;
  180. }
  181. if (translateX !== textGroup.attr('x') || translateY !== textGroup.attr('y'))
  182. applyTranslate(textGroup, translateX, translateY);
  183. }
  184. };
  185. DataMarkerAnnotation.prototype.getShapeAttrs = function () {
  186. var lineDisplay = get(this.get('line'), 'display');
  187. var pointStyle = get(this.get('point'), 'style', {});
  188. var lineStyle = get(this.get('line'), 'style', {});
  189. var textStyle = get(this.get('text'), 'style', {});
  190. var direction = this.get('direction');
  191. var lineLength = lineDisplay ? get(this.get('line'), 'length', 0) : 0;
  192. var xFactor = 0, yFactor = 0;
  193. var textBaseline = 'top', textAlign = 'start';
  194. switch (direction) {
  195. case 'upward':
  196. yFactor = -1;
  197. textBaseline = 'bottom';
  198. break;
  199. case 'downward':
  200. yFactor = 1;
  201. textBaseline = 'top';
  202. break;
  203. case 'leftward':
  204. xFactor = -1;
  205. textAlign = 'end';
  206. break;
  207. case 'rightward':
  208. xFactor = 1;
  209. textAlign = 'start';
  210. break;
  211. }
  212. return {
  213. point: __assign({ x: 0, y: 0 }, pointStyle),
  214. line: __assign({ path: [
  215. ['M', 0, 0],
  216. ['L', lineLength * xFactor, lineLength * yFactor],
  217. ] }, lineStyle),
  218. text: __assign({ x: (lineLength + 2) * xFactor, y: (lineLength + 2) * yFactor, text: get(this.get('text'), 'content', ''), textBaseline: textBaseline,
  219. textAlign: textAlign }, textStyle),
  220. };
  221. };
  222. return DataMarkerAnnotation;
  223. }(GroupComponent));
  224. export default DataMarkerAnnotation;
  225. //# sourceMappingURL=data-marker.js.map