Пофиксил обновление графа

pull/8/head
DmitriyA 2025-03-03 04:05:21 -05:00
parent 750f06a4e5
commit dbb0662366
1 changed files with 60 additions and 49 deletions

View File

@ -1,48 +1,68 @@
import React, { useRef, useEffect } from "react";
import React, { useRef, useEffect, useMemo } from "react";
import * as d3 from "d3";
import { getStatusColor } from "./dataUtils";
const TreeChart = ({ data, onNodeClick }) => {
const chartRef = useRef();
const simulationRef = useRef(null);
useEffect(() => {
if (!data || !data.items) return;
const width = 1000;
const height = 1000;
const { root, nodes, links } = useMemo(() => {
if (!data || !data.items) return { root: null, nodes: [], links: [] };
const root = d3.hierarchy(data, (d) => d.items);
const links = root.links();
const nodes = root.descendants();
return { root, nodes, links };
}, [data]);
useEffect(() => {
if (!chartRef.current) return;
const svg = d3.select(chartRef.current)
.attr("width", 2000)
.attr("height", 1000)
.attr("viewBox", [-500, -500, 1000, 1000])
.attr("style", "max-width: 100%; height: auto;");
}, []);
useEffect(() => {
if (!root || !chartRef.current) return;
const svg = d3.select(chartRef.current);
svg.selectAll("*").remove();
svg
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto;");
if (simulationRef.current) {
simulationRef.current.stop();
}
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
const link = svg.select(".links")
.selectAll("line")
.data(links)
.join("line"); // Используем join для обновления связей
.data(links, (d) => `${d.source.data.id}-${d.target.data.id}`)
.join("line")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6);
const node = svg.append("g")
.attr("stroke", "#000")
.attr("stroke-width", 1.5)
const node = svg.select(".nodes")
.selectAll("circle")
.data(nodes)
.join("circle") // Используем join для обновления узлов
.data(nodes, (d) => d.data.id)
.join("circle")
.attr("fill", (d) => getStatusColor(d.data.status))
.attr("stroke", "#fff")
.attr("r", 7)
.call(drag());
// Обновляем только те узлы, которые нуждаются в изменении
node.on("click", (event, d) => {
if (onNodeClick) {
onNodeClick(d.data.id, d.data.title);
}
});
const text = svg.select(".labels")
.selectAll("text")
.data(nodes, (d) => d.data.id)
.join("text")
.text((d) => d.data.title)
.attr("dx", 12)
.attr("dy", 4);
node.each(function (d) {
if (d.data.status === "red") {
d3.select(this)
@ -69,28 +89,8 @@ const TreeChart = ({ data, onNodeClick }) => {
}
});
const text = svg.append("g")
.attr("fill", "#000")
.attr("font-family", "Arial")
.attr("font-size", 12)
.attr("pointer-events", "none")
.selectAll("text")
.data(nodes)
.join("text")
.text((d) => d.data.title)
.attr("dx", 12)
.attr("dy", 4);
node.append("title").text((d) => d.data.title);
node.on("click", (event, d) => {
if (onNodeClick) {
onNodeClick(d.data.id, d.data.title);
}
});
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id((d) => d.data.title).distance(80).strength(1))
.force("link", d3.forceLink(links).id((d) => d.data.id).distance(80).strength(1))
.force("charge", d3.forceManyBody().strength(-500))
.force("x", d3.forceX())
.force("y", d3.forceY());
@ -111,11 +111,14 @@ const TreeChart = ({ data, onNodeClick }) => {
.attr("y", (d) => d.y + 4);
});
simulationRef.current = simulation;
return () => simulation.stop();
}, [data, onNodeClick]);
}, [root, links, nodes, onNodeClick]);
const drag = () => {
function dragstarted(event, d) {
if (!event.active) simulationRef.current.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
@ -126,14 +129,22 @@ const TreeChart = ({ data, onNodeClick }) => {
}
function dragended(event, d) {
d.fx = null;
d.fy = null;
if (!event.active) simulationRef.current.alphaTarget(0);
d.fx = d.x;
d.fy = d.y;
}
return d3.drag().on("start", dragstarted).on("drag", dragged).on("end", dragended);
};
return <svg ref={chartRef}></svg>;
return (
<svg ref={chartRef}>
<g className="links" />
<g className="nodes" />
<g className="labels" />
</svg>
);
};
export default TreeChart;