Using PHP for data taken from a database in Google GeoChart
I am trying to display data on a GeoChart that is retrieved from the population database, with the code below:
<!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 is used only when adding data to the 'data' variable, and after the php section it should contain the following:
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]
]);
I am having a problem running a file in the browser as nothing is displayed. By using "check item" it shows an unexpected marker error on line:
colorAxis {colors: ['red', 'yellow']}
which is in the options variable.
I thought this is the correct way to set colorAxis for GeoChart?
Any help would be much appreciated as I am still learning how to use PHP and SQL.
+3
source to share
1 answer
You're just missing the colon:
var options = {
region: 'GB',
displayMode: 'markers',
colorAxis : {colors: ['red', 'yellow']}
};
^
otherwise your code works well -> http://jsfiddle.net/tnxhg08r/
+1
source to share