Reputation: 51
I am trying to add tooltips on a donut chart using src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"
I want the font color of tooltips change when user switches to different pieces of the donut.
I know there are many ways to add tooltips, but in this example I want to stick to use this "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"
var tip = d3.
tip().
attr('class', 'd3-tip').
offset([100, 0]).
html(function(d) {
if (d.fruit == 'Apple') {
return "<strong style = 'color:red'>Count: </strong> <span style='color:red'>" + d.count + '</span>';
} else if (d.fruit == 'Banana') {
return "<strong style = 'color:blue'>Count: </strong> <span style='color:blue'>" + d.count + '</span>';
} else {
return "<strong style = 'color:orange'>Count: </strong> <span style='color:orange'>" + d.count + '</span>';
}
});
Upvotes: 3
Views: 1211
Reputation: 9691
You want to access the data
property on the d
parameter to get the fruit
and count
. Like this:
if (d.data.fruit == 'Apple') {
return "<strong style = 'color:blue'>Count: </strong> <span style='color:blue'>" + d.data.count + '</span>';
} else if (d.data.fruit == 'Banana') {
return "<strong style = 'color:red'>Count: </strong> <span style='color:red'>" + d.data.count + '</span>';
} else {
return "<strong style = 'color:orange'>Count: </strong> <span style='color:orange'>" + d.data.count + '</span>';
}
Also looks like you had your Apple
and Banana
colors backwards so I fixed that above too.
Upvotes: 2