UNPKG

@visactor/vrender-components

Version:

components library for dp visualization

77 lines (67 loc) 3.75 kB
import { degreeToRadian, isEmpty } from "@visactor/vutils"; import { genRotateBounds, itemIntersect } from "./util"; export function autoRotate(items, rotateConfig) { if (isEmpty(items)) return; const {orient: orient, labelRotateAngle: labelRotateAngle = [ 0, 45, 90 ]} = rotateConfig; if (0 === labelRotateAngle.length || items.some((item => !!item.attribute.angle))) return; let i = 0, n = 0, testAngle = labelRotateAngle; for (items.length > 2 && ("bottom" === orient || "top" === orient ? Math.abs(items[1].attribute.x - items[0].attribute.x) < items[0].attribute.fontSize / 2 && (testAngle = [ labelRotateAngle[labelRotateAngle.length - 1] ]) : Math.abs(items[1].attribute.y - items[0].attribute.y) < items[0].attribute.fontSize / 2 && (testAngle = [ labelRotateAngle[labelRotateAngle.length - 1] ])), testAngle && testAngle.length > 0 && (n = testAngle.length); i < n; ) { const angle = testAngle[i++]; if (items.forEach((item => { item.attribute.angle = degreeToRadian(angle); })), tryRotate(orient, items), !hasIntersect(items)) break; } } function hasIntersect(items) { for (let i = 1; i < items.length; i++) if (itemIntersect(items[i - 1], items[i])) return !0; return !1; } function tryRotate(orient, items) { "bottom" !== orient && "top" !== orient || rotateXAxis(orient, items), "left" !== orient && "right" !== orient || rotateYAxis(orient, items), genRotateBounds(items); } function clampAngle(angle = 0) { if (angle < 0) for (;angle < 0; ) angle += 2 * Math.PI; if (angle > 0) for (;angle >= 2 * Math.PI; ) angle -= 2 * Math.PI; return angle; } export function rotateYAxis(orient, items) { items.forEach(((item, i) => { item.attribute.angle && item.setAttributes(Object.assign(Object.assign({}, getYAxisLabelAlign(orient, item.attribute.angle)), { angle: clampAngle(item.attribute.angle) })); })); } export function rotateXAxis(orient, items) { items.forEach((item => { item.attribute.angle && item.setAttributes(Object.assign(Object.assign({}, getXAxisLabelAlign(orient, item.attribute.angle)), { angle: clampAngle(item.attribute.angle) })); })); } export function getXAxisLabelAlign(orient, angle = 0) { let align = [ "center", "left", "left", "left", "center", "right", "right", "right", "left" ], baseline = [ "top", "top", "middle", "bottom", "bottom", "bottom", "middle", "top", "top" ]; "top" === orient && (align = [ "center", "right", "right", "right", "center", "left", "left", "left", "right" ], baseline = [ "bottom", "bottom", "middle", "top", "top", "top", "middle", "bottom", "bottom" ]); const step = (angle = clampAngle(angle)) / (.5 * Math.PI); let index; return index = step === Math.floor(step) ? 2 * Math.floor(step) : 2 * Math.floor(step) + 1, { textAlign: align[index], textBaseline: baseline[index] }; } export function getYAxisLabelAlign(orient, angle = 0) { let align = [ "right", "right", "center", "left", "left", "left", "center", "right", "right" ], baseline = [ "middle", "middle", "top", "top", "middle", "middle", "bottom", "bottom", "middle" ]; "right" === orient && (align = [ "left", "left", "center", "right", "right", "right", "center", "left", "left" ], baseline = [ "middle", "middle", "bottom", "bottom", "middle", "middle", "top", "middle", "middle" ]); const step = (angle = clampAngle(angle)) / (.5 * Math.PI); let index; return index = step === Math.floor(step) ? 2 * Math.floor(step) : 2 * Math.floor(step) + 1, { textAlign: align[index], textBaseline: baseline[index] }; } //# sourceMappingURL=auto-rotate.js.map