从 Javascript 可绘制饼图中有条件地删除标签

Conditional removal of label from Javascript Plottable Piechart

我有以下代码:

window.onload = function(){
  var store = [{ Name:"Item 1", Total:18 },
               { Name:"Item 2", Total:7 },
               { Name:"Item 3", Total:3},
               { Name:"Item 4", Total:12}];
  

  var colorScale = new Plottable.Scales.Color();
  var legend = new Plottable.Components.Legend(colorScale);

  var pie = new Plottable.Plots.Pie()
  .attr("fill", function(d){ return d.Name; }, colorScale)
  .addDataset(new Plottable.Dataset(store))
  .sectorValue(function(d){ return d.Total; } )
  .labelsEnabled(true);

    
  new Plottable.Components.Table([[pie, legend]]).renderTo("#chart");
    
     
}
<link href="https://rawgithub.com/palantir/plottable/develop/plottable.css" rel="stylesheet"/>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://rawgithub.com/palantir/plottable/develop/plottable.js"></script>
<div id="container">
  <svg id="chart" width="350" height="350"></svg>
</div>

我的问题是如何让饼图只显示值大于或等于 10(即用户定义的限制)的标签?

您可以在您的案例中使用 labelFormatter 来有条件地隐藏标签。

使用下面的代码隐藏小于 10 的标签。

.labelFormatter(
    function(n)
    { 
      return parseInt(n) > 10 ? n.toString() : '';
    }
  );

注意:在这种情况下,return 类型必须是字符串。这就是为什么我使用 .toString().

这是代码

  var store = [{ Name:"Item 1", Total:18 },
               { Name:"Item 2", Total:7 },
               { Name:"Item 3", Total:3},
               { Name:"Item 4", Total:12}];
  

  var colorScale = new Plottable.Scales.Color();
  var legend = new Plottable.Components.Legend(colorScale);

  var pie = new Plottable.Plots.Pie()
  .attr("fill", function(d){ return d.Name; }, colorScale)
  .addDataset(new Plottable.Dataset(store))
  .sectorValue(function(d){ return d.Total; } )
  .labelsEnabled(true)
  .labelFormatter(
    function(n)
    { 
      return parseInt(n) > 10 ? n.toString() : '';
    }
  );
    
  new Plottable.Components.Table([[pie, legend]]).renderTo("#chart");
<link href="https://rawgithub.com/palantir/plottable/develop/plottable.css" rel="stylesheet"/>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://rawgithub.com/palantir/plottable/develop/plottable.js"></script>
<div id="container">
  <svg id="chart" width="350" height="350"></svg>
</div>