Rounding results in highcharts jquery script

后端 未结 6 1292
庸人自扰
庸人自扰 2021-02-07 04:20

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 n

相关标签:
6条回答
  • 2021-02-07 04:39

    There's a numberFormatfunction available in the Highcharts API that you can use (see http://www.highcharts.com/ref/#highcharts-object).

    Quoted from API doc:

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

    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.

    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.

    Returns

    A string with with the input number formatted.

    0 讨论(0)
  • 2021-02-07 04:40

    Instead of use formatter you can set yDecimals as 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.

    0 讨论(0)
  • 2021-02-07 04:41

    try

    percentageDecimals: 0 
    

    in your tooltip

    0 讨论(0)
  • 2021-02-07 04:42

    In in the tooltip option in the configuration object use Math.round() in the formatter function.

       tooltip: {
         formatter: function() {
            return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' %';
         }
      },
    
    0 讨论(0)
  • 2021-02-07 04:53
    tooltip: {
      valueDecimals: 2
    },
    
    0 讨论(0)
  • 2021-02-07 04:54
    tooltip: {
        formatter: function() {
            return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage*100)/100 +' %';
        }
    },
    
    0 讨论(0)
提交回复
热议问题