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