Alexis Alias
Alexis Alias

Reputation: 23

Add chart to tooltip in 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;
    });
})

Upvotes: 2

Views: 4708

Answers (1)

Gerardo Furtado
Gerardo Furtado

Reputation: 102194

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

How to create a chart inside a tooltip?

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.

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 --^

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...
})

Here is the demo:

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>

Upvotes: 3

Related Questions