import * as d3 from 'd3'; export type d3Selection = d3.Selection; export type GenericConstructor = new (...args: any[]) => T; const SVG_NAMESPACE = 'http://www.w3.org/2000/svg'; export function createSVGElement(tagName: string) { return document.createElementNS(SVG_NAMESPACE, tagName); } export function fromTransform(str: string) { const result = {}; const tr = /translate\((.+?)\)/; const sr = /scale\((.+?)\)/; const tm = str.match(tr); const sm = str.match(sr); if (tm) { // chrome下通过逗号分割,IE下通过空格分割 if (tm[1].includes(',')) { result.translate = tm[1].split(/\s*,\s*/).map(item => parseFloat(item)); } else { result.translate = tm[1].split(/\s+/).map(item => parseFloat(item)); // y = 0 时,IE 会省略 y 值 if (result.translate.length === 1) { result.translate.push(0); } } } if (sm) { result.scale = parseFloat(sm[1].trim()); } return result; } export function toTransform(option: { translate?: [number, number], scale?: number }) { const { translate, scale } = option; let str = `translate(${ translate.join(',') })`; if (scale) { str += `scale(${ scale })`; } return str; } export function createTextWrap(maxWidth: number) { return function () { const $self = d3.select(this); let text = $self.text(); while (this.getComputedTextLength() > maxWidth && text.length > 0) { text = text.slice(0, -1); $self.text(text + '..'); } }; } export function createMutilineTextWrap(maxWidth: number, lineCount: number = 2) { if (lineCount < 2) throw new Error('lineCount can not small than 2.'); return function () { const $self = d3.select(this); const results = []; const text = $self.text(); const total = text.length; const style = window.getComputedStyle(this); const fontSize = parseInt(style.fontSize, 10); const lineHeight = parseInt(style.lineHeight, 10); const ratio = lineHeight / fontSize; let lineNumber = 1; let start = 0; let stop = total; while (lineNumber < lineCount && start < total) { if (this.getSubStringLength(start, stop) > maxWidth) { stop -= 1; } else { results.push(text.slice(start, stop)); start = stop; stop = total; lineNumber += 1; } } if (start < total) { results.push(text.slice(start)); } $self.text(null); results.forEach((subText, index) => { const dy = index === 0 ? ratio / 2 : ratio; $self.append('tspan').attr('x', 0).attr('dy', `${dy}em`).text(subText); }); const tspan = this.querySelector('tspan:last-child'); const wrap = createTextWrap(maxWidth); if (tspan.getComputedTextLength() > maxWidth) { d3.select(tspan).each(wrap); } }; } export interface ICoord { x: number; y: number; } export function getCurvePoints(start: ICoord, end: ICoord) { // 如果 x 坐标接近相等,返回一条直线 if (Math.abs(start.x - end.x) < 1) { return [{ ...start }, { ...end }]; } const point1 = { x: Math.ceil((3 * start.x + end.x) / 4), y: Math.ceil((3 * start.y + end.y) / 4), }; const point2 = { x: Math.ceil((3 * end.x + start.x) / 4), y: Math.ceil((3 * end.y + start.y) / 4), }; const midPoint = { x: Math.ceil((start.x + end.x) / 2), y: Math.ceil((start.y + end.y) / 2), }; if (start.x < end.x) { point1.x -= 20; point2.x += 20; } else { point1.x += 20; point2.x -= 20; } return [ { ...start }, { x: start.x, y: (end.y - start.y)/2 + start.y }, { ...midPoint }, { x: end.x, y: end.y - (end.y - start.y)/2 }, { ...end }, ]; } export function getDistance(point1: ICoord, point2: ICoord) { return Math.sqrt(Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2)); } export const cache = (function () { const data = {}; return { set (key: string, value: any) { data[key] = value; }, get (key: string) { return data[key] || null; }, has (key: string) { return data.hasOwnProperty(key); }, remove (key: string) { delete data[key]; }, }; })(); export function createGraphTools(graph: any) { const tools = { getEdgesByNodeId (nodeId: string): any[] { return Object.values(graph.edges).filter((edge: any) => { return edge.srcNodeId === nodeId || edge.destNodeId === nodeId; }); }, getEdgesBySlotId (slotId: string): any[] { return Object.values(graph.edges).filter((edge: any) => { return edge.srcSlotId === slotId || edge.destSlotId === slotId; }); }, }; return tools; }