我一直在使用 d3 创建带有焦点和上下文刷的多线图表。一切都很顺利,除了在过渡时带有工具提示的数据点处的点移动到完全错误的位置。我不知道是什么原因造成的。任何帮助将非常感激。我在此处附加了完整的代码,并在图表上注明了我很确定错误应该是:
http://jsbin.com/osumaq/20/edit
单击按钮时,一个新的 json 会传递到图表中进行读取。
我认为有问题的代码块是这样的:
topicEnter.append("g").selectAll(".dot")
.data(function (d) { return d.values }).enter().append("circle").attr("clip-path", "url(#clip)")
.attr("stroke", function (d) {
return color(this.parentNode.__data__.name)
})
.attr("cx", function (d) {
return x(d.date);
})
.attr("cy", function (d) {
return y(d.probability);
})
.attr("r", 5)
.attr("fill", "white").attr("fill-opacity", .5)
.attr("stroke-width", 2).on("mouseover", function (d) {
div.transition().duration(100).style("opacity", .9);
div.html(this.parentNode.__data__.name + "<br/>" + d.probability).style("left", (d3.event.pageX) + "px").style("top", (d3.event.pageY - 28) + "px").attr('r', 8);
d3.select(this).attr('r', 8)
}).on("mouseout", function (d) {
div.transition().duration(100).style("opacity", 0)
d3.select(this).attr('r', 5);
});
非常感谢。
最佳答案
工具提示是什么意思?这是当我们将鼠标悬停在点上时出现的窗口吗?他们看起来很好。我看到的是,你的点没有移动,而线条却在移动,如果我不得不猜测,我会说你的输入和更新选择是混合的。如果这些点已经在屏幕上并且您想要更新它们的位置(通过调用您的方法update
),您应该有以下内容:
// Bind your data
topicEnter.append("g").selectAll(".dot")
.data(function (d) { return d.values })
// Enter selection
topicEnter.enter().append("circle").attr("clip-path", "url(#clip)").attr("class", "dot");
// Update all the dots
topicEnter.attr("stroke", function (d) {
return color(this.parentNode.__data__.name)
})
.attr("cx", function (d) {
return x(d.date);
})
.attr("cy", function (d) {
return y(d.probability);
})
[...]
关于javascript - D3 多线图表与工具提示转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18458007/