Highcharts动态更新X轴类别

n3schb8v  于 2022-11-11  发布在  Highcharts
关注(0)|答案(2)|浏览(317)

我正在寻找有关使用定期接收的数据更新Highcharts图表上的X轴类别的帮助。
图表在一个名为forecastgraph.html的文件中定义,通过<?php require("widget/forecastgraph.html"); ?>将其加载到index.php(我希望显示它的网页)中,图表按预期呈现。
通过js脚本(称为mqtt.js)处理的实时数据以json格式接收传入的mqtt数据,并使用jquery以如下方式更新index.php的各个部分:我使用<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格式数据的函数以如下方式处理传入数据(当加载index.php时,mqtt.js启动):

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元素效果很好,而且很稳定。(在脚本开始时声明为全局)。在上面的示例中,需要将forecast_period_1用作四个x轴类别中的第一个类别,并使用forecast_period_1_high和forecast_period_1_low来更新图表数据中相应的hi和lo值。

tvmytwxo

tvmytwxo1#

这是您想要实现的输出吗?在下面的演示中,我编写了一个函数,该函数获取一个高温度值和一个低温度值,并在按钮上触发next。通过使用series.update功能,新的数据将附加到图表中。
演示:https://jsfiddle.net/BlackLabel/he768cz3/
API:https://api.highcharts.com/class-reference/Highcharts.Series#update

dojqjjoe

dojqjjoe2#

我已经找到了一个解决方案。首先,你必须把图表存储在一个变量中,然后你才能更新图表数据。如下所示

var chart = $('#forecastgraph').highcharts({ ...option })

更新xAxis或序列数据

// 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]
                      ]
               });

相关问题