【问题标题】:d3 v4 tooltip passing data d issued3 v4 工具提示传递数据 d 问题
【发布时间】:2017-08-02 13:42:28
【问题描述】:

我试图让文本显示在 x 轴上名称的鼠标悬停上。这个stackoverflow post 几乎到了那里,除了我无法将数据传递给鼠标悬停时调用的函数。

// Add the X Axis
  svg.append("g")
    .attr("class", "axis")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(x))
    .selectAll("text")
    .style("text-anchor", "end")
    .style("font", "7px times")
    .attr("dx", "-.8em")
    .attr("dy", ".15em")
    .attr("transform", "rotate(-65)")
    .on("mouseover", function(d) {
      return tooltip.style("visibility", "visible")
    })
    .on("mouseout", function(d) {
      return tooltip.style("visibility", "hidden");
    });

  var tooltip = d3.select("#info")
    .append("div")
    .style("position", "absolute")
    .style("z-index", "10")
    // .style("visibility", "hidden")
    .text("a simple tooltip" + d.name);

当我删除 d.name 时,它​​可以工作。我无法将 d 传递给工具提示。

也许我需要使用我尝试过的 .data(data).enter() 但也许我没有正确使用它。

谢谢,

https://d3js.org/d3.v4.min.js 是我使用的唯一库。

【问题讨论】:

    标签: d3.js text tooltip mouseover


    【解决方案1】:

    您可以在鼠标悬停回调中设置文本,您可以访问d吗?

    // Add the X Axis
    svg.append("g")
        .attr("class", "axis")
        .attr("transform", "translate(0," + height + ")")
        .call(d3.axisBottom(x))
        .selectAll("text")
        .style("text-anchor", "end")
        .style("font", "7px times")
        .attr("dx", "-.8em")
        .attr("dy", ".15em")
        .attr("transform", "rotate(-65)")
        .on("mouseover", function(d) {
            return tooltip
                .style("visibility", "visible")
                .text("a simple tooltip" + d.name);
        })
        .on("mouseout", function(d) {
            return tooltip.style("visibility", "hidden");
        });
    
    var tooltip = d3.select("#info")
        .append("div")
        .style("position", "absolute")
        .style("z-index", "10")
        // .style("visibility", "hidden")
        // .text("a simple tooltip" + d.name);
    

    【讨论】:

    • 啊,我以为我真的试过了。有用。谢谢
    • 我没有使用“返回工具提示...”
    • 但我仍然不明白为什么我不能通过函数传递“d”。
    • 通过什么函数?这里唯一的功能是鼠标悬停处理程序对吗?创建工具提示的位不是函数,该代码在调用任何鼠标悬停处理程序之前运行,因此此时无法访问“d”。
    • 我的意思是为什么 '.text("a simple tooltip" + d.name);' 中不能有 'd.name'被显示?就好像 d 被传递给 function(d) 并且该函数返回工具提示。但工具提示使用 d.name。似乎 d 并没有考虑用作 d.name 的工具提示,我可能完全理解错了。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多