我想在 D3.js 的路径上触发 事件。这是一个工作示例:http://jsfiddle.net/kwoxer/kpL1uyy2/
那么我怎么能说zoomIntoArea函数触发了一个特定的路径。我的意思是最后我有一些pathes,我想在启动时加载一个特定而不点击它。我已经尝试过了:
zoomIntoArea(d3.select("lines"))
和其他一些人,但肯定不会给我正确的元素。
答案 0 :(得分:2)
您不需要缩放行为来明确缩放到路径,例如:
var bounds = path.bounds(d),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
x = (bounds[0][0] + bounds[1][0]) / 2,
y = (bounds[0][1] + bounds[1][1]) / 2,
scale = .9 / Math.max(dx / width, dy / height),
translate = [
width / 2 - scale * x,
height / 2 - scale * y];
svg.transition()
.duration(750)
.attr("transform", "translate(" +
translate + ")scale(" +
scale + ")");