31 lines
1.0 KiB
JavaScript
31 lines
1.0 KiB
JavaScript
import * as d3 from "d3";
|
|
|
|
export const calculateNodePositions = (data, nodePositions) => {
|
|
if (!data || !data.items) return { root: null, nodes: [], links: [] };
|
|
|
|
const root = d3.hierarchy(data, (d) => d.items);
|
|
const treeLayout = d3.tree().size([4000 * Math.PI, 300]); // Угловое распределение (радиан, радиус)
|
|
|
|
treeLayout(root); // Заполняем координаты
|
|
|
|
const nodes = root.descendants();
|
|
const links = nodes
|
|
.filter((d) => d.parent)
|
|
.map((d) => ({
|
|
source: d.parent,
|
|
target: d,
|
|
}));
|
|
|
|
// Преобразуем полярные координаты в декартовые
|
|
nodes.forEach((node) => {
|
|
const angle = node.x; // x теперь угол (радианы)
|
|
const radius = node.y; // y теперь радиус
|
|
nodePositions.set(node.data.id, {
|
|
x: radius * Math.cos(angle),
|
|
y: radius * Math.sin(angle),
|
|
});
|
|
});
|
|
|
|
return { root, nodes, links };
|
|
};
|