我试着用Plottable.js创建一个饼图。有人知道怎么做吗?我对如何传递值并在其中添加标签感到困惑。
这是我的样本数据:
var store = [{ Name:"Item 1", Total:18 },
{ Name:"Item 2", Total:7 },
{ Name:"Item 3", Total:3},
{ Name:"Item 4", Total:12}];再次感谢!
发布于 2015-10-17 00:25:51
您可以使用Pie.sectorValue指定每个片的值,也可以用Pie.labelsEnabled打开标签,该标签显示每个扇区的对应值。您还可以用Pie.labelFormatter格式化标签。
但是,我认为除了扇区值之外,没有其他方法将数据显示为标签,但取决于您想要的是什么,图例可能会起作用。
下面是一个带有传奇的饼形图的例子:
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)
.labelFormatter(function(n){ return "$ " + n ;});
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>
或者,如果所有的值都是唯一的,那么您可能可以使用labelFormatter对其进行黑客攻击。
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 reverseMap = {};
store.forEach(function(s) { reverseMap[s.Total] = s.Name;});
var ds = new Plottable.Dataset(store);
var pie = new Plottable.Plots.Pie()
.addDataset(ds)
.sectorValue(function(d){ return d.Total; } )
.labelsEnabled(true)
.labelFormatter(function(n){ return reverseMap[n] ;})
.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/33164488
复制相似问题