feat: base 自适应容器和自动缩放

This commit is contained in:
caoqianming 2025-09-29 10:46:07 +08:00
parent 3706d30b82
commit f84cfb334e
2 changed files with 17 additions and 5 deletions

View File

@ -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})`);
}, },
/** 更新图形 */ /** 更新图形 */

View File

@ -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>