| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.getDefaultStyle = exports.inferIdentityStyle = exports.inferRotation = exports.pointOfArc = exports.inferRadialStyle = exports.inferNonCircularStyle = void 0;
- const utils_1 = require("../../../shape/utils");
- const coordinate_1 = require("../../../utils/coordinate");
- const helper_1 = require("../../../utils/helper");
- const vector_1 = require("../../../utils/vector");
- function inferNonCircularStyle(position, points, value, coordinate) {
- const { bounds } = value;
- const [[x0, y0], [x1, y1]] = bounds;
- const w = x1 - x0;
- const h = y1 - y0;
- const xy = (options) => {
- const { x: ox, y: oy } = options;
- const px = (0, helper_1.maybePercentage)(value.x, w);
- const py = (0, helper_1.maybePercentage)(value.y, h);
- return Object.assign(Object.assign({}, options), { x: (px || ox) + x0, y: (py || oy) + y0 });
- };
- // 4 direction.
- if (position === 'left')
- return xy({ x: 0, y: h / 2, textAnchor: 'start', textBaseline: 'middle' });
- if (position === 'right')
- return xy({ x: w, y: h / 2, textAnchor: 'end', textBaseline: 'middle' });
- if (position === 'top')
- return xy({ x: w / 2, y: 0, textAnchor: 'center', textBaseline: 'top' });
- if (position === 'bottom')
- return xy({ x: w / 2, y: h, textAnchor: 'center', textBaseline: 'bottom' });
- // 4 corner position.
- if (position === 'top-left')
- return xy({ x: 0, y: 0, textAnchor: 'start', textBaseline: 'top' });
- if (position === 'top-right')
- return xy({ x: w, y: 0, textAnchor: 'end', textBaseline: 'top' });
- if (position === 'bottom-left')
- return xy({ x: 0, y: h, textAnchor: 'start', textBaseline: 'bottom' });
- if (position === 'bottom-right')
- return xy({ x: w, y: h, textAnchor: 'end', textBaseline: 'bottom' });
- // default return 'inside'
- return xy({
- x: w / 2,
- y: h / 2,
- textAnchor: 'center',
- textBaseline: 'middle',
- });
- }
- exports.inferNonCircularStyle = inferNonCircularStyle;
- function inferRadialStyle(position, points, value, coordinate) {
- const { y, y1, autoRotate, rotateToAlignArc } = value;
- const center = coordinate.getCenter();
- const arcObject = (0, utils_1.getArcObject)(coordinate, points, [y, y1]);
- const { innerRadius, outerRadius, startAngle, endAngle } = arcObject;
- const angle = position === 'inside' ? (startAngle + endAngle) / 2 : endAngle;
- const rotate = inferRotation(angle, autoRotate, rotateToAlignArc);
- const point = (() => {
- const [p0, p1] = points;
- const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
- const [x, y] = position === 'inside' ? pointOfArc(center, angle, radius) : (0, vector_1.mid)(p0, p1);
- return { x, y };
- })();
- return Object.assign(Object.assign({}, point), { textAlign: position === 'inside' ? 'center' : 'start', textBaseline: 'middle', rotate });
- }
- exports.inferRadialStyle = inferRadialStyle;
- function pointOfArc(center, angle, radius) {
- return [
- center[0] + Math.sin(angle) * radius,
- center[1] - Math.cos(angle) * radius,
- ];
- }
- exports.pointOfArc = pointOfArc;
- function inferRotation(angle, autoRotate, rotateToAlignArc) {
- if (!autoRotate)
- return 0;
- const append = rotateToAlignArc ? 0 : Math.sin(angle) < 0 ? 90 : -90;
- return (angle / Math.PI) * 180 + append;
- }
- exports.inferRotation = inferRotation;
- function inferInnerCircularStyle(position, points, value, coordinate) {
- const { y, y1, autoRotate, rotateToAlignArc, radius: radiusRatio = 0.5, offset = 0, } = value;
- const arcObject = (0, utils_1.getArcObject)(coordinate, points, [y, y1]);
- const { startAngle, endAngle } = arcObject;
- const center = coordinate.getCenter();
- const angle = (startAngle + endAngle) / 2;
- const rotate = inferRotation(angle, autoRotate, rotateToAlignArc);
- const textStyle = { textAlign: 'center', textBaseline: 'middle', rotate };
- const { innerRadius, outerRadius } = arcObject;
- const r0 = innerRadius + (outerRadius - innerRadius) * radiusRatio;
- const r1 = r0 + offset;
- const [x0, y0] = pointOfArc(center, angle, r1);
- return Object.assign({ x: x0, y: y0 }, textStyle);
- }
- function inferIdentityStyle(position, points, value, coordinate) {
- const { bounds } = value;
- const [p] = bounds;
- return {
- x: p[0],
- y: p[1],
- };
- }
- exports.inferIdentityStyle = inferIdentityStyle;
- function getDefaultStyle(position, points, value, coordinate) {
- const { bounds } = value;
- // When bounds.length = 1
- // For series mark, such as line and area.
- // The bounds for text is defined with only one point.
- // Use this point as the label position.
- if (bounds.length === 1) {
- return inferIdentityStyle(position, points, value, coordinate);
- }
- const inferDefaultStyle = (0, coordinate_1.isRadial)(coordinate)
- ? inferRadialStyle
- : (0, coordinate_1.isCircular)(coordinate)
- ? inferInnerCircularStyle
- : inferNonCircularStyle;
- return inferDefaultStyle(position, points, value, coordinate);
- }
- exports.getDefaultStyle = getDefaultStyle;
- //# sourceMappingURL=default.js.map
|