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

标签 javascript jquery ajax highcharts

我正在尝试动态更新 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/

相关文章:

javascript - 使用dojo/javascript触发点击事件?

javascript - 如果另一个元素不存在,则 Angular 创建元素

javascript - 如何在 React Native 中的 JS 中验证全名

javascript - JQuery 元素加载/就绪事件

jquery - 将值重复输出到表中

javascript - 由于内容类型不正确,AJAX 调用返回 404 错误

javascript - 使用 moment JS 制作简单时间线的问题

javascript - 如果选择在标签内,如何获取格式化文本

javascript - 使用 JS 验证 HTML 表单中的多个字段

javascript - 如何获取我正在单击的按钮的 ID