Highcharts 动态更新 x 轴类别
Highcharts update x-axis categories dynamically
我正在寻求有关使用定期接收的数据更新 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>
在 index.php 的头部加载 mqtt.js 这再次完美地工作。
我正在努力解决的问题是如何将传入数据从 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 图表上的 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>
在 index.php 的头部加载 mqtt.js 这再次完美地工作。
我正在努力解决的问题是如何将传入数据从 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]
]
});