(轉)數字格式化函數:Highcharts.numberFormat()


一、函數說明
該函數用於圖表中數值的格式化,常見用途有數值精度控制、小數點符、千位符顯示控制等。
 
二、函數使用
 
1、函數構造及參數
Highcharts.numberFormat (Number number, [Number decimals], [String decimalPoint], [String thousandsSep])
 
        參數列表
  • number   需要格式化的數字
  • decimals  小數保留位數,最后一位是四舍五入,默認為 0(可選參數)
  • decimalPoint   小數點符,默認是“.”(可選參數)
  • thousandsSep 千位符,默認是“,” (可選參數)
返回值類型:String
 
 
  2、舉個栗子
對於數字 12223.8723
Highcharts.numberFormat(12223.87)  12,224      (默認精度是0)
Highcharts.numberFormat(12223.87, 2)  12223.87   (保留兩位小數)
Highcharts.numberFormat(12223.87, 2, ",", " ")  12 223,87   (小數點用“,”,千分符用“ ”)
Highcharts.numberFormat(12223.87, 2, ".", "")   12223.87    (不顯示千分符)
 
三、操作實例
餅圖的數據及dataLabels 的格式化函數如下
plotOptions: {
pie: {
    dataLabels: {
        enabled: true,
formatter: function() { 
    return  this.point.name  + this.percentage + '%';
}
    }
}
    },    
    series: [{ 
  type: 'pie', 
name: 'Browser share', 
data: [ 
    ['Firefox', 45.60], 
    ['IE', 26.68],
    { 
name: 'Chrome',
y: 12.68, 
sliced: true, 
selected: true 
    },
    ['Safari', 8.65], 
    ['Opera', 6.62], 
    ['Others', 0.67]
]
    }]
 
這時候我們看到的餅圖文字標簽(dataLabels)為
圖中的數字(dataLabels中的餅圖扇區所占百分比)就會顯示出沒有經過精度控制的內容,利用Highcharts.numberFormat() 我們就可以控制該數值的精度。
formatter: function() { 
return this.point.name + Highcharts.numberFormat(this.percentage,2) + '%';
   }
   
至此已基本說清楚 Highcharts.numberFormat() 函數的作用了,下面說下關於該函數更多用處及數字格式化相關內容。
 
四、相關內容
 
1、需要用到數值格式化函數的地方
在圖表中有很多地方也有可能需要用到數值格式化函數,歸納如下
 
2、用於數值格式化的其他方法
       同樣是格式化,Highcharts還提供了更簡便的方法,也就是 format 字符串 ,例如與 plotOptions.series.dataLabels.formatter 對應的就是 plotOptions.pie.dataLabels.format
 
     
    示例代碼
     plotOptions: {
     pie: {
        dataLabels: {
    enabled: true,
            formatter: function() { 
                return  this.point.name  + this.percentage + '%';
            },
            // 對應的format
            format:"{point.name} + {percentage}";
}
     }
 },    
  也就是 formatter 是函數,format 是格式字符串,關於兩者的區別及優點這里就不多說,我們來說說format是如何進行數值精度控制的。
   formatter: function() { 
    return this.point.name + Highcharts.numberFormat(this.percentage,2) + '%';
 }
 format:"{point.name} {this.percentage:.2f}"
  {this.percentage:.2f} 即 {數值:.精度f}
 
 
轉自:http://bbs.hcharts.cn/article-54-1.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM