D3.js加提示框

使用D3.js对元素加入提示框,分为以下两种提示框,一种为title标签,一种为自定义样式的提示框:

title框

var path = svg.datum(root).selectAll("path")
            .data(partition.nodes)
            .enter().append("path")
            .attr("display", function(d) { return d.depth ? null : "none"; }) // hide inner ring
            .attr("d", arc)
            .style("stroke", "#fff")
            .style("fill", function(d) { return color((d.children ? d : d.parent).name); })
            .style("fill-rule", "evenodd")
            .append("title")//此处加入title标签
            .text(function(d){return d.name}//title标签的文字

自定义框

JS:加入提示框的div,并将其opacity设为0,这样提示框就会看不到。

var tooltip=d3.select("body")
    .append("div")
    .attr("class","tooltip")
    .style("opacity",0.0);

CSS:toolafter为提示框左上角的小箭头,可不加

.tooltip{
    position:absolute;
    padding:5px;
    width:120;
    height:auto;
    font-family:simsun;
    font-size:14px;
    color:black;
    background-color: rgb(255,255,255);
    border-width: 2px solid rgb(255,255,255);
    border-radius:5px;
}
.tooltip:after{
    content: '';
    position:absolute;
    bottom:100%;
    left:20%;
    margin-left: -8px;
    width:0;
    height:0;
    border-bottom:12px solid rgb(255,255,255);
    border-right:12px solid transparent;
    border-left:12px solid transparent;
}

使用:在对元素应用提示框时,需要将其定位到鼠标附近,并且将其opacity设为1以显示提示框。鼠标移开时,将其opacity设为0.

gridrect.on("mouseover",function (d) {
       var t=parseInt(threshold(d[2]).substring(1,2));
       if(t>0) {
           tooltip
               .html(" x: " +d[0]+"<br/>"+" y: "+d[1]+"<br/>"+" 端口号: "+(256 * d[1] + d[0])+"<br/>"+" 连接次数: "+d[2])
               .style("left",(d3.event.pageX) +"px")
               .style("top",(d3.event.pageY +20)+"px")
               .style("opacity",1.0)
       }
   })
       .on("mouseout",function (d) {
           tooltip.style("opacity",0.0);
       });

`

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 9,676评论 0 8
  • 1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,...
    kiddings阅读 3,199评论 0 11
  • 学习CSS的最佳网站没有之一 http://www.w3school.com.cn/tags/index.asp ...
    Amyyy_阅读 1,094评论 0 1
  • 1.块级元素和行内元素 块级(block-level)元素;行内(内联、inline-level)元素。 块元素的...
    饥人谷_小侯阅读 2,042评论 1 4
  • 朝7不晚5阅读 330评论 0 3