data-marker.js 8.9 KB

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