return from a php page for highcharts graphic assembly

0

I am putting a highcharts dynamic chart, however I am having a problem, I need to get a value from a PHP page and put in the point y of the graph, for that use a javascript function called a priori of "test" follows the code of the 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 = teste();//FUNÇÃO PARA PEGAR VALOR DA PAGINA PHP
                        series.addPoint([x, y], true, true);
                    }, 1000);
                }
            }
        },
        title: {
            text: 'Ligacoes Online'
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150
        },
        yAxis: {
            title: {
                text: 'Valor'
            },
            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 da ligacao',
                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;
                }())
            }]
    });
});

The function " test " has to put a value in the graph, (for example: 2), follows the code of the test function, where it takes a value from a SELECT in the database , it is worth mentioning that it is working ( seen by console.log ), but when I use return json it does not return:

function teste() {
    $.ajax({
        type: 'POST',
        url: 'file_get_value.php',
        cache: false,
        dataType: 'json',
        success: function (json) {
            console.log(json);
             return json;
        }
    });
}

As I am still in the process of improving my programming in javascript I came across this problem that apparently is simple, but it is giving me a headache.

    
asked by anonymous 05.09.2017 / 15:05

1 answer

1

Well I solved my problem using a global variable that putting the ajax return.

var chart_live_calls = 0;
function requestData() {
var passar = 'qtdChamadas=' + 1;
$.ajax({
    type: 'POST',
    dataType: 'json',
    url: 'enviandoDashboardAdmin.php',
    data: passar,
    success: function (point) {
        console.log(point);
        chart_live_calls = point;
    },
    cache: false
});}

and in the chart:

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 = chart_live_calls;
                    series.addPoint([x, y], true, true);
                }, 1000);

The solution is if someone has a similar problem.

    
05.09.2017 / 16:37