Javascript工具提示不会显示在按样式定义的框中 [英] Javascript Tooltip not showing up in box as defined by style

查看:97
本文介绍了Javascript工具提示不会显示在按样式定义的框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用D3树形图。我添加了一个应该显示在框中的工具提示(请参阅: http://bl.ocks。 org / Caged / 6476579 )。它显示为文本,但不显示在框中。我缺少什么?

I am working on a D3 treemap. I have added a tool tip that should be showing up in a box (see: http://bl.ocks.org/Caged/6476579). It shows up just as text, but doesn't show up in the box. What am I missing?

<HTML>
<HEAD>
  <script type="text/javascript" src="http://d3js.org/d3.v2.js"></script>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  <script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
  <style type="text/css">
    rect {
      fill: none;
      stroke: #fff;
    }

    rect:hover {
      opacity: 0.5;
    }

    text {
      font-family: "Times New Roman", Times, serif;
      font-size: 12px;
    }
  </style>
  <style>
    body {
      font: 10px sans-serif;
    }

    .d3-tip {
      line-height: 1;
      font-weight: bold;
      padding: 12px;
      background: rgba(0, 0, 0, 0.8);
      color: #fff;
      border-radius: 2px;
    }
    /* Creates a small triangle extender for the tooltip */

    .d3-tip:after {
      box-sizing: border-box;
      display: inline;
      font-size: 10px;
      width: 100%;
      line-height: 1;
      color: rgba(0, 0, 0, 0.8);
      content: "\25BC";
      position: absolute;
      text-align: center;
    }
    /* Style northward tooltips differently */

    .d3-tip.n:after {
      margin: -1px 0 0 0;
      top: 100%;
      left: 0;
    }
  </style>

  <BODY>

    <div id="body"></div>
    <script type="text/javascript">
      var pathJson = {
        "name": "Sample data",
        "children": [{
          "name": "Title 1",
          "size": 1,
          "children": [{
            "name": "Title 1-1",
            "size": 1,
            "children": [{
              "name": "1-1-1",
              "size": 1
            }, {
              "name": "1-1-2",
              "size": 1
            }, {
              "name": "1-1-3",
              "size": 1
            }, {
              "name": "1-1-4",
              "size": 1
            }]
          }, {
            "name": "Title 1-2",
            "size": 1,
            "children": [{
              "name": "1-2-1",
              "size": 1
            }, {
              "name": "1-2-2",
              "size": 1
            }, {
              "name": "1-2-3",
              "size": 1
            }]
          }, {
            "name": "Title 1-3",
            "size": 1,
            "children": [{
              "name": "1-3-1",
              "size": 1
            }]
          }]
        }]
      }
      var w = 600 - 80,
        h = 500 - 100,
        x = d3.scale.linear().range([0, w]),
        y = d3.scale.linear().range([0, h]),
        color = d3.scale.category10(),
        root,
        node;
      var treemap = d3.layout.treemap()
        .round(false)
        .size([w, h])
        .sticky(true)
        .padding([10, 0, 0, 0])
        .value(function(d) {
          return d.size;
        });
      var svg = d3.select("#body").append("div")
        .attr("class", "chart")
        .style("width", w + "px")
        .style("height", h + "px")
        .append("svg:svg")
        .attr("width", w)
        .attr("height", h)
        .append("svg:g")
        .attr("transform", "translate(.5,.5)");
      var tip = d3.tip()
        .offset([20, 0])
        .html(function(d) {
          return "<strong>Project:</strong> <span style='color:red'>" + d.name + "</span>";
        })
      svg.call(tip);
      node = root = pathJson;
      var nodes = treemap.nodes(root)
        .filter(function(d) {
          return !d.children;
        });
      var cell = svg.selectAll("g")
        .data(nodes)
        .enter().append("svg:g")
        .attr("class", "cell")
        .attr("transform", function(d) {
          return "translate(" + d.x + "," + d.y + ")";
        })
        .on("click", function(d) {
          return zoom(node == d.parent ? root : d.parent);
        })
        .on('mouseover', tip.show)
        .on('mouseout', tip.hide);
      cell.append("svg:rect")
        .attr("width", function(d) {
          return d.dx - 1;
        })
        .attr("height", function(d) {
          return d.dy - 1;
        })
        .style("fill", function(d) {
          return color(d.parent.name);
        });
      cell.append("svg:text")
        .attr("x", function(d) {
          return d.dx / 2;
        })
        .attr("y", function(d) {
          return d.dy / 2;
        })
        .attr("dy", ".35em")
        .attr("text-anchor", "middle")
        .text(function(d) {
          return d.name;
        })
        .style("opacity", function(d) {
          d.w = this.getComputedTextLength();
          return d.dx > d.w ? 1 : 0;
        });
      d3.select(window).on("click", function() {
        zoom(root);
      });
      d3.select("select").on("change", function() {
        treemap.value(this.value == "size" ? size : count).nodes(root);
        zoom(node);
      });

      function size(d) {
        return d.size;
      }

      function count(d) {
        return 1;
      }

      function zoom(d) {
        //alert(d.name);
        var kx = w / d.dx,
          ky = h / d.dy;
        x.domain([d.x, d.x + d.dx]);
        y.domain([d.y, d.y + d.dy]);
        var t = svg.selectAll("g.cell").transition()
          .duration(d3.event.altKey ? 7500 : 750)
          .attr("transform", function(d) {
            return "translate(" + x(d.x) + "," + y(d.y) + ")";
          });
        t.select("rect")
          .attr("width", function(d) {
            return kx * d.dx - 1;
          })
          .attr("height", function(d) {
            return ky * d.dy - 1;
          })
        t.select("text")
          .attr("x", function(d) {
            return kx * d.dx / 2;
          })
          .attr("y", function(d) {
            return ky * d.dy / 2;
          })
          .style("opacity", function(d) {
            return kx * d.dx > d.w ? 1 : 0;
          });
        //.style("font-size", function(d) { return kx * d.dx > d.w ? "20px" : "12px";});
        node = d;
        d3.event.stopPropagation();
      }
    </SCRIPT>
  </BODY>
</htmL>


推荐答案

一切都很好, code> d3.tip()如下

Everything is fine, but you missed adding class to d3.tip() as below

.attr('class', 'd3-tip')







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

这篇关于Javascript工具提示不会显示在按样式定义的框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆