我有以下代码:
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 (即用户定义的限制)的标签?
发布于 2015-12-02 10:35:59
您可以在您的情况下使用labelFormatter来有条件地隐藏标签。
使用下面的代码隐藏低于10的标签。
.labelFormatter(
function(n)
{
return parseInt(n) > 10 ? n.toString() : '';
}
);注意:在这种情况下,返回类型必须是字符串。这就是我使用.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>
https://stackoverflow.com/questions/34039682
复制相似问题