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

后端 未结 1 2018
抹茶落季
抹茶落季 2021-01-21 02:54

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\',
        t         


        
1条回答
  •  清歌不尽
    2021-01-21 03:24

    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']
    });
    
    

    0 讨论(0)
提交回复
热议问题