带有尾随零的Google Gauge Float值

时间:2016-06-05 15:44:21

标签: google-visualization

我正在使用Google Gauge Chart。一切都工作正常,除了我注意到我给它一个浮点值,如果值是一个整数(例如17.0),仪表显示17为值而不是17.0。如果我将仪表17.3作为值,则它正确显示17.3。有没有办法格式化仪表上的实际值输出?

1 个答案:

答案 0 :(得分:1)

您可以使用NumberFormat class

var formatter = new google.visualization.NumberFormat( {pattern: '0.0'} );
formatter.format(data, 1);

参见以下示例......

google.charts.load('current', {
  callback: function () {
    var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1Z-eKZJ_XbmnoLYyG4cht40KMo07_CYnLP-hPi1ZBjnw/edit#gid=0');
    query.send(handleQueryResponse);

    function handleQueryResponse(response) {
      var data = response.getDataTable();

      var formatter = new google.visualization.NumberFormat( {pattern: '0.0'} );
      formatter.format(data, 1);

      var options = {
        redFrom: 0,
        redTo: 35,
        yellowFrom: 35,
        yellowTo: 65,
        greenFrom: 65,
        greenTo: 100,
        minorTicks: 5
      };

      new google.visualization.Gauge(document.getElementById('gaugechart')).draw(data, options);
    }
  },
  packages: ['gauge']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="gaugechart"></div>