Some D3 Tips

Thu, Dec 04, 2014
Complete your Github portfolio.

Creating Tooltips in D3

Tooltip is the term used to reference the small box which appears over certain elements of a web graphic, offering more information to the user.

Tooltip example

There are numerous ways to create tooltips. This tutorial will show one way using a tooltip.

Using d3-tip

A developer named Justin Palmer created a D3 tooltip, and is hosting it for free on Github. You can download the whole package, install with a utility called Bower, or you can just copy and paste the JavaScript file into your own project.

To include the tooltip in your own project, make sure you link to the JavaScript file after you include the D3 library. In the example below, we named the d3 tip file d3tip.js.

<script src=""></script>
<script src="d3tip.js"></script>

Next, in your code, you will need to declare a variable to store the d3-tip element. In this variable, you will also describe what part of your data should be displayed in the tooltip.

var tip = d3.tip()
    .attr("class", "d3-tip")
    .html(function(d) {

        //you would modify what you want to show up in box here.
        return d; 

The d variable refers to the data of the element which invokes the tooltip. Let’s hypothetically say that a piece of your data was a property called “pop” for population. Our tooltip might look like this:

var tip = d3.tip()
    .attr("class", "d3-tip")
    .html(function(d) { 

        //hypothetical example:

        var content = "<strong>Population:</strong> " + d.pop; 

        return content; 

In the example above, the d.pop will refer to each element that invoked the tooltip. This would only work in cases where a “pop” property existed in the data.

To actually run the code, you’ll need two more things: 1) To load in the CSS for the tooltip, and 2) call the tooltip on mouseover. The CSS can be downloaded here and included in your own stylesheets.

//via d3-tip readme:

/* Invoke the tip in the context of your visualization */

  .attr('width', function() { return x.rangeBand() })
  .attr('height', function(d) { return h - y(d) })
  .attr('y', function(d) { return y(d) })
  .attr('x', function(d, i) { return x(i) })
  .on('mouseout', tip.hide);

Example in Action

Below is an example of a simple bubble chart using d3-tip.

<!DOCTYPE html>
    <meta charset="utf-8">
    <title>Example Bubble Chart</title>
    <link href="" rel="stylesheet">
    <script src="" charset="utf-8"></script>
    <script src="" charset="utf-8"></script>
        var margin = {top:0, right:0, bottom:0, left:0},
            width  =  400 - margin.left - margin.right,
            height =  400 -  - margin.bottom;
        var svg ="body")
            .attr("width", width + margin.left + margin.right)
            .attr("height", height + + margin.bottom);
        var data = [
            {name: "Apple",  value: 20},
            {name: "Banana", value: 12},
            {name: "Cherry", value: 10}
        var bubble = d3.layout.pack()
            .size([width, height])
        var nodes = bubble.nodes({children: data})
            .filter(function(d) { return !d.children; });
        var tip = d3.tip()
                .attr("class", "d3-tip")
                .html(function(d) { return; });
        //setup the tooltip;
            .attr("r", function(d){ return d.r; })
            .attr("cx", function(d){ return d.x; })
            .attr("cy", function(d){ return d.y; })
            .style("fill", "red")

            //show and hide the tooltip during mouse events
            .on("mouseout", tip.hide);