Google图表SQL和Json [英] Google chart SQL and Json

查看:91
本文介绍了Google图表SQL和Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找添加SQL插入到我的谷歌图表。我查看了API中的google示例,但是它使用包含值的集合文件。我已经在这里看到了一些例子,但它们看起来相当复杂。



我想用来自我的数据库的数据填充图表,我使用折旧的代码,它只是一个例子。我已阅读有关使用 json_encode 。然而,当我将它应用于我的输出时,我得到的报价根据下面的google示例是不需要的。



总之,如何才能在下面的图表中显示这个简单的查询?

查询示例

  while($ row = mysqli_fetch_array($ result))
{
$ data1 [] =['。 $ row1 ['Date']。 ','。 $ row1 ['SpeciesB']。 ];
echo['。 $ row ['Date']。 ','。 $ row ['SpeciesA']。 ','。 $ row ['SpeciesB']。 ],;
回显< br>;
}

输出

  ['2013-08-06','1','1'],
['2013-08-13','1' ,'2'],
['2013-08-20','2','2'],
['2013-08-27','3','1'],
['2013-09-17','4','1'],



print_r($ data1);



<$数组([0] => ['2013-08-06','1'] [1] => ['2013-08-13','2'] [2] => ['2013-08-20','2'] [3] => ['2013-08-27','1'] [4] => ['2013-09- 17','1'])

谷歌期望的示例

 < script type =text / javascript> 
google.load(visualization,1,{packages:[corechart]});
google.setOnLoadCallback(drawChart);
函数drawChart(){
var data = google.visualization.arrayToDataTable([
['Date','Species A','Species B'],
['20 2006年1月1日],
['20 / 06/2006',3,2],
['20 / 07/2006',2,2],
['20 / 08/2006',3,1]
]);

var options = {
title:'Site Dynamics'
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data,options);
}
< / script>


解决方案

您想使用json_encode来处理细节因为你目前的方法会在Internet Explorer中遇到问题(在你的最后一行数据后面,你将总是会得到一个训练逗号,这将导致IE弹出)。试试这个:

  $ dataArray = array(array('Date','Species A','Species B')) ; 
while($ row = mysqli_fetch_array($ result)){
//根据需要将SpeciesA和SpeciesB值解析为整数(int)或浮点数(float)
$ dataArray [] = array($ row ['Date'],(int)$ row ['SpeciesA'],(int)$ row ['Speciesb']);
}

和javascript:

  var data = google.visualization.arrayToDataTable(<?php echo json_encode($ dataArray);?>); 


Looking to add SQL insert into my google charts. I looked at the google example in the API, however that uses a set file containing the values. I have seen a few examples on here but they seem rather complicated.

I want to populate the chart with data from my DB, im using depreciated code, its just an example. I have read about using json_encode. However when I apply it to my output I get quotations which according to the google example below are not needed.

In short how would I go about getting this simple query displaying in the chart below?

Query example

while($row = mysqli_fetch_array($result))
 {
   $data1[]=    "['" . $row1['Date'] . "','" . $row1['SpeciesB'] . "']";
   echo   "['" . $row['Date'] . "','" . $row['SpeciesA'] . "','" . $row['SpeciesB'] . "'],";
 echo "<br>";
 }

Outputs

 ['2013-08-06','1','1'],
 ['2013-08-13','1','2'],
 ['2013-08-20','2','2'],
 ['2013-08-27','3','1'],
 ['2013-09-17','4','1'],

or

print_r($data1);

  Array ( [0] => ['2013-08-06','1'] [1] => ['2013-08-13','2'] [2] => ['2013-08-20','2'] [3] => ['2013-08-27','1'] [4] => ['2013-09-17','1'] ) 

Example of what google expects

<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Date', 'Species A', 'Species B'],
      ['20/05/2006',  1,      1],
      ['20/06/2006',  3,      2],
      ['20/07/2006',  2,      2],
      ['20/08/2006',  3,      1]
    ]);

    var options = {
      title: 'Site Dynamics'
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>      

解决方案

You want to use json_encode to take care of the details of building your output for you, as your current method will run into problems in Internet Explorer (you will always have a training comma after your last row of data, which will cause IE to bomb). Try this instead:

$dataArray = array(array('Date', 'Species A', 'Species B'));
while($row = mysqli_fetch_array($result)) {
    // parse the "SpeciesA" and "SpeciesB" values as integer (int) or floating point (float), as appropriate
    $dataArray[] = array($row['Date'], (int) $row['SpeciesA'], (int) $row['Speciesb']);
}

and in the javascript:

var data = google.visualization.arrayToDataTable(<?php echo json_encode($dataArray); ?>);

这篇关于Google图表SQL和Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆