admin管理员组文章数量:1310074
I'm trying to create DAGs with dagre-d3. The data for these DAGs es from a database, are different for each DAG and as such, I do not know the width/height to give the containing svg before adding all nodes and edges to the graph.
So ideally I'd call something like d3.select("#svg1").resize_to_match_contents()
after adding all nodes and edges to make sure all nodes are visible and the svg isn't too large. Of course there is no such function, and I have no idea how to implement it. I know I can call d3.select("#svg1").attr("height", "10")
to set the height, but have no idea how to retreive/pute what the height of the <g>
element inside the SVG is.
I'm trying to create DAGs with dagre-d3. The data for these DAGs es from a database, are different for each DAG and as such, I do not know the width/height to give the containing svg before adding all nodes and edges to the graph.
So ideally I'd call something like d3.select("#svg1").resize_to_match_contents()
after adding all nodes and edges to make sure all nodes are visible and the svg isn't too large. Of course there is no such function, and I have no idea how to implement it. I know I can call d3.select("#svg1").attr("height", "10")
to set the height, but have no idea how to retreive/pute what the height of the <g>
element inside the SVG is.
1 Answer
Reset to default 8The internal and external dimensions of SVG can be independently controlled using the viewBox attribute. When you talk about finding the size of the g
element, it only makes sense if there there is no viewBox
defined and there are no other scale transformations anywhere.
Here is how I would suggest you do it:
- Set the
width
/height
of thesvg
element to thewindow.innerWidth
andwindow.innerHeight
, or to the maximum dimension you want it to grow to. - Set the
viewBox
attribute to something like"0 0 100 100"
and then define all scales in your code to have the domain[0, 100]
. This will ensure that the graph shrinks to fit in your designated area.
Also, take a look at how nvd3
handles window resizes, and how to maintain the coordinate system inside the SVG element upon resize using preserveAspectRatio.
From what I could glean from their webpage, darge-d3 does not allow for setting the layout options to limit the render size explicitly and does not return the maximum dimensions it used. One could, however, use getBBox()
to get the dimensions of the g
box where it rendered and try to set the viewBox
such that it just fits the SVG (or set the height
, width
on the SVG to match the dimension for the g
1:1, but that could lead to a broken layout.
本文标签: javascriptResizing d3dagred3 svg to show all contentsStack Overflow
版权声明:本文标题:javascript - Resizing d3dagre-d3 svg to show all contents - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741836698a2400260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论