Search code examples
javascriptchartsgoogle-visualization

Google Chart vAxis values are not showing


I am working on various graphs and I am showing multiple graphs in a single page. Somehow vAxis values are not showing on some graphs but it showing in some independent (we can say its in different section and triggered manually) graphs.

I have tried everything that I could have.

var data = google.visualization.arrayToDataTable(window.item);
            let options = {
                width: 1410,
                height: 400,
                legend: {position: 'right'},
                bar: {groupWidth: '75%'},
                isStacked: true,
                vAxis: {
                    minValue: 0,
                    title: 'Count',
                    textStyle: {fontSize: 7}
                }
            };
            chart.draw(data, options);

enter image description here


Solution

  • most likely, the chart's container is hidden when it is drawn.
    it should be made visible beforehand.

    see following working snippet, which produces the same result.
    the chart's container is hidden, then shown on the chart's 'ready' event.
    as a result, the vAxis labels are missing.

    google.charts.load('current', {
      packages: ['corechart']
    }).then(function () {
      var data = new google.visualization.DataTable({
        cols: [
          {label: 'x', type: 'string'},
          {label: 'y0', type: 'number'},
          {label: 'y1', type: 'number'},
          {label: 'y2', type: 'number'},
          {label: 'y3', type: 'number'},
        ],
        rows: [
          {c:[{v: 'Column 1'}, {v: 9145.6}, {v: 1000.4}, {v: 0}, {v: 900.4}]},
          {c:[{v: 'Column 2'}, {v: 8123.1}, {v: 0}, {v: 0}, {v: 0}]},
          {c:[{v: 'Column 3'}, {v: 7030.7}, {v: 200.3}, {v: 999.75}, {v: 0}]}
        ]
      });
    
      var options = {
        width: 1410,
        height: 400,
        legend: {position: 'right'},
        bar: {groupWidth: '75%'},
        isStacked: 'true',
        vAxis: {
          minValue: 0,
          title: 'Count',
          textStyle: {fontSize: 7}
        }
      };
    
      var container = document.getElementById('chart_div');
      var chart = new google.visualization.ColumnChart(container);
      google.visualization.events.addListener(chart, 'ready', function () {
        container.className = '';
      });
      chart.draw(data, options);
    });
    .hidden {
      display: none;
      visibility: hidden;
    }
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div class="hidden" id="chart_div"></div>


    when the container is hidden, the chart cannot properly size or place chart elements.
    ensuring the chart is visible before drawing will ensure proper rendering.

    see following working snippet...

    google.charts.load('current', {
      packages: ['corechart']
    }).then(function () {
      var data = new google.visualization.DataTable({
        cols: [
          {label: 'x', type: 'string'},
          {label: 'y0', type: 'number'},
          {label: 'y1', type: 'number'},
          {label: 'y2', type: 'number'},
          {label: 'y3', type: 'number'},
        ],
        rows: [
          {c:[{v: 'Column 1'}, {v: 9145.6}, {v: 1000.4}, {v: 0}, {v: 900.4}]},
          {c:[{v: 'Column 2'}, {v: 8123.1}, {v: 0}, {v: 0}, {v: 0}]},
          {c:[{v: 'Column 3'}, {v: 7030.7}, {v: 200.3}, {v: 999.75}, {v: 0}]}
        ]
      });
    
      var options = {
        width: 1410,
        height: 400,
        legend: {position: 'right'},
        bar: {groupWidth: '75%'},
        isStacked: 'true',
        vAxis: {
          minValue: 0,
          title: 'Count',
          textStyle: {fontSize: 7}
        }
      };
    
      var container = document.getElementById('chart_div');
      var chart = new google.visualization.ColumnChart(container);
      chart.draw(data, options);
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div class="hidden" id="chart_div"></div>