如何使用Plottable.js创建饼状图
How to create Pie chart using Plottable.js
我尝试使用Plottable.js创建一个饼状图。有人知道怎么做吗?我对如何传递值和放置标签感到困惑。
下面是我的示例数据:
var store = [{ Name:"Item 1", Total:18 },
{ Name:"Item 2", Total:7 },
{ Name:"Item 3", Total:3},
{ Name:"Item 4", Total:12}];
再次感谢!
您可以使用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>
相关文章:
- 实例创建(JS)
- 如何创建js常量对象与"依赖/相对;价值观
- 使用ES6模块创建JS
- 在动画CC中创建JS JavasScript框架脚本
- 如何禁用创建 js 补间
- 从Dart创建js对象
- 在PHP中使用逗号创建JS对象
- 根据格式Y-m-d H:i:s创建JS Date类
- 从滑块输出创建 JS 公式
- Grails创建JS文件并从GSP引用它
- 使用动态函数调用创建 JS 库
- 使用处理程序动态创建 JS 文件 ASP.net
- 创建JS播放儿童电影剪辑时间线
- 创建JS工作,但不显示工具栏
- 为嵌套对象创建JS容器递归循环
- 只运行一次精灵表,创建js
- 正确使用 inject 创建 js
- 创建 JS 对象:使用文字表示法或设置变量
- 创建JS函数添加到阶段
- 在Flash CC中创建js暂停时间线声音