score:0

Accepted answer

problem solved.

this is the code i added:

  events: {
    load: function requestdata() {

        $.ajax({
            url: 'live.php',
            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
        });

    }
    }

now i have historical data from my db and it adds new data point every time there's a new entry in de db without the need to refresh the page.

here is is my live.php code:

<?php
header("content-type: text/json");

    $dbhost = 'localhost';
    $dbname = 'highchart';  
    $dbuser = '*******';                  
    $dbpass = '*******'; 


    try{

        $dbcon = new pdo("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
        $dbcon->setattribute(pdo::attr_errmode, pdo::errmode_exception);

    }catch(pdoexception $ex){

        die($ex->getmessage());
    }

    $stmt=$dbcon->prepare("select * from trafico");
    $stmt->execute();
    $json = [];
    while($row=$stmt->fetch(pdo::fetch_assoc)){
        extract($row);
            }
        $json[]= [strtotime($time_1m)*1000, (int)$tx];
        $json[]= [strtotime($time_1m)*1000, (int)$rx];

    echo json_encode($json);
?>

this is the output from live.php:

[[1522869181000,872],[1522869181000,54]]

and this is how the code looks now

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">

  var chart;
 $(document).ready(function() {

        highcharts.setoptions({
            time: {
                timezoneoffset: 3 * 60
            }
        });

     var options = {
        chart: {
          renderto: 'container',
          type: 'area',
          zoomtype: 'x',
          events: {
            load: function requestdata() {

                $.ajax({
                    url: 'live.php',
                    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: {
        },
        xaxis: {
           type: 'datetime'
        },
        yaxis: {
        },
        series: [{
           name: 'download',
           data: []
       }, {
           name: 'upload',
           data: []
        }]
     }; 
     $.getjson('data.php', function(json) {
        data1 = [];
        data2 = [];
        $.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);
     });
  });
</script>

<body>
 <div id="container" style="min-width: 400px; height: 800px; margin: 0 auto"></div>

</body>

<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>

Related Query

More Query from same tag