首页 文章

获取饼图剩余切片的百分比 - NVD3

提问于
浏览
-1

在我的饼图的某些情况下,我想向用户显示 the percentage relative to slices of the Pie Chart instead of showing the values of the slices .

例如:我的馅饼中有三个切片: A 代表馅饼的70%, B 代表馅饼的15%, C 代表馅饼的15% .

当我单击隐藏切片 A 时,我想向用户显示剩余切片的百分比,在此示例中,它将是 B = 50%和 C = 50% .

Is it possible in NVD3?

Important: 我不想在点击隐藏某些切片时重新加载图表 .

1 回答

  • 2

    您可以使用 chart.tooltipContent 覆盖图表工具提示标签 . 以下内容改编自NVD3网站上的实时代码示例 . http://nvd3.org/livecode/#codemirrorNav

    示例:http://plnkr.co/edit/UiGLIj?p=preview

    nv.addGraph(function() {
      var chart = nv.models.pieChart()
          .x(function(d) { return d.label })
          .y(function(d) { return d.value })
          .showLabels(true);
    
      chart.tooltipContent(function(key, x, obj){
        var enabledTotal = data[0].values
            .filter(function(item){return !item.disabled;})
            .reduce(function(a, b){return a + b.value}, 0);
    
        return Math.round((obj.value/enabledTotal) * 100) + "%";
      })
    
        d3.select("#chart svg")
            .datum(data)
          .transition().duration(1200)
            .call(chart);
    
      return chart;
    });
    

相关问题