我正在寻求帮助,以通过定期接收的数据更新 Highcharts 图表上的 x 轴类别。
图表在名为 Forecastgraph.html 的文件中定义。它通过
<?php require("widget/forecastgraph.html"); ?>
加载到index.php,即我想要显示它的网页。图表按预期呈现。
通过js脚本(称为mqtt.js)处理实时数据,该脚本接收json格式的传入mqtt数据,并使用jquery以这种方式更新index.php的各个部分:
$("#elementid").html(a.b.c);
。我使用 <script src="./js/mqtt.js"></script>
将 mqtt.js 加载到 index.php 的头部,这又完美地工作了。
我正在努力解决的是如何将传入数据从 mqtt.js 传递到图表,以便在新数据进入时更新它。具体来说,我正在尝试更新 xAxis 类别和相应的值对。 mqtt.js 会定期收到新的天气预报,因此需要使用该预报适用的新时间段来更新 xAxis 类别,并且需要更新数据以反映各个预测期间的新高温和低温。
图表的代码发布在下面。任何帮助将不胜感激。
猴面包树
<script type="text/javascript">
$(function () {
$('#forecastgraph').highcharts({
chart: {
type: 'columnrange',
backgroundColor: 'rgba(0,0,0,0)',
borderWidth: 0,
margin: [12, 6, 36, 20]
},
title: {
text: null,
},
exporting: {
enabled: false
},
credits: {
enabled: false
},
xAxis: {
categories: [1,2,3,4],
labels: {
y: 30,
style: {
color: 'white',
fontSize: '10px',
fontWeight: 'bold'
}
}
},
yAxis: {
title: {
enabled: false,
x: -14,
},
labels: {
align: 'left'
},
maxPadding: 0.5,
plotLines: [{
value: 10, //normmax
width: 2,
color: '#FF0000'
},{
value: 2, //normmin
width: 2,
color: '#009ACD'
}]
},
tooltip: {
enabled: false
},
plotOptions: {
columnrange: {
dataLabels: {
enabled: true,
style: {
textOutline: 'none'
},
crop: false,
overflow: 'none',
formatter: function () {
var color = this.y === this.point.high ? '#33C4FF' : 'red';
return '<span style="font-size: 12px; font-family:helvetica; font-weight:normal; text-shadow: none; color:' + color + '">' + this.y + '°</span>';
}
}
}
},
legend: {
enabled: false
},
series: [{
name: 'Temperatures',
data: [
[20, -3],
[5, -2],
[6, -2],
[8, -15]
],
color: '#b9deea',
borderColor: '#92cbde',
borderRadius: 4
}]
});
});
</script>
编辑:附加信息。
传入的json数据如下所示:
[{
"period": "Monday",
"condition": "Cloudy",
"high_temperature": "7",
"low_temperature": "-2"
"icon_code": "10",
"precip_probability": "20"
}, {
"period": "Tuesday",
"condition": "A mix of sun and cloud",
"high_temperature": "6",
"low_temperature": "-2"
"icon_code": "02",
"precip_probability": "20"
}, {
"period": "Wednesday",
"condition": "A mix of sun and cloud",
"high_temperature": "3",
"low_temperature": "-5"
"icon_code": "02",
"precip_probability": "20"
}, {
"period": "Thursday",
"condition": "A mix of sun and cloud",
"high_temperature": "1",
"low_temperature": "-10"
"icon_code": "02",
"precip_probability": "20"
}]
加载到index.php的mqtt.js脚本中负责传入json格式数据的函数以这种方式处理传入的数据(mqtt.js在加载index.php时启动):
function onMessageArrived(message) {
console.log("onMessageArrived: " + message.payloadString);
//Env Canada forecast
if (message.destinationName == "myHome/ec/json_data_ec") {
var data = JSON.parse(message.payloadString);
$("#forecast_period_1").html(data[0].period); // update div forecast_period_1 in index.php for debugging purposes and show that data is coming in
forecast_period_1 = (data[0].period); // assign to global var
forecast_period_1_high = (data[0].high_temperature); // global var
forecast_period_1_low = (data[0].low_temperature); // global var
使用传入数据更新整个index.php中的各种html元素效果很好并且很稳定。我尝试做的事情是使用 mqtt.js 脚本放置在全局变量(在脚本开头声明为全局变量)中的数据来更新图表,但没有成功。在上面的示例中,需要将forecast_period_1用作四个xAxis类别中的第一个以及forecast_period_1_high和forecast_period_1_low,以更新图表数据中各自的hi和lo值。
这是您想要实现的输出吗?在下面的演示中,我编写了一个函数,该函数采用高温和低温值,然后在按钮上触发。新数据通过使用
series.update
功能附加到图表。
演示:https://jsfiddle.net/BlackLabel/he768cz3/
API:https://api.highcharts.com/class-reference/Highcharts.Series#update
我已经找到解决方案了。首先,您必须将图表存储在变量中,然后才能更新图表数据。就像下面这样
var chart = $('#forecastgraph').highcharts({ ...option })
更新 x 轴或系列数据
// Update xAxis data label
chart.update({
xAxis: {
categories: [1,2,3,4]
}
});
// Update series data
chart.series[0].update({
data: [
[20, -3],
[5, -2],
[6, -2],
[8, -15]
]
});
在 Highcharts 11+ 中,您可以简单地执行以下操作:
this.chart.update(newOptions)
this.chart.redraw()
并且它将通过动态更新重绘图形(无图形刷新)