Answer the question
In order to leave comments, you need to log in
How to return json data in HighCharts?
Good day to all. Help send data in JSON format to JS Highcharts. There are two selectable fields: Temperature and Humidity. According to them, a schedule should be built further. With one field everything worked as soon as two added everything died.
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);
$my = array();
if($query->rowCount() < 1)
{
echo "Не найдено";
}
else
{
foreach ($result as $row)
{
$my[] = $row;
}
}
echo json_encode($my);
/* /Query */
/* /SQL */
$(function(){
var options = {
chart: {
renderTo: 'mychart',
type: 'spline'
},
title: {
text: 'Мониторинг температуры и влажности за день'
},
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: 'Значение'
}
},
series: [{}]
};
$.getJSON('../ajax/get_weather_day.php', function(data){
options.series[0].name = "Температура";
options.series[0].data = data.temperature;
options.series[1].name = "Влажность";
options.series[1].data = data.humidity;
var chart = new Highcharts.Chart(options);
});
});
Answer the question
In order to leave comments, you need to log in
I don't know what will happen if you have options.series[1] which is undefined by default - add some value.
Try to specify 2 empty objects in the series key when creating a chart.
yAxis: {
title: {
text: 'Значение'
}
},
series: [{}, {}]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question