javascript - d3.js 表格工具提示不会消失

标签 javascript d3.js tooltip

现在,当我将鼠标悬停在共现矩阵中的某个元素上时(类似于 mike bostick 的《悲惨世界》示例),会出现一个带有信息表的工具提示。要生成表格并将其附加到 div 工具提示,我使用以下代码:

function tabulate(data, columns) {
  var table = div.append("table")
      thead = table.append("thead"),
      tbody = table.append("tbody");

  // append the header row
  thead.append("tr")
      .selectAll("th")
      .data(columns)
      .enter()
      .append("th")
          .text(function(column) { return column; });

  // create a row for each object in the data
  var rows = tbody.selectAll("tr")
      .data(data)
      .enter()
      .append("tr");

  // create a cell in each row for each column
  var cells = rows.selectAll("td")
      .data(function(row) {
          return columns.map(function(column) {
              return {column: column, value: row[column]};
          });
      })
      .enter()
      .append("td")
          .text(function(d) { return d.value; });

  return table;

}

这工作得很好,每次我将鼠标悬停在元素上时都会生成表格。我的mousemove、mouseover和mouseout函数如下:

function mousemove(d) {
    tabulate(nodes[d.x].tableInfo[d.y], ['site', 'win1', 'bid1', 'win2', 'bid2'])
    div
      .style("left", (d3.event.pageX) + "px")
      .style("top", (d3.event.pageY) + "px" )
}

function mouseover(p) {
    div.transition()
        .duration(300)
        .style("opacity", 1);
    d3.selectAll(".row text").classed("active", function(d, i) { return i == p.y; });
    d3.selectAll(".column text").classed("active", function(d, i) { return i == p.x; });
}

function mouseout() {
    div.transition()
        .duration(300)
        .style("opacity", 1e-6);
    d3.selectAll("text").classed("active", false);
}

现在我遇到的问题是,每次我将鼠标悬停在该元素上时,都会再次生成表格,并且旧表格不会被删除。有什么办法可以在鼠标移开时删除旧元素吗?谢谢。

最佳答案

只需在添加新表之前删除旧表即可:

function tabulate(data, columns) {
  div.select("table").remove();
  var table = div.append("table"),
  ect

关于javascript - d3.js 表格工具提示不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176584/

相关文章:

javascript - 如何在 ExtJS 中为树列表设置工具提示?

winapi - 对话框显示工具提示,但子窗口不显示工具提示

javascript - 使用 JS 或 Jquery 在浏览器窗口中禁用声音

javascript - 从 Javascript 中的数组中删除重复项(不修改数组)——理解背后的逻辑

javascript - 来自 json 导出网格的具有 png alpha 透明度的平面 Material 不起作用

javascript - 在给定已知索引范围的日期之间进行插值

javascript - SVG 文本(带 tspan)垂直偏移

javascript - D3.js 动画更新

javascript - d3.js 圆环图::渲染其他路径但未更新

javascript - getBoundingClientRect 给出了错误的值