Bogdan
Bogdan

Reputation: 85

Customize hAxis ticks colors in Google Bar Charts

Is it possible to change the color of the ticks on the xAxis bar?

See this image

I want to make the ticks from 2000 green, the 2500 orange and the 3000 red.

I've tried this, but it doesn't seem to work:

ticks: [500, 1000, 1500, {v: 2000, color: "Green"}, {v: 2500, color: "Orange"}, {v: 3000, color: "Red"}]

ticks: [500, 1000, 1500, [2000, "Green"], [2500, "Orange"], [3000, "Red"]]

Any help is appreciated, thank you!

Upvotes: 1

Views: 1137

Answers (1)

WhiteHat
WhiteHat

Reputation: 61222

there are no config options for changing the color of individual ticks

but you can change manually, on the chart's 'ready' event,
see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = new google.visualization.arrayToDataTable([
    ['Category', 'value'],
    ['a', 2924],
    ['b', 2075],
    ['c', 2516],
    ['d', 2153],
    ['e', 2348],
    ['f', 1925]
  ]);

  var container = document.getElementById('chart_div');
  var chart = new google.visualization.BarChart(container);

  google.visualization.events.addListener(chart, 'ready', changeTickColors);

  chart.draw(data, {
    legend: {
      alignment: 'end',
      position: 'top'
    },
    hAxis: {
      ticks: [500, 1000, 1500, 2000, 2500, 3000]
    }
  });

  function changeTickColors() {
    var tickColors = {
      "2,000": "green",
      "2,500": "orange",
      "3,000": "red"
    };

    Array.prototype.forEach.call(container.getElementsByTagName('text'), function(label) {
      if (label.getAttribute('text-anchor') === 'middle') {
        if (tickColors.hasOwnProperty(label.textContent)) {
          label.setAttribute('fill', tickColors[label.textContent]);
        }
      }
    });
  }
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Upvotes: 4

Related Questions