2016-02-02 46 views
2

我正在嘗試更改Google測量儀上軸的數字格式。默認情況下,軸值是一個整數(例如0,120),但我想顯示英鎊和格式爲貨幣(例如£0,£120)。我已經能夠使用NumberFormat更改儀表底部顯示的值的格式,但不能應用於儀表的其餘部分。

的基本計的代碼如下:

軸的數字格式 - Google Gauge

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
<script type="text/javascript"> 
    google.charts.load('current', {'packages':['gauge']}); 
    google.charts.setOnLoadCallback(drawChart); 

    function drawChart() { 

     var data1 = google.visualization.arrayToDataTable([ 
      ['Label', 'Value'], 
      ['Spend', 80], 
     ]); 
     var options1 = { 
      width: 500, height: 300 
     }; 

     // change number format to £ 
     var formatter = new google.visualization.NumberFormat(
     {pattern: '£###,###'}); 
     formatter.format(data1, 1); 

     var chart = new google.visualization.Gauge(document.getElementById('chart_div1')); 
     chart.draw(data1, options1); 

    } 
</script> 
</head> 

<body> 
<div id="chart_div1"></div> 
</body> 
</html> 

如何添加一個£標誌的數字儀表上的外?謝謝。

回答

1

可以提供字符串majorTicks數組來指定自己的...

google.charts.load('current', {'packages':['gauge']}); 
 
google.charts.setOnLoadCallback(drawChart); 
 

 
function drawChart() { 
 
    var data1 = google.visualization.arrayToDataTable([ 
 
     ['Label', 'Value'], 
 
     ['Spend', 80], 
 
    ]); 
 
    var options1 = { 
 
     width: 500, height: 300, 
 
     majorTicks: [ 
 
     '£0', '', '', '', '£100' 
 
     ] 
 
    }; 
 

 
    // change number format to £ 
 
    var formatter = new google.visualization.NumberFormat({ 
 
     pattern: '£###,###' 
 
    }); 
 
    formatter.format(data1, 1); 
 

 
    var chart = new google.visualization.Gauge(document.getElementById('chart_div1')); 
 
    chart.draw(data1, options1); 
 
}
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<div id="chart_div1"></div>