谷歌图表API数据 [英] Google Chart API Data

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

问题描述

我如何填充谷歌图表API与PHPMySQL我自己的服务器端的数据,即

How do I populate the Google Chart API with my own Server side data i.e. in PHPMySQL.

目前,我有以下数据:

function drawChart()
{
 // Create the data table.
 var data = new google.visualization.DataTable();
 data.addColumn('string', 'City');
 data.addColumn('number', 'Number of Crimes');
 data.addRows([
             ['Cardiff', 300],
             ['London', 900],
             ['Manchester', 500],
             ['Dublin', 400],
             ['Liverpool', 600]
             ]);

// Set Chart Options
var options = {
    'legend': 'left',
    'title': 'Crimes (per day)',
    'is3D': 'True',
    'width':400,
    'height':300
};

// Instantiate and Draw Chart.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

但我怎么给它在我的MySQL数据库从表中的数据?

But how do I feed it the data from a table in my mySQL database?

推荐答案

你必须让你的数据从数据库的图表API的Javascript成为code之前:

Before the JavaScript-Code of the Charts API you have to get your data out of your database:

//Your database query goes here
$list = mysql_query("SELECT city,crimes FROM TABLE");
while($row = mysql_fetch_assoc($list)){
  $data[] = "['".$row['city']."', ".$row['crimes']."]";
}
$data_for_chart = implode(",\n"$data);

现在替换您JS- code:

Now replace in your JS-code:

data.addRows([
         ['Cardiff', 300],
         ['London', 900],
         ['Manchester', 500],
         ['Dublin', 400],
         ['Liverpool', 600]
         ]);

使用:

data.addRows([
         <?php echo $data_for_chart; ?>
         ]);

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

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