gpt4 book ai didi

javascript - 无法在 Highcharts 样条图上动态绘制更新的 y 值

转载 作者:行者123 更新时间:2023-11-28 06:37:55 25 4
gpt4 key购买 nike

我正在尝试动态更新 highcharts 样条图上的点。我正在使用 json.AJAX HTTP get 请求(因其异步更新功能)访问 phpMyAdmin 数据库,该数据库保存不断更新的数据字段。

从控制台日志中可以看出,并进行了一些测试,我的 AJAX HTTP 请求已成功从数据库中获取数据并将其返回到我的主机。问题出在 HIGHCHARTS 语法上。我不知道如何正确地将数据“赋予”新点的“y”坐​​标。

见附件代码:

<div id="noise" style="width: 300px, height: 300px">
<script>
$(function () {
$(document).ready(function () {
Highcharts.setOptions({
global: {
useUTC: false
}
});

$('#noise').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
// AJAX post to host, which will send CS50::query into database
load: function () {
var series = this.series[0];
setInterval(function () {
var x = (new Date()).getTime(), // current time
y = jQuery.ajax({
type: "GET",
url: '../host.php',
dataType: 'json',
data: { type: 'noise' },
success: function (noiseValue, textstatus) {
if (!('error' in noiseValue)) {
yourVariable = Number((noiseValue[0])["test"]);
console.log(yourVariable);
}
else {
console.log(noiseValue.error);
}
}
});

series.addPoint([x, y], true, true);
}, 2000);
}
}
},
title: {
text: 'Is Baby Crying?'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Quiet................................................Loud'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Baby Cries',
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 * 2000,
y: Math.random()
});
}
return data;
}())
}]
});
});
});

</script>
</div>

最佳答案

您需要在成功回调中添加点内部,例如:

success: function (noiseValue, textstatus) {
if (!('error' in noiseValue)) {
yourVariable = Number((noiseValue[0])["test"]);
console.log(yourVariable);
series.addPoint([x, yourVariable]);
} else {
console.log(noiseValue.error);
}
}

关于javascript - 无法在 Highcharts 样条图上动态绘制更新的 y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34107074/

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