gpt4 book ai didi

javascript - 有条件地从 Javascript Plottable Piechart 中删除标签

转载 作者:行者123 更新时间:2023-12-03 08:13:48 25 4
gpt4 key购买 nike

我有以下代码:

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() : '';
}
);

注意:在这种情况下,返回类型必须是字符串。这就是我使用 .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>

关于javascript - 有条件地从 Javascript Plottable Piechart 中删除标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34039682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com