【问题标题】:migration from v3 to v4 - tooltip not showing in d3从 v3 迁移到 v4 - 工具提示未显示在 d3 中
【发布时间】:2017-11-26 23:07:17
【问题描述】:

我使用this 参考创建了一个散点图。该代码在 v3 中运行良好,但在将其更改为 v4 后,工具提示、x 轴和 y 轴不显示。

我创建了一个Plunker。如果有人能告诉我它有什么问题,我将不胜感激。

function showTooltip (d, i) {

    //Save the chosen circle (so not the voronoi)
    var element = d3.selectAll(".countries."+d.CountryCode);

    //Define and show the tooltip
    $(element).popover({
        placement: 'auto top',
        container: '#chart',
        trigger: 'manual',
        html : true,
        content: function() {
            return "<span style='font-size: 12px; text-align: center;'>" + "<span>" + "Speaker: " + "</span>" + d.Speaker + "\n"
            + "<span>" + "Duration: " + "</span>" + Math.round(d.Duration) + "\n" +"</span>"; }
    });
    $(element).popover('show');

    //Make chosen circle more visible
    element.style("opacity", 1)
        .style("stroke-width", 6);

更新

This 是 v4 d3 中此散点图的更正版本。我在 Lary 的帮助下更正了它。

【问题讨论】:

    标签: javascript d3.js visualization data-visualization


    【解决方案1】:

    为:

    x轴和y轴不显示

    只需删除:

     .axis .domain {
          display: none;
        }
    

    来自风格

    为:

    工具提示,线条

    showtooltip 函数内部的变化:

    var element = d3.select(".countries."+d.CountryCode);
    
        //Define and show the tooltip
        $(element.node()).popover({
            placement: 'auto top',
            container: '#chart',
            trigger: 'manual',
            html : true,
            content: function() {
                return "<span style='font-size: 12px; text-align: center;'>" + "<span>" + "Speaker: " + "</span>" + d.Speaker + "\n"
                + "<span>" + "Duration: " + "</span>" + Math.round(d.Duration) + "\n" +"</span>"; }
        });
        $(element.node()).popover('show');
    
    //Make chosen circle more visible
    element.style("opacity", 1)
        .style("stroke-width", 6);
    
    //Append lines to bubbles that will be used to show the precise data points
    //vertical line
    d3.select(".chordWrapper").append("g")
        .attr("class", "guide")
        .append("line")
            .attr("x1", element.attr("cx"))
            .attr("x2", element.attr("cx"))
            .attr("y1", +element.attr("cy"))
            .attr("y2", (height))
            .style("stroke", element.style("fill"))
            .style("opacity",  0)
            .style("pointer-events", "none")
            .transition().duration(200)
            .style("opacity", 0.5);
    //horizontal line
    d3.select(".chordWrapper").append("g")
        .attr("class", "guide")
        .append("line")
            .attr("x1", +element.attr("cx"))
            .attr("x2", 0)
            .attr("y1", element.attr("cy"))
            .attr("y2", element.attr("cy"))
            .style("stroke",  element.style("fill"))
            .style("opacity",  0)
            .style("pointer-events", "none")
            .transition().duration(200)
            .style("opacity", 0.5);
    

    并添加:

    .on("mouseover", showTooltip)
    .on("mouseout",  removeTooltip);
    

    到你的圈子(script.js 第 84 行)

    看这里:

    https://embed.plnkr.co/86jdbWgHtABY95ZEv9SE/

    【讨论】:

    • 谢谢你,拉里。删除 .axis .domain 有所帮助,但工具提示仍然无法正常工作。
    • 抱歉忘记将鼠标悬停添加到圆节点(script.js 第 84 行)我编辑了我的答案(我不知道如何更新你的 Plunker...)
    • 非常感谢。还有一个问题。在 v3 中,我可以像我的参考一样显示到图上每个点的路径:bl.ocks.org/nbremer/801c4bb101e86d19a1d0。通过鼠标悬停,它显示 x 轴和 y 轴。当我将其更改为 v4 时,我再也无法拥有它了。你知道是什么问题吗?
    • 您在 html 中多次加载 script.js,是否需要?
    • 非常感谢。不,我犯了一个错误,因为这是我第一次使用 Plunker :) 我更正了链接。感谢提及。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    相关资源
    最近更新 更多