Deepu Sasidharan
Deepu Sasidharan

Reputation: 5309

Y axis values in google column chart showing in reverse order

Y axis values in google column chart showing in reverse order. Please take a look the result I got.

enter image description here

And my code is:

HTML

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<div class="col-sm-6" id="job_chart" style="width: 800px; height: 500px;"></div> 

jquery

$.post("/emp_action_ajax.html",{'ajaxOp':'loadJobChart'},function(data){
    var data_array = $.parseJSON(data);
    var dataarray = [["Date", "Job"]];
        for (var i = 0; i < data_array.length; i++) {
            var sub = [data_array[i].date, data_array[i].jcount];
            dataarray.push(sub);
        }
     drawStuff(dataarray);
    });

    google.load("visualization", "1.1", {packages: ['corechart', 'bar']});
      function drawStuff(dataArray) {
      var data = new google.visualization.arrayToDataTable(dataArray);

        var options = {
          width: 400,
          legend: { position: 'none' },
          chart: {
            title: 'Year and month wise job count',
            subtitle: '' },
          axes: {
            x: {
                0: { side: 'bottom', label: 'Month-Year'} // Bottom x-axis.
            },
            /*y: {
                0: { side: 'bottom', label: 'Job', direction: '-1'}
            }*/
          },
          /*vAxis: {
              title: 'Job',
              direction: '1'
            },*/
          bar: { groupWidth: "50%" }
        };

        var chart = new google.charts.Bar(document.getElementById('job_chart'));
      chart.draw(data, google.charts.Bar.convertOptions(options));
      };

What I tried

y: {
0: { side: 'bottom', label: 'Job', direction: '-1'}
 }

AND

vAxis: {
                  title: 'Job',
                  direction: '1' //-1
                }

UPADTE

I alert the dataArray inside drawStuff(dataArray) function and get

enter image description here

Upvotes: 1

Views: 1217

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 40639

It is because your data_array[i].jcount value is treated as string to make it work use parseInt() like

for (var i = 0; i < data_array.length; i++) {
     var sub = [data_array[i].date, parseInt(data_array[i].jcount)];
     dataarray.push(sub);
}

Non working Snippet,

var dataArray = [
  ["Date", "Job"],
  ['2017-1', '5'],
  ['2017-4', '2']
];
google.load("visualization", "1.1", {
  packages: ['corechart', 'bar']
});
google.setOnLoadCallback(drawStuff);

function drawStuff() {
  var data = new google.visualization.arrayToDataTable(dataArray);

  var options = {
    width: 400,
    legend: {
      position: 'none'
    },
    chart: {
      title: 'Year and month wise job count',
      subtitle: ''
    },
    axes: {
      x: {
        0: {
          side: 'bottom',
          label: 'Month-Year'
        } // Bottom x-axis.
      }
    },
    vAxis: {
      title: 'Job',
      direction: '-1'
    },
    bar: {
      groupWidth: "50%"
    }
  };

  var chart = new google.charts.Bar(document.getElementById('job_chart'));
  chart.draw(data, google.charts.Bar.convertOptions(options));
};
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<div class="col-sm-6" id="job_chart" style="width: 800px; height: 500px;"></div>

Working Snippet,

var dataArray = [
  ["Date", "Job"],
  ['2017-1', 5],
  ['2017-4', 2]
];
google.load("visualization", "1.1", {
  packages: ['corechart', 'bar']
});
google.setOnLoadCallback(drawStuff);

function drawStuff() {
  var data = new google.visualization.arrayToDataTable(dataArray);

  var options = {
    width: 400,
    legend: {
      position: 'none'
    },
    chart: {
      title: 'Year and month wise job count',
      subtitle: ''
    },
    axes: {
      x: {
        0: {
          side: 'bottom',
          label: 'Month-Year'
        } // Bottom x-axis.
      }
    },
    vAxis: {
      title: 'Job',
      direction: '-1'
    },
    bar: {
      groupWidth: "50%"
    }
  };

  var chart = new google.charts.Bar(document.getElementById('job_chart'));
  chart.draw(data, google.charts.Bar.convertOptions(options));
};
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<div class="col-sm-6" id="job_chart" style="width: 800px; height: 500px;"></div>

Upvotes: 2

Related Questions