简体   繁体   English

D3.js树布局画布调整大小

[英]D3.js Tree layout canvas resize

This is a continuation of my efforts to build a collapsible tree layout using d3.js. 这是我使用d3.js构建可折叠树布局的工作的延续。

Generate (multilevel) flare.json data format from flat json 从平面JSON生成(多级)flare.json数据格式

The layout looks like: ( http://bl.ocks.org/mbostock/raw/4339083/ ) with around 3k nodes and depth of some nodes around 25. The current size of the canvas I need to set is 8000px width and 8000px height in order that all nodes are visible which I know is not reasonable when the number of tree levels rendered is 2 or 3. Furthermore, I intend to make this code reusable with other trees that maybe smaller/larger in size based on what data source(json file) is selected. 布局看起来像:( http://bl.ocks.org/mbostock/raw/4339083/ )具有大约3k节点,一些节点的深度大约为25。我需要设置的画布的当前大小是8000px宽度和8000px高度,以使所有节点可见,当渲染的树级别数为2或3时,我知道这是不合理的。此外,我打算根据什么数据源使此代码可与其他大小可能更大/更大的树一起使用。 (json文件)已选择。

So I was wondering if it is possible to resize the canvas size relative to the positions of the nodes/ number of nodes shown on screen. 因此,我想知道是否有可能相对于屏幕上显示的节点位置/节点数来调整画布大小。 This way, the code would be much more systematic and adaptable. 这样,代码将更具系统性和适应性。

I saw this: 我看见了这个:

Dynamically resize the d3 tree layout based on number of childnodes 根据子节点数量动态调整d3树布局的大小

but this resizes the tree, which if you can imagine in a case of tree with around 3k nodes, makes it hard to read and comprehend. 但这会调整树的大小,如果您可以想象在大约3k个节点的树的情况下,将很难阅读和理解。

I know this might not even be related to d3.js but I tagged it to explain my issue and bring in d3 experts too who might have faced a similar condition. 我知道这可能甚至与d3.js无关,但我标记了它来解释我的问题,并也请了可能面临类似情况的d3专家。

I am also attempting to filter out uninformative nodes based on my criteria so as to render less number of nodes than the actual data. 我还尝试根据我的标准过滤掉无信息的节点,以使渲染的节点数少于实际数据。 (I know i will run into performance issues with larger trees). (我知道我会遇到大树的性能问题)。 Any help would be much appreciated. 任何帮助将非常感激。

NOTE: When I say canvas, I mean the area on which the tree is drawn and not the "canvas". 注意:当我说画布时,我指的是绘制树的区域,而不是“画布”。 I am not familiar with the jargon so kindly read accordingly. 我对行话不熟悉,因此请相应地阅读。

I was facing the similar problem and now I have find out a solution. 我面临着类似的问题,现在我找到了解决方案。 Check on this link. 检查此链接。 D3 collapsible tree, node merging issue D3可折叠树,节点合并问题

Hope this helps someone. 希望这对某人有帮助。

I faced similar problems also using the flare tree code as a base for what I was building and the suggested links did not seem to account for a lot of variance in node structure? 使用flare树代码作为我正在构建的基础时,我也遇到了类似的问题,建议的链接似乎没有说明节点结构的大量差异? I have many trees to display with a dynamic number of nodes and structuring. 我要显示很多树,其中包含动态数量的节点和结构。 This solution worked for me: 此解决方案为我工作:

Concerning height: After observing the translate offsets per node, I learned that dx (vs dy, as tree is flipped) is an offset from the root node, with the nodes above root going negative and those below going positive. 关于高度:在观察每个节点的平移偏移量之后,我了解到dx(相对于dy,因为树被翻转)是距根节点的偏移量,根上方的节点变为负,而下方的节点变为正。 So, I "calculated" the max offset in both directions each time a node is appended, then with that information, adjusted the canvas height and the view translation (starting point of root). 因此,每次添加节点时,我都会“计算”两个方向上的最大偏移,然后利用该信息调整画布高度和视图转换(根的起点)。

For width: max d.depth * by the (y length normalization the code uses) + margins 对于宽度:最大d.depth *由(代码使用的y长度归一化)+边距

 let aboveBount = 0 let belowBound = 0 let maxDepth = 0 nodeEnter.each( (d) => { if( Math.sign(dx) === -1 && dx < boundAbove) { boundAbove = dx } if( Math.sign(dx) === 1 && dx > boundBelow) { boundBelow = dx } if( d.depth > maxDepth){ maxDepth = d.depth } }) const newHeight = boundBelow + Math.abs(boundAbove) + nodeHeight*2 + margin.top*2 svg.style('height', newHeight) svg.style('width'. maxDepth*180 + margin.left*2) //180 was the amount set to normailze path length svg.attr('transform', `translate(${margin.left}, ${Math.abs(boundAbove) + margin.top})`) 

Well, best wishes and happy coding! 好吧,最良好的祝愿和快乐的编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM