Search code examples
javascriptgoogle-apigoogle-visualizationgoogle-datatable

Get the H-Axis(X-Axis) value of google area chart


In the google chart datatable object, I can get the current select point value(V-Axis/Y-Axis) using datatable.getValue(...). However, if I wanna get the time/date/year from X-Axis(see screenshot). I did not find any datatable's function to achieve that. Does anyone know how??

enter image description here

This is my code

google.visualization.events.addListener(chart, 'select', function(a, b, c, d) {
            var selectedItem = chart.getSelection()[0];
            if (selectedItem) {
              // Get the current Y-axis value which is 1120
              var value = data.getValue(selectedItem.row, selectedItem.column);
              alert('The user selected ' + value);
              // How I can get the value of 2015 which is the X-axis value??????
            }
        });

Solution

  • In most cases, your axis value will be in column 0, so just change out selectedItem.column for 0, and you will have the axis value:

    var axisValue = data.getValue(selectedItem.row, 0);