Buy Me a Coffee

Friday, 2 May 2014

Including an HTML link in a d3.js tool tip

The following post is a portion of the D3 Tips and Tricks book which is free to download. To use this post in context, consider it with the others in the blog or just download the the book as a pdf / epub or mobi .

Including an HTML link in a d3.js tool tip

There was an interesting question on about adding an HTML link to a tooltip. While the person asking the question had the problem pretty much solved already, I thought it might be useful for others (bear in mind that this post follows on from the original on adding tooltips in d3.js).
The premise is that you want to add a tool tip to your visualization using the method described here, but you also want to include an HTML link in the tooltip that will link somewhere else. This might look a little like the following;
Tool tip with an HTML Link
In the image above the date has been turned into a link. In this case the link goes to, but that can obviously be configurable.
The full code for this example can be found on github or in the code samples bundled with D3 Tips and Tricks (tooltips-link.html and data.csv). A working example can be found on
There are a few changes that we would want to make to our original tooltip code to implement this feature.
First of all, we’ll add the link to the date element. Adding an HTML link can be as simple as wrapping the ‘thing’ to be used as a link in <a> tags with an appropriate URL to go to.
The following adaptation of the code that prints the information into our tooltip code does just that;
    div .html(
        '<a href= "">' + // The first <a> tag
        formatTime( +
        "</a>" +                          // closing </a> tag
        "<br/>"  + d.close)     
        .style("left", (d3.event.pageX) + "px")             
        .style("top", (d3.event.pageY - 28) + "px");
<a href= ""> places our first <a> tag and declares the URL and the second tag follows after the date.
The second change we will want to make is to ensure that the tooltip stays in place long enough for us to actually click on the link. The problem being solved here is that our original code relies on the mouse being over the dot on the graph to display the tooltip. if the tooltip is displayed and the cursor moves to press the link, it will move off the dot on the graph and the tooltip vanishes (Nice!).
To solve the problem we can leave the tooltip in place adjacent to a dot while the mouse roams freely over the graph until the next time it reaches a dot and then the previous tooltip vanishes and a new one appears. The best way to appreciate this difference is to check out the live example on
The code is as follows (you may notice that this also includes the link as described above);
    .on("mouseover", function(d) {        
            .style("opacity", 0);
            .style("opacity", .9);    
        div .html(
            '<a href= "">' + // The first <a> tag
            formatTime( +
            "</a>" +                          // closing </a> tag
            "<br/>"  + d.close)     
            .style("left", (d3.event.pageX) + "px")             
            .style("top", (d3.event.pageY - 28) + "px");
We have removed the .on("mouseout" portion and moved the function that it used to carry out to the start of the.on("mouseover" portion. That way the first thing that occurs when the mouse cursor moves over a dot is that it removes the previous tooltip and then it places the new one.
The last change we need to make is to remove from the <style> section the line that told the mouse to ignore the tooltip;
  /*  pointer-events: none;    This line needs to be removed */
In this case I have just commented it out so that it’s a bit more obvious that it gets removed.
One link is interesting, but let’s face it, we didn’t go to all the trouble of putting a link into a tool tip to just go to one location. Now we shift it up a gear and start linking to different places depending on our data. At the same time (and because someone asked) we will make the link open in a new tab!
The changes to the script are fairly minor, but one fairly large change is the need to have links to go to. For this example I have added a range of links to visit to our csv file so it now looks like this;
The code change is to the piece of JavaScript where we add the html. This is what we end up with;
    div .html(
        '<a href= "''" target="_blank">' + //with a link
        formatTime( +
        "</a>" +
        "<br/>"  + d.close)     
        .style("left", (d3.event.pageX) + "px")             
        .style("top", (d3.event.pageY - 28) + "px");
We’ve replaced the URL with the variable for our link column and we’ve also added in the target="_blank" statement so that our link opens in a new tab.
A quick word of warning on this piece of code. it can look a little messy because it includes nested speech-marks and quotation marks. This makes it a bit confusing if you’re unused to the idea of nesting this type of information. If things aren’t working in this part of your code, I recommend going back to basics and adding one piece at a time, getting it right and then add the next piece. Take it slow :-).
The full code for this multi link example can be found on github or in the code samples bundled with D3 Tips and Tricks (tooltips-link-multi.html and datatips.csv). A working example can be found on
Hopefully that helps people with a similar desire to include links in their tooltips. Many thanks to the reader who suggested it :-).

The description above (and heaps of other stuff) is in the D3 Tips and Tricks book that can be downloaded for free (or donate if you really want to :-)).


  1. Thanks for this tips, it's usefull

  2. Hi, is this possible for 3d surface plot graphs? =)

    1. I seem to recall seeing something like a 3d surface plot, but I couldn't discover it again. Bearing in mind that it will be the users browser doing the processing, I presume that each individuals experience would vary markedly.