Mouseover event on two charts at the same time d3.js

后端 未结 1 1786
小蘑菇
小蘑菇 2021-01-03 11:36

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 mouseov

1条回答
  •  抹茶落季
    2021-01-03 12:08

    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 your mouseover to operate on both paths:

    .on('mouseover', function(d0) {
    
        // apply over to both paths
        d3.selectAll('path.' + d0.data.label).transition()
          .duration(1000)
          .attr("d", arcHover)
          // now loop each of them
          .each(function(d1) {
            // get a sum for the group of paths
            var total = d3.sum(this.parentNode.childNodes, function(d2) {
              return d2.value;
            });
            // and a percent
            var percent = Math.round(1000 * d1.value / total) / 10;
    
            // find correct tooltip
            var tooltip = d3.select(this.ownerSVGElement.parentNode.childNodes[1]);
            tooltip.select('.label').html(d1.data.label);
            tooltip.select('.count').html(d1.data.count);
            tooltip.select('.percent').html(percent + '%');
            tooltip.style('display', 'block');
    
          })
      })
      .on('mouseout', function(d) {
    
          // apply to both paths
          d3.selectAll('path.' + d.data.label).transition()
            .duration(500)
            .attr("d", arc);
    
          // hide all tooltips
          d3.selectAll('.tooltip').style('display', 'none');
      });
    

    Full code:

    
    
    
    
      
    
    
    
      

    0 讨论(0)
提交回复
热议问题