D3.JS线图工具提示处



我想在此图表中添加工具提示。我指的是这个示例

问题是它们在SVG中没有唯一的点,它只有路径标签。

 svg.selectAll("dot")   
    .data(data)         
.enter().append("circle")                               
    .attr("r", 5)       
    .attr("cx", function(d) { return x(d.date); })       
    .attr("cy", function(d) { return y(d.close); })     
    .on("mouseover", function(d) {      
        div.transition()        
            .duration(200)      
            .style("opacity", .9);      
        div .html(formatTime(d.date) + "<br/>"  + d.close)  
            .style("left", (d3.event.pageX) + "px")     
            .style("top", (d3.event.pageY - 28) + "px");    
        })                  
    .on("mouseout", function(d) {       
        div.transition()        
            .duration(500)      
            .style("opacity", 0);   
    });

喜欢上面的代码选择SVG上的点,但我没有任何特定元素来绑定工具提示。

我是D3.js的新手,可以帮助我。

您应该为y:

服用d.值
.attr("cy", function(d) { return y(d.value); })  

现在在鼠标上附加新元素:

.on("mouseover", function(d) {      
        svg.append("text")
          .text(d.value)
          .attr('class', 'tooltip').style("font-size","10px")
          .attr("x", x(d.date))
          .attr("y", y(d.value))
          .attr('fill', 'red');
        })                  
    .on("mouseout", function(d) {       
        d3.selectAll('.tooltip').remove();
    });

最新更新