0

Estou montando um gráfico highcharts dinâmico, porém estou com um problema, preciso pegar um valor de uma página PHP e colocar no ponto y do gráfico, para isso uso uma função javascript denominada a priori de "teste" segue o código do gráfico:

 $(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;
                }())
            }]
    });
});

a função "teste" tem que colocar um valor no gráfico, (por exemplo: 2), segue o código da função teste, onde a mesma pega um valor de um SELECT no banco de dados, é valido ressaltar que está funcionando (visto pelo console.log), porem quando uso o return json ele não retorna:

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

como ainda estou em processo de melhora da minha programação em javascript me deparei com esse problema que aparentemente é simples, mas está me dando dor de cabeça.

1 Resposta 1

1

Bom resolvi meu problema utilizando um variavel global que colocando o retorno do ajax.

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
});}

e no gráfico:

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);

fica a solução se alguém tiver algum problema parecido.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .