簡體   English   中英

如何在Plottable.js Piechart標簽中將數字精度格式化為兩位小數

[英]How to format number precision to two digit decimals in Plottable.js Piechart labels

我有以下代碼:

 var store = [{ Name:"Item 1", Total:18.73424242 }, { Name:"Item 2", Total:7.34311 }, { Name:"Item 3", Total:3.1235535}, { Name:"Item 4", Total:12.763574}]; 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> 

如何在餅圖中將值格式化為兩位數的小數位數: 18.73, 7.34, 3.12, etc2

使用d3.format().toFixed()

 var pie = new Plottable.Plots.Pie()   
 .attr("fill", function(d){return d.Name; }, colorScale)   
 .addDataset(new Plottable.Dataset(store))
 .sectorValue(function(d){ return +d3.format('0.2f')(d.Total);})   
  //.sectorValue(function(d){ return +d.Total.toFixed(2);})   //works too
 .labelsEnabled(true);

這是要點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM