簡體   English   中英

highcharts中的實時數據顯示

[英]Real time data display in highcharts

我想在我的 MYSQL 數據庫中的 highcharts 中顯示實時數據,我嘗試了 setInterval 和 setTimeout 和 chart.redraw() 等方法,但似乎無法使其工作。 我還嘗試過 highcharts 鏈接中的動態更新圖表:在此處enter code here https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/stock/demo/dynamic-update但似乎無法將隨機數據語法轉換為我的數據庫數據。 我的代碼是:

<html>
<head>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"> 
 </script>
<script src="https://code.highcharts.com/stock/modules/drag-panes.js"> 
</script>
<link rel="stylesheet" type="text/css" href="/public/oi chart.css">
</head>
<figure class="highcharts-figure">
<div id="container"></div>
</figure>

<script type="text/javascript">

 $(document).ready(function() {
            Highcharts.stockChart('container', {
                    options = {
                        chart: {
                            renderTo: 'container',
                            defaultSeriesType: 'line'
                        },
  Highcharts.stockChart('container', {
    rangeSelector: {
        enabled: true,
        selected: customRange,
        buttons: [{
            type: 'day',
            count: 1,
            text: '1D'
            }, {
            type: 'week',
            count: 1,
            text: '1W'
             }, {
            type: 'week',
            count: 2,
            text: '2W'
                     }, {
            type: 'month',
            count: 1,
            text: '1M'
            }, {
            type: 'all',
            text: 'All'
        }]
         },
       time: {
        timezoneOffset: timezone
       },
         events: {
                            load: function requestData() {

                                $.ajax({
                                    url: 'temp_chart.js',
                                    success: function(points) {
                                        var series = chart.series,
                                            shift;

                                     $.each(series, function(i, s) {
                                       shift = s.data.length > 1;
                                       s.addPoint(points[i], true, true);
                                        });
                                        setTimeout(requestData, 1000);
                                        chart.redraw();
                                    },
                                    cache: false
                                });

                            }
                        },
        title: {
        text: 'Temperature',

        },
        subtitle: {
        text: 'temperature in maharashtra',
        },
           yAxis: {
        title: {
            text: 'temperature'
        },
        resize: {
            enabled: true
        }
       },
       xAxis: {
        type: 'datetime'
       },

      legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'middle'
       },

      plotOptions: {
        series: {
            marker: {
                enabled: false
            },
            label: {
                connectorAllowed: false
            },

        }
         },

        series: [{
            name: 'Mumbai',
            data: []

           }, {
             name: 'Pune',
             data: []

        }],
        };

             $.getJSON('temp_chart.js', function(json) {
             data1 = [<% data array %>];
             data2 = [<% data array %>];
                        $.each(json, function(key, value) {
                            data1.push([value[0], value[1]]);
                            data2.push([value[0], value[2]]);
                        });

                        options.series[0].data = data1;
                        options.series[1].data = data2;
                        chart = new Highcharts.stockChart(options);
                    });

        responsive: {
        rules: [{
            condition: {
                maxWidth: 500
            },
            chartOptions: {
                legend: {
                    layout: 'horizontal',
                    align: 'center',
                    verticalAlign: 'bottom'
                }
            }
        }]
       }

      });
           </script>

           </html>~ 

請注意,加載回調應該嵌套在chart.events object 配置中。 API: https://api.highcharts.com/highcharts/chart.events.load

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM