如何在不改变Google Chat的顺序的情况下添加百分比标签

时间:2017-03-15 00:21:57

标签: javascript charts google-visualization

我想在数字标签上添加百分比标记并为其找到一个选项。

{hAxis: {format: 'percent'}}

但它也将数字加倍100

  
    

例如,{hAxis:{format:'#,###%'}}显示值" 1,000%"," 750%&# 34;和" 50%"对于值10,7.5和0.5。

  

https://developers.google.com/chart/interactive/docs/customizing_axes#number-formats

如何在标签中添加%

1 个答案:

答案 0 :(得分:0)

您可以尝试添加自定义轴标签,或ticks ...

使用对象表示法,您可以同时提供...

值(v:)和格式化值(f:

然后使用谷歌的NumberFormat课程,手动构建每个tick ...

var formatPercent = new google.visualization.NumberFormat({
  suffix: '%'
});

...

ticks.push({
  v: data.getValue(i, 0),
  f: formatPercent.formatValue(data.getValue(i, 0))
});

...

hAxis: {
  ticks: ticks
}

请参阅以下工作代码段...



google.charts.load('current', {
  callback: drawChart,
  packages:['corechart']
});
function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['X', 'Y'],
    [1, 1],
    [10, 2],
    [20, 3],
    [30, 4]
  ]);

  var formatPercent = new google.visualization.NumberFormat({
    pattern: '#,##0',
    suffix: '%'
  });

  var ticks = [];
  for (var i = 0; i < data.getNumberOfRows(); i++) {
    ticks.push({
      v: data.getValue(i, 0),
      f: formatPercent.formatValue(data.getValue(i, 0))
    });
  }

  var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  chart.draw(data, {
    hAxis: {
      ticks: ticks
    }
  });
}
&#13;
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
&#13;
&#13;
&#13;