I'll start by saying I'm completely new to Javascript. I have worked with Java and PHP more than anything.
I copied much of the code from the example and have tried tinkering with it to get it to run but have not had any luck. The original example in JSFiddle is here: http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/stock/demo/compare/
Any input is appreciated.
<?php
echo <<< 'EOT'
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://code.highcharts.com/highstock.js"></script>
<script src="https://code.highcharts.com/exporting.js"></script>
</head>
<body>
EOT;
require_once('hvst.php'); // Generates the JSON file using json_encode();
echo <<< 'EOT'
function createChart() {
var fields = ['humidity', 'curtemp'];
Highcharts.stockChart('container', {
rangeSelector: {
selected: 4
},
yAxis: {
labels: {
formatter: function () {
return (this.value > 0 ? ' + ' : '') + this.value + '%';
}
},
plotLines: [{
value: 0,
width: 2,
color: 'silver'
}]
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2,
split: true
},
series: seriesOptions
});
$.each(fields, function (timestamp, curtemp) {
$.getJSON('chart1.json', function (humidity) {
seriesOptions[timestamp] = {
name: curtemp,
data: humidity
},
seriesCounter += 1;
if (seriesCounter === fields.length) {
createChart();
}
});
});
};
</script>
<div id="container" style="height: 400px; min-width: 400px"></div>
</body>
</html>
EOT;
?>
Edit: hvst.php
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
//Set JSON header
header("Content-type: text/json");
include 'dbread.php';
//include 'correlate.php';
// Get last 5 days
$days=5;
$resultSet1 = readDB('SELECT timestamp,humidity,curtemp FROM public."WeatherData" WHERE timestamp > NOW()-INTERVAL \''.$days.' days\';');
$fp = fopen('chart1.json', 'w');
fwrite($fp, json_encode($resultSet1,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
fclose($fp);
?>
Here's a sample of the first few lines from the JSON output:
[
{
"timestamp": "2016-11-05 10:10:02.427425",
"humidity": 48,
"curtemp": 46
},
{
"timestamp": "2016-11-05 10:10:03.83401",
"humidity": 48,
"curtemp": 46
},