我刚开始使用 Highcharts,但已经遇到了问题。我想用来自 Ajax 调用的数据填充我的图表,但它只显示一个空白图表。
首先,我想向您展示我如何获取数据:
<?php
require_once("mysql_config.php");
$query = "SELECT temp FROM daten ORDER BY daten.id DESC LIMIT 0,6";
$answer = mysqli_query($mysqli,$query);
$row;
while($row = $answer->fetch_array()){
echo $row['temp'] . ",";
}
?>
这个输出看起来像:-20,0,0,7,0,0,100
还有我的 AJAX 函数:
function ajax(website, element, callback) {
var http = null;
var http = new XMLHttpRequest();
var url = website;
if(http!=null){
http.open("POST",url,true);
http.onreadystatechange = function(){
if(http.readyState == 4){
var text = http.responseText;
callback(text, element);
}
}//Ende readychange
http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
http.send();
}//Ende if(http2!=null)
}//Ende Funktion
回调是我的 create_chart 函数
function create_chart(rawdata, element){
newdata = rawdata.split(",");
//var newdata = [-20,0,0,7,0,0,100];
var chart = new Highcharts.Chart({
chart: {
type: 'line',
renderTo: 'test'
},
title: {
text: 'Testchart'
},
subtitle:{
text: 'Subtitle'
},
xAxis: {
categories: ['A','B','C','D','E','F','G']
},
yAxis: {
title: {
text: 'This is it'
}
},
series: [{
name: "Test",
data: newdata
}]
});
}
问题是,它只在使用 ajax 时绘制没有数据的图表,但是当我使用“预填充”数组(如我注释掉的那个)时,它工作正常。