简体   繁体   中英

Google Charts JSON problems

I am trying to access JSON from the Flurry API to graph information that is there. However, when I am trying to run the graph, I get the Data column(s) for axis #0 cannot be of type string alert in the space where I am sending the array. The following is my code:

google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(fillData);

function fillData() { 
  var dataArray = [];
  var tempArray = ['@name', '@totalCount'];

  dataArray.push(tempArray);
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open('GET', '//flurry address', true);
  xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4) {
    if(xmlhttp.status == 200) {
        var obj = JSON.parse(xmlhttp.responseText);
        for (i = 0;i < obj.parameters.key[0].value[i]["@name"]; i++)
        {
          dataArray.push([obj.parameters.key[0].value[i]["@name"], obj.parameters.key[0].value[i]["@totalCount"]]);
          alert(dataArray[i]);
        }
     }
  }
};
  //xmlhttp.send(null);

drawBasic(dataArray);

}


function drawBasic(dataarray) {
  var data = google.visualization.arrayToDataTable(dataarray);

  var options = {
    title: 'Login Counts',
    chartArea: {width: '50%'},
    hAxis: {
      title: 'Login Count',
      minValue: 0
    },
    vAxis: {
      title: 'Company'
    }
  };

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

  chart.draw(data, options);
}

Try the code that is shown here:

https://jsfiddle.net/9j5x97r7/2/

The jsfiddle Is not working because i couldnt load the API and i dont have time. But if you make it load it works (try in your code).

The first row is different as you made. The for is wrong

And you need to parse the number because in the response is a string.

Let me know if it helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM