Hello everyone,
I am trying to connect to Postgre to retrieve content of the database following which I am converting the data into JSON format. I have tried to convert it into a data table format to visualize it in terms of Line chart. Unfortunately, I am getting a blank page upon loading googel_chart.html.
Here is my HTML file:
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript">
// Load the Visualization API and the LineChart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
dataType: "json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var tableData = JSON.parse(jsonData);
console.log (tableData);
var data = new google.visualization.arrayToDataTable(tableData);
data.addColumn('string', 'Humidity');
data.addColumn('number','jsonData');
data.addRows(tableData);
var options = {
title: 'Humidity and Temperature values',
curveType: 'function',
legend: { position: 'bottom' }
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
My getData.php file is as shown below:
<?php
//setting header to json
header('Content-Type: application/json');
$conn_string = "host=192.168.1.4 port=5432 dbname=vivekdb user=vivek password=postgres";
$dbconn = pg_connect($conn_string);
//print ($dbconn)
if(!$dbconn){
die("Connection failed: " . $dbconn);
}
//query to get data from the table
$query = "SELECT humidity FROM iot.dht22_humidity_data";
//execute query
$result = pg_query($dbconn,$query);
//$json = file_get_contents($row);
//$array = json_decode($json, TRUE);
//print_r($array);
//To check if there is data
if (!$result) {
echo "An error occurred.\n";
exit;
}
//loop through the returned data
//while ($row = pg_fetch_row($result)) {
//$myArr = array("$row[0]");
// $myJSON = json_encode($myArr);
//echo "Humidity :", $myJSON;
//echo "\n";
//}
//$myarray = array()
while ($row = pg_fetch_row($result)) {
$myarray[] = $row;
}
$mydata= json_encode($myarray, JSON_UNESCAPED_UNICODE |JSON_PRETTY_PRINT);
echo "Humidity:", $mydata;
//now print the data
//close connection
pg_close($dbconn);
?>
I am able to get this as output upon loading getData.php
Humidity:[
[
"78.5"
],
[
"99.4"
],
[
"99.9"
],
[
"98.7"
],
[
"88.3"
],
[
"87.4"
],
[
"91.9"
],
[
"90.7"
]
]
I am getting a blank page upon loading the google_chart page. Any assistance in fixing the issue would be of great help.