javascript - D3 多线图表与工具提示转换问题

标签 javascript d3.js transition

我一直在使用 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/

相关文章:

javascript - 如何加快浏览器的滚动速度?

vue.js - D3更新条形图VUE.js

css - 如何在没有硬编码高度的情况下使用 Vue.js 转换?

javascript - 不变性应该抛出错误,但它不会在字符串中

javascript - p5.j​​s 我如何让一条线在两个方向上无限延伸

javascript - 在加载时对 D3 热图单元进行动画处理?

javascript - 将字符串时间转换为毫秒

css 简单悬停淡入淡出

javascript - 我如何在第一次迭代时反转 CSS3 动画(而不是第二次迭代)

javascript - 无法使用 Angular 从 HTML View 获取值到 Controller 。解决方案返回$scope变量未定义