【问题标题】:mouseover tooltip will not stay fixed in right corner鼠标悬停工具提示不会固定在右上角
【发布时间】:2016-10-11 14:42:27
【问题描述】:

我正在关注帖子中的代码:

Horizontal Bar Chart D3, .txt file

data = [{"class":"C 1","nums":{"A":716, "B":1287, "C":249}}
,{"class":"C 2","nums":{"A":2000, "B":1876, "C":3009}}
,{"class":"C 3","nums":{"A":899, "B":2333, "C":418}}];

data.forEach(function(d){
    var values = 0;
    for (var key in d.nums){
      values += d.nums[key]
    }
    d.total = values;
  });

var div = d3.select("body").append("div").attr("class", "toolTip");

var xScale = scale = d3.scale.linear()
            .domain([0, d3.max(data, function(d) { return d.total; })])
            .range([0, 400]);

var svg = d3.select('body')
            .append("svg")
            .attr("width", 400)
            .attr("height", 400);

var bars = svg.selectAll(".rects")
            .data(data)
            .enter()
            .append("rect");

bars.attr("x", 0).attr("y", function(d,i){ return i*50})
    .attr("height", 40)
.attr("fill", "teal")
    .attr("width", function(d){ return xScale(d.total)});

bars.on("mousemove", function(d){
                div.style("left", d3.event.pageX+10+"px").style("top", d3.event.pageY-25+"px").style("display", "inline-block").html("All the values: A=" + d.nums.A + ", B=" + d.nums.B + ", C=" + d.nums.C);
            }).on("mouseout", function(d){
                div.style("display", "none");
            });

我要做的是修复右下角的工具提示。我知道需要在 bar.on("mousemove"...) 中将位置设置为固定,但我很难将属性设置为一些固定的 x,y 坐标。如何在固定位置放置工具提示?

【问题讨论】:

    标签: javascript html d3.js


    【解决方案1】:

    将此添加到 CSS 中:

    .toolTip{
      position:absolute;
      bottom: 0;
      right: 0;
      }
    

    并从鼠标悬停中删除它:

    div.style("left", d3.event.pageX+10+"px")
    .style("top", d3.event.pageY-25+"px")
    .style("display", "inline-block")
    .html("All the values: A=" + d.nums.A + ", B=" + d.nums.B + ", C=" + d.nums.C);
    

    工作示例:

    data = [{
      "class": "C 1",
      "nums": {
        "A": 716,
        "B": 1287,
        "C": 249
      }
    }, {
      "class": "C 2",
      "nums": {
        "A": 2000,
        "B": 1876,
        "C": 3009
      }
    }, {
      "class": "C 3",
      "nums": {
        "A": 899,
        "B": 2333,
        "C": 418
      }
    }];
    
    data.forEach(function(d) {
      var values = 0;
      for (var key in d.nums) {
        values += d.nums[key]
      }
      d.total = values;
    });
    
    var div = d3.select("body").append("div").attr("class", "toolTip");
    
    var xScale = scale = d3.scale.linear()
      .domain([0, d3.max(data, function(d) {
        return d.total;
      })])
      .range([0, 400]);
    
    var svg = d3.select('body')
      .append("svg")
      .attr("width", 400)
      .attr("height", 400);
    
    var bars = svg.selectAll(".rects")
      .data(data)
      .enter()
      .append("rect");
    
    bars.attr("x", 0).attr("y", function(d, i) {
        return i * 50
      })
      .attr("height", 40)
      .attr("fill", "teal")
      .attr("width", function(d) {
        return xScale(d.total)
      });
    
    bars.on("mousemove", function(d) {
      div
     // .style("left", d3.event.pageX + 10 + "px")
      //.style("top", d3.event.pageY - 25 + "px")
      .style("display", "inline-block")
      .html("All the values: A=" + d.nums.A + ", B=" + d.nums.B + ", C=" + d.nums.C);
    }).on("mouseout", function(d) {
      div.style("display", "none");
    });
    .toolTip{
      position:absolute;
      bottom: 0;
        right: 0;
      }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多