Embed URL


SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

D3 tooltip using SVG title element

View index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<!DOCTYPE html>
<script type="text/javascript" src=""></script>
<div id="chart"></div>
<script type="text/javascript">
var w = 800, h = 500;
var colors = d3.scale.category20();
var vis ="#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);
var data = d3.range(20).map(function() { return { x:Math.random()*w, y:Math.random()*h, r:Math.random()*30 } });
.attr("stroke", "black")
.attr("fill", function(d, i) { return colors(i); })
.attr("cx", function(d, i) { return d.x; })
.attr("cy", function(d, i) { return d.y; })
.attr("r", function(d, i) { return d.r; })
// Here we add an SVG title element the contents of which is effectively rendered in a tooltip
.text(function(d, i) { return "My color is " + colors(i); });

There is a catch though, check out this:



many thanks for the sample code, one problem that I see in this code as well as in other examples. title element exists in html code however it doesn't display when you run the webpage. Do you have any idea for the reason of that? Maybe it should be styled with css?

I believe it works when one uses svg:group. Any explanation on this would be very useful.



It seems that it depends on the browser, check out this discussion. So instead of relying on the browsers' interpretation of svg:title it's better to use tooltips drawn manually, like for instance the one in the jQuery tipsy example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.