我有一个从CanvasJS复制的PHP图,我一直在设法从我的数据库(不断更新的)中获取一些值到图中。我已经成功地导入了数据库中已经存在的值,但是我似乎无法找到将新值导入函数updateChart()的方法。
很长一段时间以来,我一直试图找到一个解决方案,但没有找到任何可行的方法,我试图研究一种使用$.GET的方法,但它似乎没有起作用。
function updateChart() {
var deltaY1, deltaY2;
xValue += updateInterval;
// adding random value
yValue1 += Math.round(2 + Math.random() *(-2-2));
yValue2 += Math.round(2 + Math.random() *(-2-2));
// pushing the new values
dataPoints1.push({
x: xValue,
y: yValue1
});
dataPoints2.push({
x: xValue,
y: yValue2
});
// updating legend text with updated with y Value
chart.options.data[0].legendText = "Building A " + yValue1 + " watts";
chart.options.data[1].legendText = " Building B " + yValue2+ " watts";
chart.render();}
我不知道如何继续这样的事情,因为大多数教程在线只谈论非常基本的东西。
我很乐意事先接受任何帮助和感谢。
发布于 2020-08-09 18:20:02
我希望这是你要的。
您可以使用Ajax来发出php请求,并作为javascript变量获取结果。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$.ajax(function(){
type: 'POST',
url: 'url_of_file.php',
data: { 'var': var_for_query }, // type and data are not necessary if you don't need to send js variables through php
success: function(data){
function updateChart() {
// ...
}
}
});
</script>
'data'只是您的php脚本的结果,所以基本上只需打印结果,如下
$array = array($row['something'],etc...);
echo $array;
免责声明:为了更新内容,每次更改行时都必须发出请求,只要将代码放入您想要更新页面时调用的函数中即可。
https://stackoverflow.com/questions/63272450
复制相似问题