简体   繁体   English

D3:树形布局上的约束语义缩放

[英]D3: Constrained Semantic Zooming on a Tree Layout

I'm trying to apply constrained and semantic zoom behavior based on these two examples: Constrained Zoom and SVG Semantic Zooming , on my tree visualization. 我正在尝试基于这两个示例应用约束和语义缩放行为: 约束缩放SVG语义缩放 ,在我的树可视化中。 I might have been applying the constrained zoom part correctly, but it's getting complicated and fishy when I tried to integrate the semantic zooming part: The cursor doesn't stay on the particular node which I zoom to. 可能已经正确地应用了受约束的缩放部分,但是当我尝试集成语义缩放部分时它变得复杂和腥:光标不会停留在我缩放到的特定节点上。 I'm starting to think that I should only choose either constrained or semantic zooming, but not both. 我开始认为我应该只选择约束或语义缩放,但不能同时选择两者。 Is it actually possible to combine the two (constrained + semantic zooming) on a tree layout? 实际上是否可以将两者(约束+语义缩放)组合在树形布局上?

Here is my effort so far: http://jsfiddle.net/glenn/GpjFN/ . 这是我到目前为止的努力: http//jsfiddle.net/glenn/GpjFN/

The relevant code for the zooming: 缩放的相关代码:

...

var zoom = d3.behavior.zoom()
    .scaleExtent([1, 5])
    .on('zoom', move);

function move() {
    var t = d3.event.translate,
        s = d3.event.scale,
        width = viewportSize[0],
        height = viewportSize[1];

    t[0] = Math.min(width * (s - 1), Math.max(width * (1 - s), t[0]));
    t[1] = Math.min(height * (s - 1), Math.max(height * (1 - s), t[1]));

    zoom.translate(t);

    viewport.attr('transform',
        'translate(' + t + ')' + ' scale(' + s + ')');

    // TODO: ???
};

svg.call(zoom);

The problem is that you have several nested g elements with translations applied. 问题是你有几个嵌套的g元素应用了翻译。 That is, any translation you apply to the viewport element is relative to the translation you have applied to the parent g element. 也就是说,适用于任何翻译viewport元素相对于已应用到父翻译g元素。 The event coordinates ( d3.event ) are determined by the absolute mouse position however. 但事件坐标( d3.event )由绝对鼠标位置确定。 What you're seeing is the offset between the two g elements when zooming in -- that's by how much the position is shifted. 您所看到的是放大时两个g元素之间的偏移 - 即位置偏移的位置。

There's a simple fix though, simply add this offset to the even translation coordinates in your move function: 虽然有一个简单的解决方法,只需将此偏移量添加到move函数中的偶数平移坐标:

t[0] += translationVector[0];
t[1] += translationVector[1];

This will transform the absolute event coordinates into relative container coordinates. 这会将绝对事件坐标转换为相对容器坐标。 Complete example here . 在这里完成示例。

Oh and in principle there's no restriction on how you can combine different things, so yes, constrained and semantic zoom together are certainly possible. 哦,原则上对于如何组合不同的东西没有限制,所以是的,约束和语义缩放在一起肯定是可能的。

Edit: 编辑:

There's quite a bit of translation and offsetting going on in your fiddle. 你的小提琴中有相当多的翻译和抵消。 Much of it is nested, which makes it difficult to use the absolute coordinates from the event. 其中大部分是嵌套的,这使得难以使用事件的绝对坐标。 I've rewritten parts of your code to remove the nested offsets, which makes it much easier to handle and also removes the need to add the offsets as above. 我已经重写了部分代码来删除嵌套的偏移量,这使得它更容易处理,并且无需像上面那样添加偏移量。 I've also fixed the functions that constrain the translation. 我还修复了限制翻译的功能。

Complete code here . 完整代码在这里

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

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