显示有关鼠标悬停的数据

Sco*_*ieB 159 mouseover tooltip d3.js

我有一组数据,我正在散点图中.当我将鼠标悬停在其中一个圆圈上时,我希望它能够弹出数据(如x,y值,或许更多).这是我尝试使用的:

vis.selectAll("circle")
   .data(datafiltered).enter().append("svg:circle")
   .attr("cx", function(d) { return x(d.x);})
   .attr("cy", function(d) {return y(d.y)})
   .attr("fill", "red").attr("r", 15)
   .on("mouseover", function() {
        d3.select(this).enter().append("text")
            .text(function(d) {return d.x;})
            .attr("x", function(d) {return x(d.x);})
            .attr("y", function (d) {return y(d.y);}); });
Run Code Online (Sandbox Code Playgroud)

我怀疑我需要提供更多信息才能输入什么数据?

Lar*_*off 176

我假设你想要的是一个工具提示.最简单的方法是在svg:title每个圆圈上附加一个元素,因为浏览器会显示工具提示并且您不需要鼠标处理程序.代码就像是

vis.selectAll("circle")
   .data(datafiltered).enter().append("svg:circle")
   ...
   .append("svg:title")
   .text(function(d) { return d.x; });
Run Code Online (Sandbox Code Playgroud)

如果你想要更高级的工具提示,你可以使用tipsy.请看这里的例子.

  • 我喜欢醉了.我现在唯一的问题是它指向圆圈的左上角,而不是该演示中的边缘.我没有找到任何明显的原因.http://jsfiddle.net/scottieb/JwaaV/(非常低调) (3认同)

Pwd*_*wdr 140

这里描述了制作工具提示的一种非常好的方法:简单的D3工具提示示例

你必须附加一个div

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

然后你可以使用它来切换它

.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");});
Run Code Online (Sandbox Code Playgroud)

d3.event.pageX/ d3.event.pageY是当前鼠标坐标.

如果要更改可以使用的文本 tooltip.text("my tooltip text");

工作实例

  • 您可以将数据绑定到此工具提示吗? (4认同)
  • Afaik你可以将数据绑定到每个DOM元素. (2认同)

Dan*_*010 39

我最近发现了一个很棒的库.它使用简单,结果非常简洁:d3-tip.

你可以在这里看到一个例子:

在此输入图像描述

基本上,您所要做的就是下载(index.js),包括脚本:

<script src="index.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后按照此处的说明进行操作 (相同链接示例)

但对于您的代码,它将是这样的:

定义方法:

var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-10, 0])
  .html(function(d) {
    return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>";
  })
Run Code Online (Sandbox Code Playgroud)

创建你的svg(就像你已经做的那样)

var svg = ...
Run Code Online (Sandbox Code Playgroud)

调用方法:

svg.call(tip);
Run Code Online (Sandbox Code Playgroud)

向您的对象添加提示:

vis.selectAll("circle")
   .data(datafiltered).enter().append("svg:circle")
...
   .on('mouseover', tip.show)
   .on('mouseout', tip.hide)
Run Code Online (Sandbox Code Playgroud)

别忘了添加CSS:

<style>
.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>
Run Code Online (Sandbox Code Playgroud)

  • 最新的[d3-tip](https://github.com/Caged/d3-tip)支持d3v4就好了.你是不是很明显,但是d3v4对我来说很有用. (2认同)

Mik*_*kov 7

这个简洁的例子演示了如何在 d3 中创建自定义工具提示的常用方法。

var w = 500;
var h = 150;

var dataset = [5, 10, 15, 20, 25];

// firstly we create div element that we can use as
// tooltip container, it have absolute position and
// visibility: hidden by default

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

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

// here we add some circles on the page

var circles = svg.selectAll("circle")
  .data(dataset)
  .enter()
  .append("circle");

circles.attr("cx", function(d, i) {
    return (i * 50) + 25;
  })
  .attr("cy", h / 2)
  .attr("r", function(d) {
    return d;
  })
  
  // we define "mouseover" handler, here we change tooltip
  // visibility to "visible" and add appropriate test
  
  .on("mouseover", function(d) {
    return tooltip.style("visibility", "visible").text('radius = ' + d);
  })
  
  // we move tooltip during of "mousemove"
  
  .on("mousemove", function() {
    return tooltip.style("top", (event.pageY - 30) + "px")
      .style("left", event.pageX + "px");
  })
  
  // we hide our tooltip on "mouseout"
  
  .on("mouseout", function() {
    return tooltip.style("visibility", "hidden");
  });
Run Code Online (Sandbox Code Playgroud)
.tooltip {
    position: absolute;
    z-index: 10;
    visibility: hidden;
    background-color: lightblue;
    text-align: center;
    padding: 4px;
    border-radius: 4px;
    font-weight: bold;
    color: orange;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
Run Code Online (Sandbox Code Playgroud)


dan*_*mal 6

您可以像这样传入要在鼠标悬停中使用的enter数据 - mouseover事件使用一个函数,将您之前编辑的数据作为参数(并将索引作为第二个参数),这样您就不需要再次使用enter()了.

vis.selectAll("circle")
.data(datafiltered).enter().append("svg:circle")
.attr("cx", function(d) { return x(d.x);})
.attr("cy", function(d) {return y(d.y)})
.attr("fill", "red").attr("r", 15)
.on("mouseover", function(d,i) {
    d3.select(this).append("text")
        .text( d.x)
        .attr("x", x(d.x))
        .attr("y", y(d.y)); 
});
Run Code Online (Sandbox Code Playgroud)