The code in the link is for two pie charts. http://jsbin.com/vipodidiyo/edit?html,css,js,outputThey are almost the same just different data and positions. When user mouseover a piece of pie, that piece and tooltip will pup up in that pie chart.
My question is... when user mouseover a piece of pie, how can I make the popups happen in both of the charts. For example, if user mouseover the green part in the first pie chart, the green parts and tooltips in both charts will show up at the same time.
How can I make two charts connected? Does anyone have clue about this? Appreciate!!
Updated
I found a similar question here and try to change the code follow Lars's answer. Interactions that affect multiple separate charts in d3.js?
I assigned a class name 'path', and changed d3.select(this)
to d3.select('.path')
. But wherever my mouse is, the purple part of first pie chart pops up.
Here is my updated JSbins http://jsbin.com/gozuyekibi/edit?html,css,js,output
var path = svg.selectAll('path').
data(pie(data)).
enter().
append('path').
attr('d', arc).
attr('class','path').
//attr('id','pathid').
attr('fill', function(d,i){
return color(d.data.label);}).
on('mouseover', function(d) {
d3.select('.path').transition()
.duration(1000)
.attr("d", arcHover);
var total = d3.sum(data.map(function(d) {
return d.count;
}));
var percent = Math.round(1000 * d.data.count / total) / 10;
tooltip.select('.label').html(d.data.label);
tooltip.select('.count').html(d.data.count);
tooltip.select('.percent').html(percent + '%');
tooltip.style('display', 'block');
}).
on('mouseout', function() {
d3.select('.path').transition()
.duration(500)
.attr("d", arc);
tooltip.style('display', 'none');
});
First, assign a single class to your tooltips and clean up that css. Next, assign each arc
path
a class so that your can pair your paths on mouseover. Then generalize yourmouseover
to operate on both paths:Full code: