【问题标题】:Why are my tooltips in d3 not updating correctly?为什么我在 d3 中的工具提示没有正确更新?
【发布时间】:2014-01-30 19:50:27
【问题描述】:

我正在使用这个jsfiddle。当我将鼠标悬停在条上时,所有工具提示最初都是正确的。但是当我单击 weekview 按钮更改图表时,工具提示不会更新。

我认为问题出在这部分:

layer.enter()
    .append("g")
    .attr("class", "layer");

layer.attr("fill", function (d, i) {
    return color(i);
    })
.append("svg:title")
.text(function(d){  
    return d[0].s;
});

layer.exit()
    .remove();

附加文本是我添加工具提示的地方。我认为进入和退出会刷新栏并因此刷新工具提示,但它没有正确执行。

当我的图表发生变化时如何更新工具提示?

【问题讨论】:

  • 我在你的 jsfiddle 中没有得到任何工具提示。
  • 我在 Chrome 和 FF 中都获得了工具提示。
  • 我确实看到,在单击按钮 WeekView 后,工具提示显示错误的文本。

标签: javascript d3.js tooltip stacked-chart


【解决方案1】:

这是因为每次条形更改时您都在附加一个新的<title> 元素。追加应该在输入选择中完成一次,然后只需在更新选择中更新标题的值。

这是您的代码的修改版本,其中包含一些内联 cmets(我已删除与工具提示无关的部分):

layer.enter()
    .append("g")
    .attr("class", "layer")
        .append("title");  // add new element under new layer

// add or update the value of the title element
layer.select("title").text(function(d) {  
    return d[0].s;
});

【讨论】:

  • @Grammin 也试试这个。在 DOM 检查器中查看 SVG 的标题标签。并反复点击WeekView。您将看到所有多余的 HTML 标记都被附加。那是你的错误,这个答案应该可以解决它。
猜你喜欢
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多