feat: base 自适应容器和自动缩放
This commit is contained in:
parent
3706d30b82
commit
f84cfb334e
|
@ -1,5 +1,5 @@
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div style="width:100%;height:100%" id="svgContainer">
|
||||||
<svg id="mySvg"></svg>
|
<svg id="mySvg"></svg>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
@ -133,10 +133,23 @@ export default {
|
||||||
// 渲染图
|
// 渲染图
|
||||||
this.render(this.inner, this.g);
|
this.render(this.inner, this.g);
|
||||||
|
|
||||||
// 自动计算 viewBox 尺寸
|
// 获取 svg 的宽高
|
||||||
const { width, height } = this.g.graph();
|
const svgWidth = document.getElementById("svgContainer").clientWidth;
|
||||||
|
const svgHeight = document.getElementById("svgContainer").clientHeight;
|
||||||
|
|
||||||
d3.select("#mySvg")
|
d3.select("#mySvg")
|
||||||
.attr("viewBox", `0 0 ${width + 2} ${height + 2}`);
|
.attr("viewBox", `0 0 ${svgWidth} ${svgHeight}`);
|
||||||
|
|
||||||
|
const graphHeight = this.g.graph().height;
|
||||||
|
const graphWidth = this.g.graph().width;
|
||||||
|
|
||||||
|
// ===== 自动缩放和居中 =====
|
||||||
|
const scale = Math.min(svgWidth / graphWidth, svgHeight / graphHeight);
|
||||||
|
|
||||||
|
const translateX = (svgWidth - graphWidth * scale) / 2;
|
||||||
|
const translateY = (svgHeight - graphHeight * scale) / 2;
|
||||||
|
|
||||||
|
this.inner.attr("transform", `translate(${translateX},${translateY}) scale(${scale})`);
|
||||||
},
|
},
|
||||||
|
|
||||||
/** 更新图形 */
|
/** 更新图形 */
|
||||||
|
|
|
@ -65,7 +65,6 @@ const init = async () => {
|
||||||
});
|
});
|
||||||
nodes.value = nodes_;
|
nodes.value = nodes_;
|
||||||
edges.value = edges_;
|
edges.value = edges_;
|
||||||
console.log("x", nodes.value, edges.value)
|
|
||||||
}
|
}
|
||||||
defineExpose({ init })
|
defineExpose({ init })
|
||||||
</script>
|
</script>
|
Loading…
Reference in New Issue