gpt4 book ai didi

php - 实时更新 2 个系列的 Flot 图

转载 作者:行者123 更新时间:2023-11-29 03:43:49 25 4
gpt4 key购买 nike

我正在使用 jquery 和 flot 显示 2 个时间序列的值。我使用它们的时间很短,现在我坚持使用这些示例。基本上,我改编了几个示例,使用我包含在 JQuery 脚本中的 PHP 脚本从 MySQL 数据库中提取系列。到目前为止一切正常。我希望能够每隔几秒刷新一次这个系列。此刷新似乎不起作用,我不确定为什么。下面是用于生成图形的 jquery 代码。我现在放了脚本的一部分。

    $(function(){ 

//add data source to flot. 2 datasets with same structure: data in UNIX_TIMESTAMP format, value in DECIMAL format

<?php include 'datasource.php'; ?>;

//declare datasets

var datasets = {
"temperature": {
label: "Temperature (C)",
data: <?php echo json_encode($dataset1); ?>
},
"humidity": {
label: "Humidity (%)",
data: <?php echo json_encode($dataset2); ?>
}
};

//set fixed colors for each series
var i = 0;
$.each(datasets, function(key, val) {
val.color = i;
++i;
});

// insert checkboxes
var choiceContainer = $("#choices");
$.each(datasets, function(key, val) {
choiceContainer.append('&nbsp;&nbsp;<input type="checkbox" name="' + key +
'" checked="checked" id="id' + key + '">' +
'<label for="id' + key + '">'
+ val.label + '</label>');
});
choiceContainer.find("input").click(plotAccordingToChoices);
//define plot options
var options = {series: { shadowSize: 0 },
yaxis: { min: <?php echo json_encode($mintemp) ?>, max: <?php echo json_encode($maxtemp) ?> },
xaxis: { show: true, mode: "time", timeformat: "%h:%M %d.%m.%y", labelWidth: "10"}};
//draw plot
function plotAccordingToChoices() {
var data = [];
choiceContainer.find("input:checked").each(function () {
var key = $(this).attr("name");
if (key && datasets[key])
data.push(datasets[key]);
});
if (data.length > 0)
$.plot($("#placeholder"), data, options);
}
plotAccordingToChoices();
//define plot refresh timeout
setInterval(plotAccordingToChoices(), 3000);})

最佳答案

setInterval 的第一个参数应该是一个字符串:

setInterval('plotAccordingToChoices()', 3000);

或者只有函数名(不调用它):

setInterval(plotAccordingToChoices, 3000);

参见:https://developer.mozilla.org/en/window.setInterval


要从服务器端(PHP)获取更新的数据,您还需要进行远程调用(AJAX)。您可以使用 jQuery getScript function .

像这样:

function updatePlot() {
$.getScript('update_plot.php');
}
setInterval(updatePlot, 3000);

然后,在您的 update_plot.php 文件中,您可以返回混合了 PHP 的 JavaScript 代码(就像您已经做的那样):

<?php // here some PHP code to get your data ?>
// and here some javascript code to use the data
plotAccordingToChoices(); // and finally update the plot

关于php - 实时更新 2 个系列的 Flot 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9568307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com