Google charts with php

0

I'm developing an application where each user will receive different types of data through google charts, however I can not get the data to appear on the screen. Here is the javascript code snippet:

<script type="text/javascript">


        google.charts.load('current', {packages: ['corechart']});
        google.charts.setOnLoadCallback(drawChart);

        function drawChart() {

            // Create the data table.
            var data = new google.visualization.arrayToDataTable([
                [{label: 'Status', type: 'string'},
                 {label: 'Quantidade', type: 'number'}
                ],

                <?php
                    foreach ($_SESSION["data_grafico1"] as $dados){
                        echo "['$dados[0]', $dados[1]]";
                    }
                ?>
                ]);



            // Set chart options
            var options = {'title':'Quantidade de protocolos por Status',
                'width':400,
                'height':300};

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, options);
        }

    </script>

The information contained in the $ data variable is the indexes (string), represented by $ data [0] and the numeric values contained in $ data [1] respectively. Can anyone help me with this? The information in $ data [0] and $ data [1] is retrieved normally, however when adding in js it is not displayed. Thanks for the help in advance.

    
asked by anonymous 23.10.2017 / 23:26

1 answer

0

It is necessary to create a array with the data of foreach but the key 0 must be the title of the axes.

  

$array_list[]="['Status','Quantidade']"; // setting the axis title

Now create foreach and feed array_list with data

<?php
foreach ($_SESSION["data_grafico1"] as $dados){
    $array_list[]= "['$dados[0]', $dados[1]]";
}
?>

Created $array now just insert it into the chart.

<script type="text/javascript">
    google.charts.load('current', {'packages':['corechart']});
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {

    var data = google.visualization.arrayToDataTable([
      <?php echo (implode(",", $array_list)); ?> //inserindo o array_list
    ]);

    var options = {
      title: 'Quantidade de protocolos por Status'
    };

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);
  }

In this way the graph will be created within div with id defined within JS

    
24.10.2017 / 13:48