使用" google.visualization.NumberFormat"添加后缀

时间:2016-05-31 14:17:49

标签: javascript google-visualization number-formatting suffix google-gauges

一直试图添加'%'除了Google Gauge图表上的价值之外,我确定它是一个我不知道的愚蠢。

以下是我尝试过的内容,发布了另一篇文章:Adding a % sign to Google Gauges

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

                function drawChart() {

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

                    var options = {
                        redFrom: 0,
                        redTo: 35,
                        yellowFrom: 35,
                        yellowTo: 65,
                        greenFrom: 65,
                        greenTo: 100,
                        minorTicks: 5
                    };
                   var formatter = new google.visualization.NumberFormat( {suffix: '%'} ); formatter.format(data,1);
function handleQueryResponse(response) {
                        var data = response.getDataTable();
                        new google.visualization.Gauge(document.getElementById('gaugechart')).draw(data);
                    }
                }

任何朝着正确方向的推动都会非常感激,请温柔......

仅供参考我这是Stack Exchange中的第一篇文章,任何格式错误或问题都不让我知道。

1 个答案:

答案 0 :(得分:1)

在尝试使用formatter之前,

需要等到数据返回 将其移到handleQueryResponse



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', suffix: '%'} );
      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>
&#13;
&#13;
&#13;