Compare commits

..

No commits in common. "3706d30b82956bdef4220f2210d8eeb3176b26f0" and "a722292c57f218a26b1d76847acaac5f7b74bca2" have entirely different histories.

1 changed files with 31 additions and 42 deletions

View File

@ -61,15 +61,9 @@ export default {
}); });
this.render = new dagreD3.render(); this.render = new dagreD3.render();
let svg = d3.select("#mySvg").attr("preserveAspectRatio", "xMidYMid meet"); let svg = d3.select("#mySvg");
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);
}, },
@ -77,7 +71,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));
@ -97,21 +91,11 @@ export default {
} }
}); });
const edgeGroups = {}; // key
that.edges.forEach((edge) => { that.edges.forEach((edge, index) => {
const key = `${edge.source}_${edge.target}`; let edgeColor = "green"; // 绿
if (!edgeGroups[key]) edgeGroups[key] = []; if (edge.label === "驳回") edgeColor = "red";
edgeGroups[key].push(edge); if (edge.label === "退回") edgeColor = "red";
});
// 线
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( that.g.setEdge(
edge.source, edge.source,
@ -119,24 +103,29 @@ export default {
{ {
label: edge.label, label: edge.label,
id: edge.id, id: edge.id,
// style: `fill:#ffffff;stroke:${edgeColor};stroke-width:1.5px`,
curve: curves[index % curves.length], curve: d3.curveBasis,
labeloffset: 16 + index * 10, // minlen: 1 + index % 3,
minlen: 2 + index, // dagre
style: `fill:none;stroke:${edgeColor};stroke-width:1.5px`,
}, },
`${edge.source}_${edge.target}_${edge.id}` `${edge.source}_${edge.target}_${edge.id}`
); );
}); });
});
// //
this.render(this.inner, this.g); this.render(this.inner, this.g);
// viewBox // SVG
const { width, height } = this.g.graph(); let mySvgHeight =
d3.select("#mySvg") document.getElementsByClassName("nodes")[0]?.getBoundingClientRect().height +
.attr("viewBox", `0 0 ${width + 2} ${height + 2}`); 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);
}
}, },
/** 更新图形 */ /** 更新图形 */