Google Chart, PHP and Ajax - Pie chart does not show

Asked

Viewed 371 times

0

I’m not able to plot the Chart pie from google Chart. When I change the type of chart to Barchart or columnchart, it plots, but when I put piechart it does not divide.

File that takes data from BD

<?php

require('../../Controller/conexao_bd/conexao_dw_bi.php');
class ContasPagas{

    public function pesquisaContasPagasForne($sk_empresa_atual){

    try{

            $sk_empresa = $sk_empresa_atual; 
            $pdo = Conexao::getInstance();
            $table = array();

            //colunas 
                $table['cols'] = array(
                    array('label'=>'Fornecedor','type'=>'string'),
                    array('label'=>'QTD','type'=>'number')
                );


            //rows
            $rows = array();                    

            $query = $pdo->prepare("SELECT COUNT(fato.valor_lancamento) as qtd_contas, forne.cnpj_cpf_fornecedor as doc_forne, forne.nome_fornecedor as forne FROM fato_lancamento_contabil as fato INNER JOIN dim_fornecedor as forne ON forne.cnpj_cpf_fornecedor = fato.cnpj_cpf_forne_cliente WHERE dim_empresa_sk_empresa = :sk_empresa AND dim_cliente_sk_cliente IS NULL AND dim_fornecedor_sk_fornecedor IS NOT NULL GROUP BY 2,3");

            $query->bindParam(':sk_empresa',$sk_empresa, PDO::PARAM_INT);
            $query->execute();


                    while ($linha=$query->fetch(PDO::FETCH_ASSOC)){                 

                        $forne = $linha['forne'];
                        $qtd_contas = $linha['qtd_contas'];
                        $temp = array();
                        $temp[] = array('v'=>$forne);
                        $temp[] = array('v'=>$qtd_contas);
                        $rows[] = array('c'=>$temp);
                }

                $table['rows'] = $rows;
                $jsonTable = json_encode($table);                   

                echo $jsonTable;


        }catch(PDOException $error){

            print "Erro: ".$error->getMessage();
        }
    }
}
?>

Script JS

<script type="text/javascript">

    google.load('visualization','1',{'packages' : ['corechart', 'table'],'language':'pt-BR'}); // pacotes de gráficos e tabela
    google.setOnLoadCallback(desenharGraficoPagamentosForne_pie);   

    function desenharGraficoPagamentosForne_pie(){

        // ajax
        var jsonDados5 = $.ajax({
            url:"http://localhost/sistema-bi-web/Model/Empresa/get_qt_contas_pagas_fornce.php",
            dataType: "json",
            async: false
        }).responseText;

        var options5 = {
            title: 'x'
        };

        //dados do grafico
        var dados5 = new google.visualization.DataTable(jsonDados5);

        var grafico5 = new google.visualization.PieChart(document.getElementById('grafico_pagamentos_forne_pie'));

        // desenha o grafico de acordo com os parametros passados
        grafico5.draw(dados5, options5); 
    }

</script>

1 answer

0

I managed to fix it. I had to parse int in the qtd_accounts variable, which was coming as String by json.

$qtd_accounts = intval($line['qtd_accounts']);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.