具有工具提示转换问题的D3多线图

问题描述 投票:3回答:1

我一直在使用d3创建一个带有焦点和上下文刷的多线图。一切进展顺利,除了在转换过程中,数据点处的点与工具提示一起移动到完全错误的位置。我无法弄清楚造成这种情况的原因。任何帮助将非常感激。我在这里附上完整的代码,并在图表上注明我非常肯定该bug应该是:

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);
    });

非常感谢你。

javascript d3.js transition
1个回答
0
投票

工具提示是什么意思?它是我们悬停在点上时出现的窗口吗?他们似乎很好。我可以看到的是,你的点不会在线条移动时,如果我不得不猜测我会说你的输入和更新选择是混合的。如果点已经在屏幕上并且你想要更新它们的位置(通过调用你的方法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);
    })
    [...]
© www.soinside.com 2019 - 2024. All rights reserved.