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

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

问题描述

如何使用我自己的服务器端数据(即在 PHPMySQL 中)填充 Google Chart API.

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?

推荐答案

在 Charts API 的 JavaScript 代码之前,您必须从数据库中获取数据:

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 代码:

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天全站免登陆