How to give style to x-axis of google charts?

2019-02-24 23:05发布

I am implementing google charts and I want to give style to x-axis data. I found there the following code to do this.

hAxis:{
        title: 'Month',
        textStyle :{
                    fontSize : 10
        }
}

I want to underline the text data and change cursor style to the pointer. I searched on the google chart website but didn't get.

1条回答
唯我独甜
2楼-- · 2019-02-24 23:58

you can modify the text elements directly,
when the 'ready' event fires on the chart

you can select which axis labels to modify by using the 'text-anchor' attribute

x-axis labels have a value of 'middle', y-axis 'end'

see following working snippet...

google.charts.load('current', {
  callback: function () {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Category');
    data.addColumn('number', 'Value');
    data.addRows([
       ['Category A', 200],
       ['Category B', 110],
       ['Category D', 310],
       ['Category E', 280],
       ['Category F', 175]
    ]);
    var options = {
      hAxis: {
        textStyle: {
          fontSize : 10
        },
        title: 'Month'
      },
      height: 400,
      width: 600
    };

    var chartContainer = document.getElementById('chart_div');
    var chart = new google.visualization.ComboChart(chartContainer);

    google.visualization.events.addListener(chart, 'ready', function () {
      // modify x-axis labels
      var labels = chartContainer.getElementsByTagName('text');
      Array.prototype.forEach.call(labels, function(label) {
        if (label.getAttribute('text-anchor') === 'middle') {
          label.style.textDecoration = 'underline';
          label.style.cursor = 'pointer';
        }
      });
    });

    chart.draw(data, options);
  },
  packages: ['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

查看更多
登录 后发表回答