【问题标题】:How to add multiple tool tips to a bar chart如何向条形图添加多个工具提示
【发布时间】:2017-08-12 09:39:25
【问题描述】:

我一直在使用带有内置工具提示的 D3 svg 图表(我相信使用 d3-tip 库)。原代码可以看这里:http://bl.ocks.org/Caged/6476579

我已经成功地对其进行了大部分自定义,但是我需要添加第二个鼠标悬停效果;这样当您将鼠标悬停在一个条上时,它不仅会打开工具提示,还会打开图表右上角的另一个提示或图层。

我尝试执行以下操作,但似乎不起作用。我只是在语法中遗漏了一些东西吗?还是这个策略根本行不通?

var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-10, 0])
  .html(function(d) {
    return "<strong>Frequency2:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
  })

//adding second tool tip
var tip2 = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-40, 0])
  .html(function(d) {
    return "<strong>Other Variables:</strong> <span style='color:red'>" + d.frequency2 + "</span>";
  })

然后

  svg.selectAll(".bar")
      .data(data)
    .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) { return x(d.letter); })
      .attr("width", x.rangeBand())
      .attr("y", function(d) { return y(d.frequency2); })
      .attr("height", function(d) { return height - y(d.frequency2); })
      .on('mouseover', tip.show, tip2.show)
      .on('mouseout', tip.hide, tip2.hide);

图表仍然有效,但现在没有显示任何工具提示。

【问题讨论】:

    标签: javascript d3.js svg mouseover


    【解决方案1】:

    你现在在做什么......

    .on('mouseover', tip.show, tip2.show)
    

    ... 将无法正常工作。在selection.on 中,第三个参数是capture

    selection.on(typenames[, listener[, capture]])

    因此,解决方案是将tiptip2 包装在另一个函数中:

    .on("mouseover", function() {
        tip.show();
        tip2.show();
    })
    

    这是一个演示(将鼠标悬停在圆圈上):

    var svg = d3.select("svg");
    
    var tip = d3.tip()
      .attr('class', 'd3-tip')
      .offset([-10, 20])
      .html("<strong>Frequency2:</strong> <span style='color:red'>foo</span>")
    
    var tip2 = d3.tip()
      .attr('class', 'd3-tip')
      .offset([90, 20])
      .html("<strong>Other Variables:</strong> <span style='color:red'>bar</span>")
    
    svg.call(tip)
    svg.call(tip2)
    
    d3.select("circle").on("mouseover", function() {
      tip.show();
      tip2.show();
    }).on("mouseout", function() {
      tip.hide();
      tip2.hide();
    });
    .d3-tip {
      line-height: 1;
      font-weight: bold;
      padding: 12px;
      background: rgba(0, 0, 0, 0.8);
      color: #fff;
      border-radius: 2px;
    }
    <script src="https://d3js.org/d3.v3.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.min.js"></script>
    <svg>
      <circle cx="50" cy="70" r="20" fill="teal"></circle>
    </svg>

    【讨论】:

    • 谢谢杰拉尔多。似乎它应该可以工作,但我遇到了一个奇怪的错误,并且没有显示任何工具提示。我看到:Uncaught TypeError: Cannot read property 'frequency2' of undefined at Function.&lt;anonymous&gt; (svg7-2.html:105) at Function.tip.show (d3.tip.v0.6.3.js:33) at SVGRectElement.&lt;anonymous&gt; (svg7-2.html:152) at SVGRectElement.__onmouseover (d3.v3.min.js:1)
    • 你可以看到我的 sn-p 工作。所以,你有一个不同的问题,与你提出的问题无关。
    • 是的,你的例子绝对有效!是否有可能在执行时无法访问数据?我相信错误表明它当时无法读取 frequency2 变量。正确的?您是否可以尝试使用包含数据而不是您在其位置插入的文本的示例,看看它是否仍然有效?如果有帮助,我也可以发布我的完整代码!
    • 请不要编辑这个问题:这个问题已经被回答了。用你的问题发布另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    相关资源
    最近更新 更多