简体   繁体   English

Google Charts JSON调用可能出现计时错误

[英]Possible timing error with Google Charts JSON call

I've spent the best part of a day attempting a pie chart, and now I have a pie chart, it tells me lies! 我花了一天的大部分时间尝试饼图,现在我有了饼图,它告诉我说谎! It says there is one series, accounting for 100% which is incorrect! 它说有一个系列,占100%,这是不正确的! Could someone please tell me where I am going wrong. 有人可以告诉我我要去哪里了。

So far, I have this: 到目前为止,我有这个:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js">       
</script>
<script type="text/javascript" src="https://www.google.com/jsapi">      
</script>
<script type="text/javascript">

google.load("visualization", "1", {packages:["corechart"]});

function drawChart() {

  var jsonData = $.ajax({
  url: "getdata_searches.php",
  dataType:"json",
  async: false
  }).responseText;

// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);

var options = {
  title: 'Searches By Type'
};

var chart = new google.visualization.PieChart(document.getElementById('piechart'));

chart.draw(data, options);
}

google.setOnLoadCallback(drawChart);
</script>

My getdata_searches.php file is so: 我的getdata_searches.php文件是这样的:

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 

$sql="Select Other, Count(Other) As Count From activity WHERE tel = '0000' AND Other <> '' Group By Other Order By Count(Other) Desc;";

$result = $conn->query($sql);

$data = array('cols' => array(array('label' => 'Other', 'type' => 'string'),
                        array('label' => 'Count', 'type' => 'number')),
              'rows' => array());

while ($row = $result->fetch_assoc()) {
    $data['rows'][] = array('c' => array(array('v' => $row["Other"]), array('v' => $row["Count"])));
}

echo json_encode($data);

which produces: 产生:

{
"cols":[
    {"label":"Other","type":"string"},
    {"label":"Count","type":"number"}],
"rows":[
    {"c":[{"v":"AVS Search"},{"v":"215"}]},
    {"c":[{"v":"Urgent Care Centres"},{"v":"50"}]},
    {"c":[{"v":"Emer. Depts"},{"v":"37"}]},
    {"c":[{"v":"Adult Trauma Networks"},{"v":"19"}]}
    ]
}

I've gone round and round in circles on this one, I must be missing something obvious, please help! 我已经绕过这一圈了,我一定缺少明显的东西,请帮忙!

The problem appears to be the values for the "Count" column. 问题似乎出在“计数”列的值上。

String "215" instead of Number 215 . String "215"而不是Number 215

Created the code snippet using corrected JSON you provided... 使用您提供的更正JSON创建了代码段...

 google.load("visualization", "1", {packages:["corechart"]}); function drawChart() { var data = new google.visualization.DataTable({ "cols":[ {"label":"Other","type":"string"}, {"label":"Count","type":"number"} ], "rows":[ {"c":[{"v":"AVS Search"},{"v":215}]}, {"c":[{"v":"Urgent Care Centres"},{"v":50}]}, {"c":[{"v":"Emer. Depts"},{"v":37}]}, {"c":[{"v":"Adult Trauma Networks"},{"v":19}]} ] }); var options = { title: 'Searches By Type' }; var chart = new google.visualization.PieChart(document.getElementById('piechart')); chart.draw(data, options); } google.setOnLoadCallback(drawChart); 
 <script src="https://www.google.com/jsapi"></script> <div id="piechart"></div> 

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

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