我正在尝试使我的实时更新图表生效。图表本身来自HighCharts,并且使用MySQL数据库值插入值。图表工作正常,但我似乎无法使用实时更新部分。
我已经使用了Highcharts实时更新脚本,但是我认为SQL部分是问题所在,希望有人可以向我发送正确的消息。
要明确:用户可以填写表格,然后将答案插入数据库。而且我需要实时更新图表,以便不必刷新页面。
PHP部分
foreach ($grafiektitel as $competentie) {
$sql2 = "SELECT $competentie FROM titel";
$result = $link->query($sql2);
$waardes = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$waardes[] = $row[$competentie];
}
} else {
echo "0 results";
}
?>
<?php
$vals = array_count_values($waardes);
echo '<div id="' . $competentie . '" class="desc">';
echo '
<div class="grafiek" id="container-' . $competentie . '" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<table id="datatable-' . $competentie . '">
<thead>
<tr>
<th></th>
<th>Slecht</th>
<th>Matig</th>
<th>Voldoende</th>
<th>Goed</th>
<th>Zeer Goed</th>
</tr>
</thead>
<tbody>
<tr>
<th>' . $competentie . '</th>
<td>' . $vals[1] . '</td>
<td>' . $vals[2] . '</td>
<td>' . $vals[3] . '</td>
<td>' . $vals[4] . '</td>
<td>' . $vals[5] . '</td>
</tr>
</tbody>
</table>
<hr>
';
echo '</div>';
我的Highcharts脚本:
$(document).ready(function(){
$('.grafiek').each(function(index){
Highcharts.chart($(this).attr('id'), {
data: {
table: 'datatable-'+$(this).attr('id').replace('container-','')
},
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
title: {
text: 'Competentieanalyse'
},
yAxis: {
allowDecimals: false,
title: {
text: 'Units'
}
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
this.point.y + ' ' + this.point.name.toLowerCase();
}
}
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i += 1) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
}())
}]
});
});
});