gpt4 book ai didi

php - 如何使用动态数据和谷歌图表?

转载 作者:可可西里 更新时间:2023-11-01 07:13:48 26 4
gpt4 key购买 nike

例如,我们在 Google Code API 处有此折线图

此图表反射(reflect)了一组已定义的数据,但是我想使用我自己的来自 php/mysql 脚本的数据来创建图表。

这是 google 提供的嵌入 html 页面的代码..

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Google Visualization API Sample
</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['imagelinechart']});
</script>
<script type="text/javascript">
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('number', 'Height');
data.addRows(3);
data.setCell(0, 0, 'Tong Ning mu');
data.setCell(1, 0, 'Huang Ang fa');
data.setCell(2, 0, 'Teng nu');
data.setCell(0, 1, 174);
data.setCell(1, 1, 523);
data.setCell(2, 1, 86);

// Create and draw the visualization.
new google.visualization.ImageLineChart(document.getElementById('visualization')).
draw(data, null);
}


google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 300px; height: 300px;"></div>
</body>
</html>

我想到的选项是将以下代码片段保持在一个循环中并生成数据。有人有简单有效的方法来做到这一点吗?

data.addColumn('string', 'Name');
data.addColumn('number', 'Height');
data.addRows(3);
data.setCell(0, 0, 'Tong Ning mu');
data.setCell(1, 0, 'Huang Ang fa');
data.setCell(2, 0, 'Teng nu');
data.setCell(0, 1, 174);
data.setCell(1, 1, 523);
data.setCell(2, 1, 86);

最佳答案

您的问题涉及到让我非常沮丧的事情:Google 的 API 文档不是很好!特别是对于图表 API,基本上在所有示例中,图表数据都硬编码在页面中,在现实生活中,您基本上总是渲染存储在数据库中并传输到浏览器的数据。

1) 您需要服务器上的一些代码(我使用 PHP)来查询数据库、“打印”并传输 JSON/复杂数据结构,这是一个对象,其中属性是包含具有属性和值的附加对象的数组以 Google 的图表 JavaScript 期望在浏览器中接收它的确切格式。我是这样做的:

$sql = "SELECT year,d_pop FROM metrics WHERE year IN ('1940','1950','1960','1970','1980')";

$sth = mysql_query($sql, $conn) or die(mysql_error());

//start the json data in the format Google Chart js/API expects to recieve it
$JSONdata = "{
\"cols\": [
{\"label\":\"Year\",\"type\":\"string\"},
{\"label\":\"Detroit Population\",\"type\":\"number\"}
],
\"rows\": [";

//loop through the db query result set and put into the chart cell values
while($r = mysql_fetch_assoc($sth)) {
$JSONdata .= "{\"c\":[{\"v\": " . $r['year'] . "}, {\"v\": " . $r['d_pop'] ."}]},";

}

//end the json data/object literal with the correct syntax
$JSONdata .= "]}";

echo $JSONdata;

2) 您需要在页面上的 JavaScript 中接收该 JSON 对象,并将其传递给 Google 的 Chart JS。我引入了 JQuery,然后像这样使用它的 AJAX 方法:

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

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

// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {vAxis: {minValue: 0}});
}

我的代码片段着重于查询 mySQL 数据库、生成 Google Charts API 需要的 JSON 对象以及使用 JQuery 和 AJAX 接收它的关键部分。希望这能有所启发!

关于php - 如何使用动态数据和谷歌图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9971917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com