feat: base 流程图边的处理
This commit is contained in:
parent
72339aa0f4
commit
a212337b3a
|
@ -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}`);
|
||||
},
|
||||
|
||||
/** 更新图形 */
|
||||
|
|
Loading…
Reference in New Issue