Show data on mouseover of circle

前端 未结 5 1289
我寻月下人不归
我寻月下人不归 2020-11-22 10:05

I have a set of data that I am plotting in a scatter. When I mouseover one of the circles I would like it to popup with data (like x, y values, maybe more). Here is what I

5条回答
  •  执笔经年
    2020-11-22 10:17

    A really good way to make a tooltip is described here: Simple D3 tooltip example

    You have to append a div

    var tooltip = d3.select("body")
        .append("div")
        .style("position", "absolute")
        .style("z-index", "10")
        .style("visibility", "hidden")
        .text("a simple tooltip");
    

    Then you can just toggle it using

    .on("mouseover", function(){return tooltip.style("visibility", "visible");})
    .on("mousemove", function(){return tooltip.style("top",
        (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");})
    .on("mouseout", function(){return tooltip.style("visibility", "hidden");});
    

    d3.event.pageX / d3.event.pageY is the current mouse coordinate.

    If you want to change the text you can use tooltip.text("my tooltip text");

    Working Example

提交回复
热议问题