Google Charts - placing custom formatting axis val

2019-08-09 03:11发布

问题:

I have some numeric data (e.g. "1, 2, 3, 4, 5..") based on which I want to build a chart. However, instead of raw numbers, I want to show other text as axis values.

For example, instead of number "1" I want to display "one"

Is that possible to do with Google Charts?

回答1:

using object notation, you can provide the value (v:) and the formatted value (f:) for each tick

{v: 1, f: 'one'}

see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    [1, 1],
    [2, 2],
    [3, 3],
    [4, 4],
    [5, 5]
  ], true);

  var options = {
    hAxis: {
      ticks: [
        {v: 1, f: 'one'},
        {v: 2, f: 'two'},
        {v: 3, f: 'three'},
        {v: 4, f: 'four'},
        {v: 5, f: 'five'}
      ]
    }
  };

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