javascript - highcharts 和 echarts动态曲线初始点的问题
阿神
阿神 2017-04-11 10:58:00
[JavaScript讨论组]

如下代码为highcharts 动态曲线的官方实例

$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });
        $('#container').highcharts({
            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 = Math.random();
                            series.addPoint([x, y], true, true);
                        }, 1000);
                    }
                }
            },
            title: {
                text: 'Live random data'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '' + this.series.name + '
' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '
' + Highcharts.numberFormat(this.y, 2); } }, 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: Math.random() }); } return 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: Math.random()
             });
         }
         return data;
}())

但是当实际的情况,如websocket、kafka、rabbitmq推送过来的数据是不可能有初始化的

也就是根本没有那个for循环,每次只进来一个点的数据。

这种情况下第一次启动程序的时候图上是没有一个点的,而不是像实例中一打开就有点充满曲线。

如何才能做到从没有点,到有一个点,并且随着数据到来向后推移点的位置,直到充满曲线达到实例中的初始化曲线?

阿神
阿神

闭关修行中......

全部回复(1)
大家讲道理
var socket_on_num = 0;//全局-监听到数据的次数。每次的数据push到data,socket_on_num++ 

if(socket_on_num === 10){//10次时
$('#container').highcharts({
            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];
                        //监听数据series.addPoint([x, y], true, true);
                    }
                }
            },
            title: {
                text: 'Live random data'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'Value'
                },
                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: 'Random data',
                data: data
            }]
        });
}

就是先模拟for,再动态加点。就是显示的图慢些,可以改成2

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号