舍入导致highcharts jquery脚本

Rob*_*t E 19 jquery rounding highcharts

我知道这有点......但反正会问.我正在使用highcharts jquery脚本(http://www.highcharts.com/)来生成饼图.我试图在饼图中舍入数字结果,但找不到任何文档.我被卡住了!

我的数据看起来像这样:

data: [
    ['Equity',   3],
    ['Cash',     6]
]
Run Code Online (Sandbox Code Playgroud)

饼图输出:33.333333333333和66.666666666666

我宁愿分别上下调整结果,所以它读取并显示33和64.是否有人知道如何在highcharts中设置它?

Yan*_*sky 45

在配置对象的工具提示选项中,在formatter函数中使用Math.round().

   tooltip: {
     formatter: function() {
        return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' %';
     }
  },
Run Code Online (Sandbox Code Playgroud)


cak*_*tux 31

tooltip: {
  valueDecimals: 2
},
Run Code Online (Sandbox Code Playgroud)


eol*_*son 22

numberFormat您可以使用Highcharts API中提供的功能(请参阅http://www.highcharts.com/ref/#highcharts-object).

引自API doc:

numberFormat(Number number,[Number decimals],[String decimalPoint],[String thousandsSep]):String

格式化编号为千位的JavaScript编号,固定的小数位数和可选的小数点.它是PHP函数的一个端口,具有相同的名称.有关参数的完整说明,请参阅PHP number_format.

tooltip: {
    formatter: function() {
        return ''+ this.series.name +''+
            this.x +': '+ Highcharts.numberFormat(this.y, 0, ',') +' millions';
    }
}, ...
Run Code Online (Sandbox Code Playgroud)

参数

  • number:Number要格式化的原始数字.
  • decimals:Number所需的小数位数.
  • decimalPoint:String小数点.默认为"." 或者在options.lang.decimalPoint中全局指定的字符串.
  • thousandsSep:String千位分隔符.默认为","或者在options.lang.thousandsSep中全局指定的字符串.

返回

带有输入数字格式的字符串.


Ric*_*ann 6

而不是使用formatter你可以设置yDecimals2:

tooltip: {
    yDecimals: 2
}
Run Code Online (Sandbox Code Playgroud)

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.