将图表添加到D3中的工具提示 [英] Add chart to tooltip in d3

查看:142
本文介绍了将图表添加到D3中的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在工具提示中添加简单的条形图;它由2个变量组成-男性和女性.我希望有人可以帮助我将其放在工具提示中,而不是将其附加到当前正在附加的位置.我给了这个特定的区域以补充,这样我就知道它实际上正在显示(它确实是),但是我不知道如何将其放入工具提示中.任何帮助深表感谢.哦,这需要在d3中完成,这是为什么我要问这个问题的部分原因–我看到了一个在纯d3中未实现的类似问题,而且我无法完全遵循正在模仿的内容在此示例中.

I am attempting to add a simply bar chart to my tooltip; it consists of 2 variables -- men and women. I was hoping someone might be able to help me put this inside of the tooltip instead of appending it to where it is currently being appended. I've given this a particular area to be appended just so that I know that it is, in fact, showing up(which it is), but I don't know how to get it into the tool tip. Any help is much appreciated. Oh, and this needs to be done in d3, which is partial to why I am asking this question -- I saw a similar question that wasn't implemented in pure d3, and I couldn't completely follow what was going on to emulate it in this example.

.on("mouseover",  function(d)
{
  tip.show(d);
  var state = d.properties.name;
  var men = d.properties.men;
  var women = d.properties.women;
  var dataset = [men, women];
  var barHeight = 20;

  var x = d3.scale.linear()
    .domain([0, d3.max(dataset)])
    .range([0, width/2]);

  var chart = d3.select(".chart")
    .attr("width", width)
    .attr("height", barHeight * dataset.length);

  var bar = chart.selectAll("g")
    .data(dataset)
    .enter().append("g")
    .attr("transform", function(d, i)
    {
      return "translate(0," + i * barHeight + ")";
    });

  bar.append("rect")
    .attr("width", x)
    .attr("height", barHeight - 1);

  bar.append("text")
    .attr("x", function(d)
    {
      return x(d)/2+5;
    })
    .attr("y", barHeight / 2)
    .attr("dy", ".35em")
    .text(function(d)
    {
      return "$" + d;
    });
})

推荐答案

由于您没有共享创建图表的全部代码,因此此答案仅处理您问题的标题:

Since you didn't shared the whole code to create the chart, this answer will deal with your question's title only:

我不是d3.tip()用户,因为我创建了自己的工具提示.但是,您想要的东西一点也不复杂:由于工具提示是<div>元素,因此您绝对可以在其中添加SVG.

I'm not a d3.tip() user, since I create my own tooltips. But what you want is not complicated at all: As the tooltips are <div> elements, you can definitely add a SVG inside them.

但是,您必须知道位置以创建SVG.因此,在下面的演示中,我将创建此d3.tip工具提示:

However, you have to know where to create the SVG. So, in the following demo, I'm creating this d3.tip tooltip:

var tool_tip = d3.tip()
    .attr("class", "d3-tip")
    .offset([20, 120])
    .html("<p>This is a SVG inside a tooltip:</p>
        <div id='tipDiv'></div>");
    //div ID here --^

这里的重要部分是:d3.tip div内有一个内部<div>,具有给定的ID(在这种情况下为tipDiv).我将使用该ID在工具提示中创建我的SVG:

The important part here is this: there is a inner <div> inside the d3.tip div, with a given ID (in that case, tipDiv). I'm gonna use that ID to create my SVG inside the tooltip:

selection.on('mouseover', function(d) {
    tool_tip.show();
    var tipSVG = d3.select("#tipDiv")
        //select the div here--^
        .append("svg")
        //etc...
})

这是演示:

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

var tool_tip = d3.tip()
  .attr("class", "d3-tip")
  .offset([20, 120])
  .html("<p>This is a SVG inside a tooltip:</p><div id='tipDiv'></div>");

svg.call(tool_tip);

var data = [14, 27, 19, 6, 17];

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

circles.attr("cx", 50)
  .attr("cy", function(d, i) {
    return 20 + 50 * i
  })
  .attr("r", function(d) {
    return d
  })
  .attr("fill", "teal")
  .on('mouseover', function(d) {
    tool_tip.show();
    var tipSVG = d3.select("#tipDiv")
      .append("svg")
      .attr("width", 200)
      .attr("height", 50);

    tipSVG.append("rect")
      .attr("fill", "steelblue")
      .attr("y", 10)
      .attr("width", 0)
      .attr("height", 30)
      .transition()
      .duration(1000)
      .attr("width", d * 6);

    tipSVG.append("text")
      .text(d)
      .attr("x", 10)
      .attr("y", 30)
      .transition()
      .duration(1000)
      .attr("x", 6 + d * 6)
  })
  .on('mouseout', tool_tip.hide);

.d3-tip {
  line-height: 1;
  padding: 6px;
  background: wheat;
  border-radius: 4px solid black;
  font-size: 12px;
}

p {
  font-family: Helvetica;
}

<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.min.js"></script>
<p>Hover over the circles:</p>

这篇关于将图表添加到D3中的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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