在 Google GeoChart 中使用 PHP 处理从数据库中获取的数据
Using PHP to with data taken from a database, in Google GeoChart
我正在尝试在 GeoChart 上显示数据,这些数据是从 'population' 数据库中提取的,代码如下所示:
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawMarkersMap);
function drawMarkersMap() {
var data = google.visualization.arrayToDataTable([
<?php
echo '["City","Population"],';
require_once('connect_population.php');
try{
$query = "SELECT City, People FROM population";
$statement = $db->query($query);
while($row = $statement->fetch(PDO::FETCH_ASSOC)){
echo '["'.$row["City"].'",'.$row["People"].'],';
}
}catch(PDOException $e){
$message = '<p>Something went wrong!</p><p>' . $e->getMessage() . '</p>';
}
?>
]);
var options = {
region: 'GB',
displayMode: 'markers',
colorAxis {colors: ['red', 'yellow']}
};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
PHP 仅在向 'data' 变量添加数据时使用,在 php 部分之后,应包含:
var data = google.visualization.arrayToDataTable([
["City","Population"],["London",7172091],["Bristol",420556],["Liverpool",469017],["Leeds",443247],["Birmingham",970892],["Manchester",394269],["Newcastle",655875],["Leicester",330574],["Belfast",276459],["Glasgow",629501],["Sheffield",439866],["Edinburgh",430082]
]);
我在浏览器中 运行 文件时遇到问题,因为没有显示任何内容。使用 'inspect element',它在行中显示意外的标记错误:
colorAxis {colors: ['red', 'yellow']}
在选项变量中。
我认为这是为 GeoChart 设置 colorAxis 的正确方法?
任何帮助将不胜感激,因为我仍在学习如何使用 PHP 和 SQL。
你只是缺少一个冒号:
var options = {
region: 'GB',
displayMode: 'markers',
colorAxis : {colors: ['red', 'yellow']}
};
^
否则你的代码运行良好 -> http://jsfiddle.net/tnxhg08r/
我正在尝试在 GeoChart 上显示数据,这些数据是从 'population' 数据库中提取的,代码如下所示:
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawMarkersMap);
function drawMarkersMap() {
var data = google.visualization.arrayToDataTable([
<?php
echo '["City","Population"],';
require_once('connect_population.php');
try{
$query = "SELECT City, People FROM population";
$statement = $db->query($query);
while($row = $statement->fetch(PDO::FETCH_ASSOC)){
echo '["'.$row["City"].'",'.$row["People"].'],';
}
}catch(PDOException $e){
$message = '<p>Something went wrong!</p><p>' . $e->getMessage() . '</p>';
}
?>
]);
var options = {
region: 'GB',
displayMode: 'markers',
colorAxis {colors: ['red', 'yellow']}
};
var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
PHP 仅在向 'data' 变量添加数据时使用,在 php 部分之后,应包含:
var data = google.visualization.arrayToDataTable([
["City","Population"],["London",7172091],["Bristol",420556],["Liverpool",469017],["Leeds",443247],["Birmingham",970892],["Manchester",394269],["Newcastle",655875],["Leicester",330574],["Belfast",276459],["Glasgow",629501],["Sheffield",439866],["Edinburgh",430082]
]);
我在浏览器中 运行 文件时遇到问题,因为没有显示任何内容。使用 'inspect element',它在行中显示意外的标记错误:
colorAxis {colors: ['red', 'yellow']}
在选项变量中。
我认为这是为 GeoChart 设置 colorAxis 的正确方法?
任何帮助将不胜感激,因为我仍在学习如何使用 PHP 和 SQL。
你只是缺少一个冒号:
var options = {
region: 'GB',
displayMode: 'markers',
colorAxis : {colors: ['red', 'yellow']}
};
^
否则你的代码运行良好 -> http://jsfiddle.net/tnxhg08r/