import { Graph } from '../../Graph'; import { StaticLayout } from './StaticLayout'; class GridLayout extends StaticLayout { apply (graph: Graph): void { const bbox = graph.boundingBox(); const cx = (bbox[1] + bbox[3]) / 2; const cy = (bbox[0] + bbox[2]) / 2; const w = 0.8 * Math.abs(bbox[1] - bbox[3]); const h = 0.8 * Math.abs(bbox[0] - bbox[2]); const nodes = graph.nodes(); const ratio = w/h; let r = 0, c = 0; while (r * c < nodes.length) { c / r < ratio ? ++c : ++r; } const t = cy + h / 2; const l = cx - w / 2; const dx = w / c; const dy = h / r; let row = 0, col = 0; nodes.forEach(node => { node.x = l + col * dx; node.y = t - row * dy; if (col++ === c) { col = 0; row++; } }); } } export { GridLayout }