json to graph

0

I am realizing a graph in real time using the highcharts library only that I need that in the axis of and it appears the obtaining of data of a query to a database so that it is visualized in this graph I show them the code that I have

<?php
error_reporting(0);
header('Content-Type: application/json');
$pdo=new PDO("mysql:dbname=banorte;host=127.0.0.1","root","adminroot");
switch($_GET['Consultar']){
        // Buscar Último Dato
        case 1:
            $statement=$pdo->prepare("SELECT valory as y FROM llamadas ORDER BY id ASC LIMIT 0,1");
            $statement->execute();
            $results=$statement->fetchAll(PDO::FETCH_ASSOC);
            $json=json_encode($results);
            echo $json;
        break; 
        // Buscar Todos los datos
        default:

            $statement=$pdo->prepare("SELECT valory as y FROM llamadas ORDER BY id ASC");
            $statement->execute();
            $results=$statement->fetchAll(PDO::FETCH_ASSOC);
            $json=json_encode($results);
            echo $json;
        break;

}
?>

And this is the javascript to use the library

<!DOCTYPE html>
<html lang="es">
<head>
   <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
   <script src="https://code.highcharts.com/highcharts.js"></script>
   <script src="https://code.highcharts.com/modules/exporting.js"></script>
   <script src="https://code.highcharts.com/modules/export-data.js"></script>
    <meta charset="UTF-8">
    <title>prueba</title>
    <script type="text/javascript">
    var chart;
    $(document).ready(function() {


Highcharts.setOptions({
    global: {
        useUTC: false
    }
});

Highcharts.chart('container', {
    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 '<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: (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;
        }())
    }]
});     
</script>
</head>
<body>
    <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</body>
</html>
    
asked by Lestat Deutchlan 07.05.2018 в 20:32
source

1 answer

0

You can accommodate the JSON so that you can easily check the data.

Create a foreach where you separate the data and concatenations in this way:

{"x":{1,2,3,4,5},"y":{5,6,7,8,9}}
    
answered by 07.05.2018 в 20:46