简体   繁体   English

D3 可折叠树 - 缩放时跳动

[英]D3 collapsible tree - jumpy on zoom

I've already spent too much time trying to figure this out.我已经花了太多时间试图弄清楚这一点。 My goal is to create d3 collapsible tree but for some reason when you zoom it, it moves the tree on position 0,0.我的目标是创建 d3 可折叠树,但是由于某种原因,当您缩放它时,它会将树移动到位置 0,0。 I've already seen a few questions with similar problem such as this one d3.behavior.zoom jitters, shakes, jumps, and bounces when dragging but can't figure it out how to apply it to my situation.我已经看到了一些具有类似问题的问题,例如拖动时 d3.behavior.zoom 抖动、抖动、跳跃和弹跳,但不知道如何将其应用于我的情况。

I think this part is making a problem but I'm not sure how to change it to have the proper zooming functionality.我认为这部分有问题,但我不确定如何更改它以具有适当的缩放功能。

d3.select('g').transition()
    .duration(duration)
    .attr("transform", "translate(" + x + "," + y + ")scale(" + scale + ")")
zoomListener.scale(scale);

Here is my code: https://jsfiddle.net/ramo2600/y79r5dyk/11/这是我的代码: https : //jsfiddle.net/ramo2600/y79r5dyk/11/

You are translating your zoomable g to position [100,100] but not telling the zoom d3.behavior.zoom() about it.您正在将可缩放的g转换为位置[100,100]但没有告诉缩放d3.behavior.zoom() So it starts from [0,0] and you see the "jump".所以它从[0,0]开始,你会看到“跳跃”。

Modify your centerNode function to:将您的centerNode函数修改为:

function centerNode(source) {
    scale = zoomListener.scale();
    // x = -source.y0;
    y = -source.x0;
    // x = x * scale + viewerWidth / 2;
    x = 100;
    y = 100;
    // y = y * scale + viewerHeight / 2;
    d3.select('g').transition()
        .duration(duration)
        .attr("transform", "translate(" + x + "," + y + ")scale(" + scale + ")")
    zoomListener.scale(scale);
    zoomListener.translate([x,y]); //<-- tell zoom about position
}

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

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