line.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.renderAxisLine = exports.isAxisVertical = exports.isAxisHorizontal = exports.getValuePos = exports.getArcValuePos = exports.getLinearValuePos = void 0;
  4. var tslib_1 = require("tslib");
  5. var util_1 = require("@antv/util");
  6. var animation_1 = require("../../../animation");
  7. var util_2 = require("../../../util");
  8. var constant_1 = require("../constant");
  9. var utils_1 = require("./utils");
  10. exports.getLinearValuePos = (0, util_1.memoize)(function (value, attr) {
  11. var _a = tslib_1.__read(attr.startPos, 2), sx = _a[0], sy = _a[1], _b = tslib_1.__read(attr.endPos, 2), ex = _b[0], ey = _b[1];
  12. var _c = tslib_1.__read([ex - sx, ey - sy], 2), dx = _c[0], dy = _c[1];
  13. return [sx + dx * value, sy + dy * value];
  14. }, function (value, attr) { return tslib_1.__spreadArray(tslib_1.__spreadArray([value], tslib_1.__read(attr.startPos), false), tslib_1.__read(attr.endPos), false).join(); });
  15. exports.getArcValuePos = (0, util_1.memoize)(function (value, attr) {
  16. var radius = attr.radius, _a = tslib_1.__read(attr.center, 2), cx = _a[0], cy = _a[1];
  17. var angle = (0, util_2.degToRad)((0, utils_1.getLineAngle)(value, attr));
  18. return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)];
  19. }, function (value, attr) {
  20. var startAngle = attr.startAngle, endAngle = attr.endAngle, radius = attr.radius, center = attr.center;
  21. return tslib_1.__spreadArray([value, startAngle, endAngle, radius], tslib_1.__read(center), false).join();
  22. });
  23. function getValuePos(value, attr) {
  24. if (attr.type === 'linear')
  25. return (0, exports.getLinearValuePos)(value, attr);
  26. return (0, exports.getArcValuePos)(value, attr);
  27. }
  28. exports.getValuePos = getValuePos;
  29. function isAxisHorizontal(attr) {
  30. return (0, utils_1.getLineTangentVector)(0, attr)[1] === 0;
  31. }
  32. exports.isAxisHorizontal = isAxisHorizontal;
  33. function isAxisVertical(attr) {
  34. return (0, utils_1.getLineTangentVector)(0, attr)[0] === 0;
  35. }
  36. exports.isAxisVertical = isAxisVertical;
  37. function isCircle(startAngle, endAngle) {
  38. return endAngle - startAngle === 360;
  39. }
  40. function getArcPath(startAngle, endAngle, cx, cy, radius) {
  41. var diffAngle = endAngle - startAngle;
  42. var _a = tslib_1.__read([radius, radius], 2), rx = _a[0], ry = _a[1];
  43. var _b = tslib_1.__read([(0, util_2.degToRad)(startAngle), (0, util_2.degToRad)(endAngle)], 2), startAngleRadians = _b[0], endAngleRadians = _b[1];
  44. var getPosByAngle = function (angle) { return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)]; };
  45. var _c = tslib_1.__read(getPosByAngle(startAngleRadians), 2), x1 = _c[0], y1 = _c[1];
  46. var _d = tslib_1.__read(getPosByAngle(endAngleRadians), 2), x2 = _d[0], y2 = _d[1];
  47. if (isCircle(startAngle, endAngle)) {
  48. var middleAngleRadians = (endAngleRadians + startAngleRadians) / 2;
  49. var _e = tslib_1.__read(getPosByAngle(middleAngleRadians), 2), xm = _e[0], ym = _e[1];
  50. return [
  51. ['M', x1, y1],
  52. ['A', rx, ry, 0, 1, 0, xm, ym],
  53. ['A', rx, ry, 0, 1, 0, x2, y2],
  54. ];
  55. }
  56. // 大小弧
  57. var large = diffAngle > 180 ? 1 : 0;
  58. // 1-顺时针 0-逆时针
  59. var sweep = startAngle > endAngle ? 0 : 1;
  60. var isClosePath = false;
  61. return isClosePath
  62. ? "M".concat(cx, ",").concat(cy, ",L").concat(x1, ",").concat(y1, ",A").concat(rx, ",").concat(ry, ",0,").concat(large, ",").concat(sweep, ",").concat(x2, ",").concat(y2, ",L").concat(cx, ",").concat(cy)
  63. : "M".concat(x1, ",").concat(y1, ",A").concat(rx, ",").concat(ry, ",0,").concat(large, ",").concat(sweep, ",").concat(x2, ",").concat(y2);
  64. }
  65. function getArcAttr(arc) {
  66. var _a = arc.attributes, startAngle = _a.startAngle, endAngle = _a.endAngle, center = _a.center, radius = _a.radius;
  67. return tslib_1.__spreadArray(tslib_1.__spreadArray([startAngle, endAngle], tslib_1.__read(center), false), [radius], false);
  68. }
  69. function renderArc(container, attr, style, animate) {
  70. var startAngle = attr.startAngle, endAngle = attr.endAngle, center = attr.center, radius = attr.radius;
  71. return container
  72. .selectAll(constant_1.CLASS_NAMES.line.class)
  73. .data([{ path: getArcPath.apply(void 0, tslib_1.__spreadArray(tslib_1.__spreadArray([startAngle, endAngle], tslib_1.__read(center), false), [radius], false)) }], function (d, i) { return i; })
  74. .join(function (enter) {
  75. return enter
  76. .append('path')
  77. .attr('className', constant_1.CLASS_NAMES.line.name)
  78. .styles(attr)
  79. .styles({ path: function (d) { return d.path; } });
  80. }, function (update) {
  81. return update
  82. .transition(function () {
  83. var _this = this;
  84. var animation = (0, util_2.keyframeInterpolate)(this, getArcAttr(this), tslib_1.__spreadArray(tslib_1.__spreadArray([startAngle, endAngle], tslib_1.__read(center), false), [radius], false), animate.update);
  85. if (animation) {
  86. var layout = function () {
  87. var data = (0, util_1.get)(_this.attributes, '__keyframe_data__');
  88. _this.style.path = getArcPath.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(data), false));
  89. };
  90. animation.onframe = layout;
  91. animation.onfinish = layout;
  92. }
  93. return animation;
  94. })
  95. .styles(attr);
  96. }, function (exit) { return exit.remove(); })
  97. .styles(style)
  98. .transitions();
  99. }
  100. function renderTruncation(container, _a) {
  101. var truncRange = _a.truncRange, truncShape = _a.truncShape, lineExtension = _a.lineExtension;
  102. // TODO
  103. }
  104. function extendLine(startPos, endPos, range) {
  105. if (range === void 0) { range = [0, 0]; }
  106. var _a = tslib_1.__read([startPos, endPos, range], 3), _b = tslib_1.__read(_a[0], 2), x1 = _b[0], y1 = _b[1], _c = tslib_1.__read(_a[1], 2), x2 = _c[0], y2 = _c[1], _d = tslib_1.__read(_a[2], 2), l1 = _d[0], l2 = _d[1];
  107. var _e = tslib_1.__read([x2 - x1, y2 - y1], 2), x = _e[0], y = _e[1];
  108. var L = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  109. var _f = tslib_1.__read([-l1 / L, l2 / L], 2), s1 = _f[0], s2 = _f[1];
  110. return [s1 * x, s1 * y, s2 * x, s2 * y];
  111. }
  112. function getLinePath(points) {
  113. var _a = tslib_1.__read(points, 2), _b = tslib_1.__read(_a[0], 2), x1 = _b[0], y1 = _b[1], _c = tslib_1.__read(_a[1], 2), x2 = _c[0], y2 = _c[1];
  114. return { x1: x1, y1: y1, x2: x2, y2: y2 };
  115. }
  116. function renderLinear(container, attr, style, animate) {
  117. var showTrunc = attr.showTrunc, startPos = attr.startPos, endPos = attr.endPos, truncRange = attr.truncRange, lineExtension = attr.lineExtension;
  118. var _a = tslib_1.__read([startPos, endPos], 2), _b = tslib_1.__read(_a[0], 2), x1 = _b[0], y1 = _b[1], _c = tslib_1.__read(_a[1], 2), x2 = _c[0], y2 = _c[1];
  119. var _d = tslib_1.__read(lineExtension ? extendLine(startPos, endPos, lineExtension) : new Array(4).fill(0), 4), ox1 = _d[0], oy1 = _d[1], ox2 = _d[2], oy2 = _d[3];
  120. var renderLine = function (data) {
  121. return container
  122. .selectAll(constant_1.CLASS_NAMES.line.class)
  123. .data(data, function (d, i) { return i; })
  124. .join(function (enter) {
  125. return enter
  126. .append('line')
  127. .attr('className', function (d) { return "".concat(constant_1.CLASS_NAMES.line.name, " ").concat(d.className); })
  128. .styles(style)
  129. .transition(function (d) {
  130. return (0, animation_1.transition)(this, getLinePath(d.line), false);
  131. });
  132. }, function (update) {
  133. return update.styles(style).transition(function (_a) {
  134. var line = _a.line;
  135. return (0, animation_1.transition)(this, getLinePath(line), animate.update);
  136. });
  137. }, function (exit) { return exit.remove(); })
  138. .transitions();
  139. };
  140. if (!showTrunc || !truncRange) {
  141. return renderLine([
  142. {
  143. line: [
  144. [x1 + ox1, y1 + oy1],
  145. [x2 + ox2, y2 + oy2],
  146. ],
  147. className: constant_1.CLASS_NAMES.line.name,
  148. },
  149. ]);
  150. }
  151. var _e = tslib_1.__read(truncRange, 2), r1 = _e[0], r2 = _e[1];
  152. var dx = x2 - x1;
  153. var dy = y2 - y1;
  154. var _f = tslib_1.__read([x1 + dx * r1, y1 + dy * r1], 2), x3 = _f[0], y3 = _f[1];
  155. var _g = tslib_1.__read([x1 + dx * r2, y1 + dy * r2], 2), x4 = _g[0], y4 = _g[1];
  156. var animation = renderLine([
  157. {
  158. line: [
  159. [x1 + ox1, y1 + oy1],
  160. [x3, y3],
  161. ],
  162. className: constant_1.CLASS_NAMES.lineFirst.name,
  163. },
  164. {
  165. line: [
  166. [x4, y4],
  167. [x2 + ox2, y2 + oy2],
  168. ],
  169. className: constant_1.CLASS_NAMES.lineSecond.name,
  170. },
  171. ]);
  172. renderTruncation(container, attr);
  173. return animation;
  174. }
  175. function renderAxisArrow(container, type, attr, style) {
  176. var showArrow = attr.showArrow, showTrunc = attr.showTrunc, lineArrow = attr.lineArrow, lineArrowOffset = attr.lineArrowOffset, lineArrowSize = attr.lineArrowSize;
  177. var shapeToAddArrow;
  178. if (type === 'arc')
  179. shapeToAddArrow = container.select(constant_1.CLASS_NAMES.line.class);
  180. else if (showTrunc)
  181. shapeToAddArrow = container.select(constant_1.CLASS_NAMES.lineSecond.class);
  182. else
  183. shapeToAddArrow = container.select(constant_1.CLASS_NAMES.line.class);
  184. if (!showArrow || !lineArrow || (attr.type === 'arc' && isCircle(attr.startAngle, attr.endAngle))) {
  185. var node = shapeToAddArrow.node();
  186. if (node)
  187. node.style.markerEnd = undefined;
  188. return;
  189. }
  190. var arrow = (0, util_2.renderExtDo)(lineArrow);
  191. arrow.attr(style);
  192. (0, util_2.scaleToPixel)(arrow, lineArrowSize, true);
  193. shapeToAddArrow.style('markerEnd', arrow).style('markerEndOffset', -lineArrowOffset);
  194. }
  195. function renderAxisLine(container, attr, animate) {
  196. var type = attr.type;
  197. var animation;
  198. var style = (0, util_2.subStyleProps)(attr, 'line');
  199. if (type === 'linear')
  200. animation = renderLinear(container, attr, (0, util_2.omit)(style, 'arrow'), animate);
  201. else
  202. animation = renderArc(container, attr, (0, util_2.omit)(style, 'arrow'), animate);
  203. renderAxisArrow(container, type, attr, style);
  204. return animation;
  205. }
  206. exports.renderAxisLine = renderAxisLine;
  207. //# sourceMappingURL=line.js.map