1
美好的一天。幫助在JS Highcharts中以JSON格式發送數據。有兩個字段被選中:溫度和溼度。對他們應該基於時間表。有一個領域的一切工作,因爲只有兩個全部死亡。如何在HighCharts中返回json數據?
PHP:
<?php
/* SQL */
/* Connect */
try
{
$connection = new PDO("mysql:host=localhost;dbname=my","root","");
}
catch (PDOException $e)
{
echo 'Connection error: ' . $e->getMessage();
}
/* /Connect */
/* Query */
$query = $connection->prepare("SELECT temperature, humidity FROM weather WHERE date >= CURDATE()");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result, JSON_NUMERIC_CHECK);
/* /Query */
/* /SQL */
?>
JS:
$(function(){
var options = {
chart: {
renderTo: 'mychart',
type: 'spline'
},
title: {
text: 'Temperature'
},
xAxis: {
categories: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
},
yAxis: {
title: {
text: 'Values'
}
},
series: [{}]
};
$.getJSON('../ajax/get_weather_day.php', function(data){
options.series[0].name = "Temperature";
options.series[0].data = data;
options.series[1].name = "Humidity";
options.series[1].data = data;
var chart = new Highcharts.Chart(options);
});
});