在工具提示上显示Google图表的百分比。

6

我希望使用谷歌图表展示一个比例,使用百分数。我知道如何将vAxis更改为百分数:

vAxis: {format:'#%'},

但问题是,我的数据在工具提示中显示的不是百分比,而是十进制值(0.85而不是预期的85%)

输入图像描述

以下是我的代码:

  google.load("visualization", "1", {packages:["corechart"]});
        google.setOnLoadCallback(function() {})
        function drawChart(data) {
          var data = google.visualization.arrayToDataTable(data);

          var options = {
            title: "Pourcentage de production et productivité de l'entreprise",
            chartArea: {top:39},
            vAxis: {format:'#%'},
            pointSize:2
          }

          var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
          chart.draw(data, options);
        }

drawChart([['Year', '% de production', 'Productivité'],[ '06/03/2013', 0.85 , 0.58 ],[ '07/03/2013', 0.85 , 0.58 ],[ '23/03/2013', 0.85 , 0.58 ],[ '25/03/2013', 1 , 1.5 ],[ '26/03/2013', 0.72 , 0.63 ],[ '04/04/2013', 1 , 1.57 ]])
1个回答

13
您可以使用格式化程序

以下是一个示例(将此代码插入绘制图表之前):

  var formatter = new google.visualization.NumberFormat({pattern:'#,###%'});
  formatter.format(data, 1);
  formatter.format(data, 2);
您可以使用的格式是ICU DecimalFormat的一个子集,如果您想要在我提供的格式上稍微做出一些更改。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接