【问题标题】:Highcharts Not displaying function's data valuesHighcharts 不显示函数的数据值
【发布时间】:2019-09-03 07:11:02
【问题描述】:

我使用以下 Highchart 的演示创建了一个 Highchart:

https://www.highcharts.com/demo/dynamic-update

现在我做了什么我创建了自己的函数来向图表添加动态值。

我创建了一个函数来从特定的 php 文件中获取动态数据,该文件的数据在每次页面加载事件时都会发生变化。

我在getData 函数console.log 中获取数据值

这是我正在使用的脚本。

<script type="text/javascript">
  $(document).ready(function(){     
     function getData(){
            $.ajax({
                type: 'GET',
                url: 'data.php',
                success: function(data){
                //  var number = document.write(data) ;
                 console.log(data);
                    return data ;

                }
            }) ;
        }
Highcharts.chart('chart', {
    chart: {
        type: 'spline',
        animation: Highcharts.svg, // don't animate in old IE
        marginRight: 10,
        events: {
            load: function () {

                // set up the updating of the chart each second
                var series = this.series[0];
                setInterval(function () {
                    var x = (new Date()).getTime(), // current time
                        y = getData();
                          console.log(y);
                    series.addPoint([x, y], true, true);
                }, 1000);
            }
        }
    },

    time: {
        useUTC: false
    },

    title: {
        text: 'Live random data'
    },
    xAxis: {
        type: 'datetime',
        tickPixelInterval: 150
    },
    yAxis: {
        title: {
            text: 'Value'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    tooltip: {
        headerFormat: '<b>{series.name}</b><br/>',
        pointFormat: '{point.x:%Y-%m-%d %H:%M:%S}<br/>{point.y:.2f}'
    },
    legend: {
        enabled: false
    },
    exporting: {
        enabled: false
    },
    series: [{
        name: 'Random data',
        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 * 1000,
                    y: getData()
                });
            }
            return data;
        }())
    }]
});

});
</script>

现在您可以看到,我创建了一个 getData 函数并获取数据值作为回报。

在 getData 函数下的控制台日志中,我每隔一秒就会得到一个整数值作为回报。

问题是在 Highchart 的函数下,我无法使用 getData 函数获取数据值,它在控制台中返回 undefined。

Highchart 正在运行,但未显示任何数据点。它正在移动,但没有显示任何数据点。

请在我做错的地方纠正我。

感谢任何帮助。谢谢

【问题讨论】:

    标签: javascript function highcharts


    【解决方案1】:

    ajax 调用是异步运行的,因此您无法真正从中返回数据。

    您应该在 ajax 成功函数中渲染图表。

    一个很好的例子已经在这里了。

    https://www.highcharts.com/docs/working-with-data/live-data

    基本上 1.加载点调用函数getData 2.在Getdata中调用ajax函数。 3. ajax 新数据渲染图表成功。

    document.addEventListener('DOMContentLoaded', function() {
        chart = Highcharts.chart('container', {
            chart: {
                type: 'spline',
                events: {
                    load: requestData
                }
            },
            title: {
                text: 'Live random data'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150,
                maxZoom: 20 * 1000
            },
            yAxis: {
                minPadding: 0.2,
                maxPadding: 0.2,
                title: {
                    text: 'Value',
                    margin: 80
                }
            },
            series: [{
                name: 'Random data',
                data: []
            }]
        });        
    });
    
    /**
     * Request data from the server, add it to the graph and set a timeout 
     * to request again
     */
    function requestData() {
        $.ajax({
            url: 'live-server-data.php',
            success: function(point) {
                var series = chart.series[0],
                    shift = series.data.length > 20; // shift if the series is 
                                                     // longer than 20
    
                // add the point
                chart.series[0].addPoint(point, true, shift);
    
                // call it again after one second - add this if you want to auto refresh
                // setTimeout(requestData, 1000);    
            },
            cache: false
        });
    

    }

    【讨论】:

    • 不会每次都加载图表吗?我只想追加数据
    • 只有在 highchart 加载时才会触发,所以技术上只加载一次。
    • 好的。让我检查一下。如果可行,那就太棒了。
    • 该示例在我的本地主机上运行良好。但我无法根据我在 requestData 函数中的需要创建图表。我的数据只包含一个值。喜欢[4]。示例数据具有这种格式的值[1567503090000,3]
    • 我可以在图表上显示我的数据。但现在我在 1 秒内显示 1 个数据点。有什么方法可以让我在秒内显示至少 10 分。我尝试传递 10 个点的数组,例如 [1,6,4,6,2,4,8,5,3]。但图表显示了一些奇怪的过渡。
    猜你喜欢
    • 2020-01-03
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多