【问题标题】:D3 v5 zoom limit panD3 v5 缩放限制平移
【发布时间】:2019-01-15 10:24:59
【问题描述】:

我试图限制 d3 缩放中的平移,但我没有得到正确的结果。我正在使用以下代码来扩展scaletranslate

var treeGroup = d3.select('.treeGroup');
var rootSVG = d3.select('.rootSVG')
var zoom = d3.zoom()
     .scaleExtent([1.6285, 3])
     .translateExtent([[0, 0],[800, 600]])
     .on('zoom', function(){
           treeGroup.attr('transform', d3.event.transform);
     })

rootSVG.call(zoom);

这里是 JSFiddle:https://jsfiddle.net/nohe76yd/45/

scaleExtent 工作正常,但 translateExtent 出现问题。如何为 translateExtent 指定正确的值,以便在平移内容时始终保留在 svg 容器内?

【问题讨论】:

标签: d3.js


【解决方案1】:

translateExtent 在动态用于您正在使用的图形组时效果最佳。它有两个参数:topLeftbottomRight,分别是 x 和 y 坐标。

在我的示例中,我在 getBBox() 的帮助下根据图形的大小重新计算范围并添加了一些边距。看看吧,或许对你有帮助:https://bl.ocks.org/agnjunio/fd86583e176ecd94d37f3d2de3a56814

编辑:在缩放功能中添加执行此操作的代码,以便于阅读。

// Define some world boundaries based on the graph total size
// so we don't scroll indefinitely
const graphBox = this.selections.graph.node().getBBox();
const margin = 200;
const worldTopLeft = [graphBox.x - margin, graphBox.y - margin];
const worldBottomRight = [
    graphBox.x + graphBox.width + margin,
    graphBox.y + graphBox.height + margin
];
this.zoom.translateExtent([worldTopLeft, worldBottomRight]);

【讨论】:

  • 我正在尝试这样做,以避免向左(x 轴)无限平移,但 .getBBox() 返回所有零值。 x,y,宽度和高度。你能帮忙吗
猜你喜欢
  • 2012-05-12
  • 2013-05-16
  • 1970-01-01
  • 2019-07-25
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 2017-01-29
相关资源
最近更新 更多