简体   繁体   English

Google Charts JSON问题

[英]Google Charts JSON problems

I am trying to access JSON from the Flurry API to graph information that is there. 我正在尝试从Flurry API访问JSON以图形化那里的信息。 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. 但是,当我尝试运行图形时,我在发送数组的空间中得到了Data column(s) for axis #0 cannot be of type string的“ Data column(s) for axis #0 cannot be of type string警报Data column(s) for axis #0 cannot be of type string 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/ https://jsfiddle.net/9j5x97r7/2/

The jsfiddle Is not working because i couldnt load the API and i dont have time. jsfiddle无法正常工作,因为我无法加载API,而且我没有时间。 But if you make it load it works (try in your code). 但是,如果让它加载,它就可以工作(在您的代码中尝试)。

The first row is different as you made. 第一行与您所做的不同。 The for is wrong for是错误的

And you need to parse the number because in the response is a string. 您需要解析数字,因为响应中是一个字符串。

Let me know if it helps you 让我知道是否对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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