feat: base 流程图边的处理

This commit is contained in:
caoqianming 2025-09-29 09:43:36 +08:00
parent 72339aa0f4
commit a212337b3a
1 changed files with 42 additions and 31 deletions

View File

@ -61,9 +61,15 @@ export default {
}); });
this.render = new dagreD3.render(); this.render = new dagreD3.render();
let svg = d3.select("#mySvg"); let svg = d3.select("#mySvg").attr("preserveAspectRatio", "xMidYMid meet");
this.inner = svg.append("g"); this.inner = svg.append("g");
//
let zoom = d3.zoom().on("zoom", (event) => {
this.inner.attr("transform", event.transform);
});
svg.call(zoom);
this.renderGraph(); this.renderGraph();
this.bindNodeClick(svg); this.bindNodeClick(svg);
}, },
@ -71,7 +77,7 @@ export default {
/** 渲染节点和边 */ /** 渲染节点和边 */
renderGraph() { renderGraph() {
let that = this; let that = this;
that.inner.selectAll("*").remove(); //
// //
that.g.nodes().forEach((v) => that.g.removeNode(v)); that.g.nodes().forEach((v) => that.g.removeNode(v));
that.g.edges().forEach((e) => that.g.removeEdge(e)); that.g.edges().forEach((e) => that.g.removeEdge(e));
@ -91,41 +97,46 @@ export default {
} }
}); });
// key const edgeGroups = {};
that.edges.forEach((edge, index) => { that.edges.forEach((edge) => {
let edgeColor = "green"; // 绿 const key = `${edge.source}_${edge.target}`;
if (edge.label === "驳回") edgeColor = "red"; if (!edgeGroups[key]) edgeGroups[key] = [];
if (edge.label === "退回") edgeColor = "red"; edgeGroups[key].push(edge);
});
that.g.setEdge( // 线
edge.source, const curves = [d3.curveBasis, d3.curveCardinal, d3.curveBundle.beta(0.8)];
edge.target,
{ //
label: edge.label, Object.values(edgeGroups).forEach((edges) => {
id: edge.id, edges.forEach((edge, index) => {
style: `fill:#ffffff;stroke:${edgeColor};stroke-width:1.5px`, let edgeColor = "green";
curve: d3.curveBasis, if (edge.label === "驳回" || edge.label === "退回") edgeColor = "red";
minlen: 1 + index % 3,
}, that.g.setEdge(
`${edge.source}_${edge.target}_${edge.id}` edge.source,
); edge.target,
{
label: edge.label,
id: edge.id,
//
curve: curves[index % curves.length],
labeloffset: 16 + index * 10, //
minlen: 2 + index, // dagre
style: `fill:none;stroke:${edgeColor};stroke-width:1.5px`,
},
`${edge.source}_${edge.target}_${edge.id}`
);
});
}); });
// //
this.render(this.inner, this.g); this.render(this.inner, this.g);
// SVG // viewBox
let mySvgHeight = const { width, height } = this.g.graph();
document.getElementsByClassName("nodes")[0]?.getBoundingClientRect().height + d3.select("#mySvg")
50; .attr("viewBox", `0 0 ${width + 2} ${height + 2}`);
let mySvgWdith =
document.getElementsByClassName("output")[0]?.getBoundingClientRect().width +
150;
if (mySvgHeight && mySvgWdith) {
document.getElementById("mySvg").setAttribute("height", mySvgHeight);
document.getElementById("mySvg").setAttribute("width", mySvgWdith);
}
}, },
/** 更新图形 */ /** 更新图形 */