Material Axis Format Example

An example of a Google Material Chart with Axis Formats.

by Sergio Spagnuolo

HTML

<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['bar']}]}"></script>
       <div id="chart_div"></div>
    <br/>
    <div id="btn-group">
      <button class="button button-blue" id="none">Aprovadas</button>
      <button class="button button-blue" id="scientific">Scientific Notation</button>
      <button class="button button-blue" id="decimal">Decimal</button>
      <button class="button button-blue" id="short">Short</button>
    </div>

JavaScript

google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Ano', 'A favor', 'Absteve', 'Contrário', 'Consenso'],
          ['2015', 4, 1, 0, 7],
          ['2014', 10, 4, 4, 14],
          ['2013', 11, 2, 0, 18],
          ['2012', 5, 1, 0, 13],
          ['2011', 9, 2, 0, 26],
          ['2010', 5, 4, 1, 24],
          ['2009', 7, 6, 1, 24],
          ['2018', 11, 6, 0, 24],
          ['2017', 8, 4, 0, 23],
          ['2006', 3, 0, 0, 5]
        ]);

        var options = {
          chart: {
            title: 'Company Performance',
            subtitle: 'Sales, Expenses, and Profit: 2014-2017',
          },
          bars: 'horizontal', // Required for Material Bar Charts.
          hAxis: {format: 'decimal'},
          height: 400,
          colors: ['#1b9e77', '#d95f02', '#7570b3', 'a7002c']
        };

        var chart = new google.charts.Bar(document.getElementById('chart_div'));

        chart.draw(data, google.charts.Bar.convertOptions(options));

        var btns = document.getElementById('btn-group');

        btns.onclick = function (e) {

          if (e.target.tagName === 'BUTTON') {
            options.hAxis.format = e.target.id === 'none' ? '' : e.target.id;
            chart.draw(data, google.charts.Bar.convertOptions(options));
          }
        }
      }