【发布时间】:2014-04-20 11:58:26
【问题描述】:
我在平移时使用了变换,从几个示例中复制而来。
zoom = d3.behavior.zoom()
.x(this.xScale)
.scaleExtent([0.5, 2])
.on("zoom", zoomFunction(this))
.on("zoomend", zoomEndFunction(this));
svg = histogramContainer.append("svg")
.attr('class', 'chart')
.attr('width', width)
.attr('height', height)
.call(zoom)
.append('g')
.attr('transform', 'translate(' + this.margin.left + ' , ' +
(height - this.margin.bottom) + ')');
function zoomFunction(scope) {
return function() {
var that = scope;
that.xDelta = d3.event.translate[0];
that.zoomScale = d3.event.scale;
// some other code removed for simplicity
svg.selectAll(".stackedBar").attr("transform", "translate(" +
that.xDelta + ",0)scale(" +
that.zoomScale + ", 1)");
};
}
问题在于,由于新元素在 之后进入 pan 然后“旧”元素应用了 transform 属性,但新元素没有。
这会破坏未来的平移,因为旧元素将从缩放前 xScale 绘制它们的位置转换,而新元素将从缩放调整后的 xScale 转换。
在我看来,我可以使用缩放调整后的 xScale 重绘旧元素,但我不确定何时以及如何在“幕后”执行此操作。
或者,我可以使用旧的 xScale 绘制新元素,并对它们应用与旧元素相同的变换。这似乎更混乱,因为元素会来来去去,我必须跟踪“当前变换”。我的直觉告诉我“状态太多”。
【问题讨论】:
标签: d3.js