HighCharts 隨機數動態曲線展示(動態數據實時展示)

cymt 9年前發布 | 3K 次閱讀 JavaScript

Highcharts 是一個用純JavaScript編寫的一個圖表庫, 能夠很簡單便捷的在web網站或是web應用程序添加有交互性的圖表,本文主要研究在隨機數展示上的應用。

具體代碼如下:

    <html>  
    <head>  
       <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>  
       <script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>  
       <script>  
    $(function () {                                                                       
        $(document).ready(function() {                                                    
            Highcharts.setOptions({                                                       
                global: {                                                                 
                    useUTC: false                                                         
                }                                                                         
            });                                                                           

            var chart;                                                                    
            $('#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: '實時隨機數展示'   //主標題                                           
                },                                                                        
                xAxis: {                     //X軸                                             
                    type: 'datetime',                                                     
                    tickPixelInterval: 150                                                
                },                                                                        
                yAxis: {                     //Y軸                                             
                    title: {                                                              
                        text: '數據'                                                     
                    },                                                                    
                    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: '隨機數',                                                  
                    data: (function() {                                                   
                        // 產生隨機數                               
                        var data = [],                                                    
                            time = (new Date()).getTime(),                                
                            i;                                                            

                        for (i = -24; i <= 0; i++) {  //顯示范圍為25個數據 ,可修改                                
                            data.push({                                                   
                                x: time + i * 1000,    //1000毫秒產生一個數據                                
                                y: Math.random()      //隨機數產生函數                                
                            });                                                           
                        }                                                                 
                        return data;                                                      
                    })()                                                                  
                }]                                                                        
            });                                                                           
        });                                                                               

    });    
      </script>  
    </head>  

    <body>  
       <div id="container" style="min-width:400px;height:400px;"></div>  
    </body>  
    </html>  

 本文由用戶 cymt 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!