| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.renderAxisLine = exports.isAxisVertical = exports.isAxisHorizontal = exports.getValuePos = exports.getArcValuePos = exports.getLinearValuePos = void 0;
- var tslib_1 = require("tslib");
- var util_1 = require("@antv/util");
- var animation_1 = require("../../../animation");
- var util_2 = require("../../../util");
- var constant_1 = require("../constant");
- var utils_1 = require("./utils");
- exports.getLinearValuePos = (0, util_1.memoize)(function (value, attr) {
- 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];
- var _c = tslib_1.__read([ex - sx, ey - sy], 2), dx = _c[0], dy = _c[1];
- return [sx + dx * value, sy + dy * value];
- }, function (value, attr) { return tslib_1.__spreadArray(tslib_1.__spreadArray([value], tslib_1.__read(attr.startPos), false), tslib_1.__read(attr.endPos), false).join(); });
- exports.getArcValuePos = (0, util_1.memoize)(function (value, attr) {
- var radius = attr.radius, _a = tslib_1.__read(attr.center, 2), cx = _a[0], cy = _a[1];
- var angle = (0, util_2.degToRad)((0, utils_1.getLineAngle)(value, attr));
- return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)];
- }, function (value, attr) {
- var startAngle = attr.startAngle, endAngle = attr.endAngle, radius = attr.radius, center = attr.center;
- return tslib_1.__spreadArray([value, startAngle, endAngle, radius], tslib_1.__read(center), false).join();
- });
- function getValuePos(value, attr) {
- if (attr.type === 'linear')
- return (0, exports.getLinearValuePos)(value, attr);
- return (0, exports.getArcValuePos)(value, attr);
- }
- exports.getValuePos = getValuePos;
- function isAxisHorizontal(attr) {
- return (0, utils_1.getLineTangentVector)(0, attr)[1] === 0;
- }
- exports.isAxisHorizontal = isAxisHorizontal;
- function isAxisVertical(attr) {
- return (0, utils_1.getLineTangentVector)(0, attr)[0] === 0;
- }
- exports.isAxisVertical = isAxisVertical;
- function isCircle(startAngle, endAngle) {
- return endAngle - startAngle === 360;
- }
- function getArcPath(startAngle, endAngle, cx, cy, radius) {
- var diffAngle = endAngle - startAngle;
- var _a = tslib_1.__read([radius, radius], 2), rx = _a[0], ry = _a[1];
- var _b = tslib_1.__read([(0, util_2.degToRad)(startAngle), (0, util_2.degToRad)(endAngle)], 2), startAngleRadians = _b[0], endAngleRadians = _b[1];
- var getPosByAngle = function (angle) { return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)]; };
- var _c = tslib_1.__read(getPosByAngle(startAngleRadians), 2), x1 = _c[0], y1 = _c[1];
- var _d = tslib_1.__read(getPosByAngle(endAngleRadians), 2), x2 = _d[0], y2 = _d[1];
- if (isCircle(startAngle, endAngle)) {
- var middleAngleRadians = (endAngleRadians + startAngleRadians) / 2;
- var _e = tslib_1.__read(getPosByAngle(middleAngleRadians), 2), xm = _e[0], ym = _e[1];
- return [
- ['M', x1, y1],
- ['A', rx, ry, 0, 1, 0, xm, ym],
- ['A', rx, ry, 0, 1, 0, x2, y2],
- ];
- }
- // 大小弧
- var large = diffAngle > 180 ? 1 : 0;
- // 1-顺时针 0-逆时针
- var sweep = startAngle > endAngle ? 0 : 1;
- var isClosePath = false;
- return isClosePath
- ? "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)
- : "M".concat(x1, ",").concat(y1, ",A").concat(rx, ",").concat(ry, ",0,").concat(large, ",").concat(sweep, ",").concat(x2, ",").concat(y2);
- }
- function getArcAttr(arc) {
- var _a = arc.attributes, startAngle = _a.startAngle, endAngle = _a.endAngle, center = _a.center, radius = _a.radius;
- return tslib_1.__spreadArray(tslib_1.__spreadArray([startAngle, endAngle], tslib_1.__read(center), false), [radius], false);
- }
- function renderArc(container, attr, style, animate) {
- var startAngle = attr.startAngle, endAngle = attr.endAngle, center = attr.center, radius = attr.radius;
- return container
- .selectAll(constant_1.CLASS_NAMES.line.class)
- .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; })
- .join(function (enter) {
- return enter
- .append('path')
- .attr('className', constant_1.CLASS_NAMES.line.name)
- .styles(attr)
- .styles({ path: function (d) { return d.path; } });
- }, function (update) {
- return update
- .transition(function () {
- var _this = this;
- 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);
- if (animation) {
- var layout = function () {
- var data = (0, util_1.get)(_this.attributes, '__keyframe_data__');
- _this.style.path = getArcPath.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(data), false));
- };
- animation.onframe = layout;
- animation.onfinish = layout;
- }
- return animation;
- })
- .styles(attr);
- }, function (exit) { return exit.remove(); })
- .styles(style)
- .transitions();
- }
- function renderTruncation(container, _a) {
- var truncRange = _a.truncRange, truncShape = _a.truncShape, lineExtension = _a.lineExtension;
- // TODO
- }
- function extendLine(startPos, endPos, range) {
- if (range === void 0) { range = [0, 0]; }
- 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];
- var _e = tslib_1.__read([x2 - x1, y2 - y1], 2), x = _e[0], y = _e[1];
- var L = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
- var _f = tslib_1.__read([-l1 / L, l2 / L], 2), s1 = _f[0], s2 = _f[1];
- return [s1 * x, s1 * y, s2 * x, s2 * y];
- }
- function getLinePath(points) {
- 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];
- return { x1: x1, y1: y1, x2: x2, y2: y2 };
- }
- function renderLinear(container, attr, style, animate) {
- var showTrunc = attr.showTrunc, startPos = attr.startPos, endPos = attr.endPos, truncRange = attr.truncRange, lineExtension = attr.lineExtension;
- 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];
- 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];
- var renderLine = function (data) {
- return container
- .selectAll(constant_1.CLASS_NAMES.line.class)
- .data(data, function (d, i) { return i; })
- .join(function (enter) {
- return enter
- .append('line')
- .attr('className', function (d) { return "".concat(constant_1.CLASS_NAMES.line.name, " ").concat(d.className); })
- .styles(style)
- .transition(function (d) {
- return (0, animation_1.transition)(this, getLinePath(d.line), false);
- });
- }, function (update) {
- return update.styles(style).transition(function (_a) {
- var line = _a.line;
- return (0, animation_1.transition)(this, getLinePath(line), animate.update);
- });
- }, function (exit) { return exit.remove(); })
- .transitions();
- };
- if (!showTrunc || !truncRange) {
- return renderLine([
- {
- line: [
- [x1 + ox1, y1 + oy1],
- [x2 + ox2, y2 + oy2],
- ],
- className: constant_1.CLASS_NAMES.line.name,
- },
- ]);
- }
- var _e = tslib_1.__read(truncRange, 2), r1 = _e[0], r2 = _e[1];
- var dx = x2 - x1;
- var dy = y2 - y1;
- var _f = tslib_1.__read([x1 + dx * r1, y1 + dy * r1], 2), x3 = _f[0], y3 = _f[1];
- var _g = tslib_1.__read([x1 + dx * r2, y1 + dy * r2], 2), x4 = _g[0], y4 = _g[1];
- var animation = renderLine([
- {
- line: [
- [x1 + ox1, y1 + oy1],
- [x3, y3],
- ],
- className: constant_1.CLASS_NAMES.lineFirst.name,
- },
- {
- line: [
- [x4, y4],
- [x2 + ox2, y2 + oy2],
- ],
- className: constant_1.CLASS_NAMES.lineSecond.name,
- },
- ]);
- renderTruncation(container, attr);
- return animation;
- }
- function renderAxisArrow(container, type, attr, style) {
- var showArrow = attr.showArrow, showTrunc = attr.showTrunc, lineArrow = attr.lineArrow, lineArrowOffset = attr.lineArrowOffset, lineArrowSize = attr.lineArrowSize;
- var shapeToAddArrow;
- if (type === 'arc')
- shapeToAddArrow = container.select(constant_1.CLASS_NAMES.line.class);
- else if (showTrunc)
- shapeToAddArrow = container.select(constant_1.CLASS_NAMES.lineSecond.class);
- else
- shapeToAddArrow = container.select(constant_1.CLASS_NAMES.line.class);
- if (!showArrow || !lineArrow || (attr.type === 'arc' && isCircle(attr.startAngle, attr.endAngle))) {
- var node = shapeToAddArrow.node();
- if (node)
- node.style.markerEnd = undefined;
- return;
- }
- var arrow = (0, util_2.renderExtDo)(lineArrow);
- arrow.attr(style);
- (0, util_2.scaleToPixel)(arrow, lineArrowSize, true);
- shapeToAddArrow.style('markerEnd', arrow).style('markerEndOffset', -lineArrowOffset);
- }
- function renderAxisLine(container, attr, animate) {
- var type = attr.type;
- var animation;
- var style = (0, util_2.subStyleProps)(attr, 'line');
- if (type === 'linear')
- animation = renderLinear(container, attr, (0, util_2.omit)(style, 'arrow'), animate);
- else
- animation = renderArc(container, attr, (0, util_2.omit)(style, 'arrow'), animate);
- renderAxisArrow(container, type, attr, style);
- return animation;
- }
- exports.renderAxisLine = renderAxisLine;
- //# sourceMappingURL=line.js.map
|