2016-10-03 47 views
0

我加入逗號到我的數據值,如下所示:如何在Chart.JS逗號的條形圖上給出自動生成的y軸值?

ctx.fillText(addCommas(dataset.data[i]), model.x, y_pos); 
. . . 
function addCommas(nStr) { 
    nStr += ''; 
    x = nStr.split('.'); 
    x1 = x[0]; 
    x2 = x.length > 1 ? '.' + x[1] : ''; 
    var rgx = /(\d+)(\d{3})/; 
    while (rgx.test(x1)) { 
     x1 = x1.replace(rgx, '$1' + ',' + '$2'); 
    } 
    return x1 + x2; 
} 

...但條形圖的y軸的「上下文」值是「生」(沒有引號)作爲可以在這裏看到:

enter image description here

我怎樣才能得到這些值更人性化,也通過添加逗號,以便閱讀:

1,900,000 
1,800,000 
1,700,000 
1,600,000 
1,500,000 

回答

2

使用您的手工製作addCommas功能應該做的伎倆:

options: { 
    scales: { 
     yAxes: [{ 
      ticks: { 
       userCallback: function(item) { 
        return addCommas(item); 
       }, 
      } 
     }] 
    } 
} 
相關問題