highcharts jquery 脚本中的舍入结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9085644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding results in highcharts jquery script
提问by Robert E
I know this is a bit out there... but gonna ask anyways. I'm using highcharts jquery script (http://www.highcharts.com/) to generate a pie chart. I am trying to round off the number results in the pie chart and cannot find any documentation to do so. I'm stuck!
我知道这有点过时了……但还是要问一下。我正在使用 highcharts jquery 脚本 (http://www.highcharts.com/) 生成饼图。我试图四舍五入饼图中的数字结果,但找不到任何文档来这样做。我被卡住了!
My data looks something like this:
我的数据看起来像这样:
data: [
['Equity', 3],
['Cash', 6]
]
And the pie chart outputs: 33.333333333333 and 66.666666666666
饼图输出: 33.333333333333 和 66.666666666666
I'd rather get the results rounded up and down respectively so it reads and shows 33 and 64. Does anyoone know how this can be set up in highcharts?
我宁愿将结果分别向上和向下取整,以便读取并显示 33 和 64。有人知道如何在 highcharts 中设置它吗?
回答by Yanofsky
In in the tooltip option in the configuration object use Math.round() in the formatter function.
在配置对象的工具提示选项中,在格式化程序函数中使用 Math.round()。
tooltip: {
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' %';
}
},
回答by caktux
tooltip: {
valueDecimals: 2
},
回答by eolsson
There's a numberFormat
function available in the Highcharts API that you can use (see http://www.highcharts.com/ref/#highcharts-object).
numberFormat
您可以使用 Highcharts API 中的一个函数(请参阅http://www.highcharts.com/ref/#highcharts-object)。
Quoted from API doc:
引用自 API 文档:
numberFormat(Number number, [Number decimals], [String decimalPoint], [String thousandsSep]) : String
numberFormat(数字编号,[数字小数],[字符串小数点],[字符串千分之一]):字符串
Formats a JavaScript number with grouped thousands, a fixed amount of decimals and an optional decimal point. It is a port of PHP's function with the same name. See PHP number_format for a full explanation of the parameters.
使用成组的千位、固定数量的小数和一个可选的小数点来格式化 JavaScript 数字。它是 PHP 函数的同名端口。有关参数的完整说明,请参阅 PHP number_format。
tooltip: {
formatter: function() {
return ''+ this.series.name +''+
this.x +': '+ Highcharts.numberFormat(this.y, 0, ',') +' millions';
}
}, ...
Parameters
参数
- number: Number The raw number to format.
- decimals: Number The desired number of decimals.
- decimalPoint: String The decimal point. Defaults to "." or to the string specified globally in options.lang.decimalPoint.
- thousandsSep: String The thousands separator. Defaults to "," or to the string specified globally in options.lang.thousandsSep.
- number: Number 要格式化的原始数字。
- decimals: Number 所需的小数位数。
- decimalPoint: String 小数点。默认为“。” 或在 options.lang.decimalPoint 中全局指定的字符串。
- 千位分隔符:字符串千位分隔符。默认为 "," 或在 options.lang.thousandsSep 中全局指定的字符串。
Returns
退货
A string with with the input number formatted.
带有输入数字格式的字符串。
回答by Ricardo Alvaro Lohmann
Instead of use formatter
you can set yDecimals
as 2
:
相反,使用formatter
你可以设置yDecimals
为2
:
tooltip: {
yDecimals: 2
}
yDecimals: Number
How many decimals to show in each series' y value. This is overridable in each series' tooltip options object. The default is to preserve all decimals.
y小数:Number
How many decimals to show in each series' y value. This is overridable in each series' tooltip options object. The default is to preserve all decimals.
回答by Batjaa
tooltip: {
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage*100)/100 +' %';
}
},
回答by RailsZilla.com
try
尝试
percentageDecimals: 0
in your tooltip
在您的工具提示中