gpt4 book ai didi

php - 谷歌图表 API 数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:53 24 4
gpt4 key购买 nike

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

目前我有以下数据:

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 数据库中的表中的数据提供给它呢?

最佳答案

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

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

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

与:

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

关于php - 谷歌图表 API 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14979987/

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