将Google Charts Column的Y轴从数字更改为自定义名称

时间:2015-05-11 00:49:51

标签: javascript charts google-visualization

我想将Google Charts Column的Y轴从数字更改为自定义名称。例如,在Y轴上会有" F,E,D,C,B,A"或者"最坏,坏,可接受,好,最好"。这可以在Google Charts Column中使用吗? https://jsfiddle.net/api/post/library/pure/

使用Javascript:

google.setOnLoadCallback(drawStuff);

function drawStuff() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Country');
  data.addColumn('number', 'GDP');
  data.addRows([
    ['US', 16768100],
    ['China', 9181204],
    ['Japan', 4898532],
    ['Germany', 3730261],
    ['France', 2678455]
  ]);

 var options = {
   title: 'GDP of selected countries, in US $millions',
   width: 500,
   height: 300,
   legend: 'none',
   bar: {groupWidth: '95%'},
   vAxis: { gridlines: { count: 4 } }
 };

 var chart = new google.visualization.ColumnChart(document.getElementById('number_format_chart'));
 chart.draw(data, options);

 document.getElementById('format-select').onchange = function() {
   options['vAxis']['format'] = this.value;
   chart.draw(data, options);
 };

};

1 个答案:

答案 0 :(得分:2)

您应该使用vAxis上的'ticks'选项,并包含每个数值的格式化值。 e.g。

vAxis: { ticks: [{v: 200000, f: 'best'}, {v: 400000, f: 'good'}, ... ] }