Adding a title attribute to svg:g element in D3.js

accraze picture accraze · Jul 13, 2012 · Viewed 28.5k times · Source

I'm building an app with D3.js that displays a data in a tree-map. Ideally what I would like to is give the tree-map a tool-tip to display more info on each node of the tree-map. The tree-map is fed data from a .JSON file.

I am currently working with the jquery plug-in Poshy Tip where I can pass this info through the title= attribute. I know I need to somehow add the title attribute to the svg:g element in the tree-map but I can't figure out where I set each nodes title attr.

Heres the beginning of my script where I make all my declarations etc...

<div id="chart"></div>
<script type="text/javascript">
var tree = d3.layout.tree()
.size([h, w - 160]);

var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });

var vis = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(40,0)");

d3.json("test.json", function(json) {
  json.x0 = 800;
  json.y0 = 0;
  update(root = json);
});

Here is how I am using poshytip() in the head

$(function(){
  $('node').poshytip({slide: false, followCursor: true, alignTo: 'cursor', 
    showTimeout: 0, hideTimeout: 0, alignX: 'center', alignY: 'inner-bottom',
    className: 'tip-twitter'});
}

Pretty much I just want to be able to "mouseover" the individual items in the interactive tree-map and get a tooltip to pop-up.. but i havent had any luck. How can I set each item to have a specific Id.. do I do that in the .JSON file? Is there an easier way to do this? Am I going about this in the wrong manner? Any help would be great.

Answer

Elijah picture Elijah · Jul 26, 2012
var vis = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h);

var yourGElement = vis.append("svg:g").attr("transform", "translate(40,0)");

yourGElement.append("svg:title").text("Your tooltip info");