Generating graphics with hightchart, PHP, MYSQL and Angular JS

Asked

Viewed 1,332 times

1

I’m trying to generate graphs dynamically with technologies (PHP, MYSQL, Angularjs).

This is my PHP code:

$sql = "SELECT descricao, estoque from `alimentos`";

Transaction::open("mysql");
$conn = Transaction::get();         
$result = $conn->query($sql);

if($result){
    while($row = $result->fetch(PDO::FETCH_OBJ)){
        $data[] = array($row->descricao, $row->estoque);                    
    }
            return json_encode($data);              
}

Note: When inspecting for firebug the return of PHP code is:

[["frango","200.000"],["Bacon","15.000"],["Calabresa","100.000"],["Carne Moida","20.000"]]

This is my Angular JS code

'use strict';

angular.module('app').controller('painelController', function($scope, $http, $window, $location){
    //Define metodos para graficos
    $scope.chartAlimentos = function(){
        $http.post(url + 'server/modulos/materia_prima/alimentos.php', {action:'loadChart'}).success(function(response){                                    
            //Gera o grafico na view            
            $('#chart-alimentos').highcharts({
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false
                },
                title: {
                    text: 'Estoque atual' 
                },
                tooltip: {
                    pointFormat: '<b>Percentual</b>: {point.percentage:.1f} %'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '{point.name}: <b>{point.y} Kg</b>',
                            style: {
                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            },
                            showInLegend: true
                        }
                    }               
                },
                series: [{
                    type: 'pie',
                    data: [ response ]
                }],
                credits: {
                    enabled: false
                }, 
                exporting: {
                    enabled: true
                }
            });         

        });
    }

});

Note: I saw in some tutorials that people ask to use the following code in the graph script:

series: [{
    type: 'pie',
    data: ['<?php echo join(',',$sjon) ?>' ]
}],

But in my case it is not possible due to separation of client and server codes

And finally the error that occurs is that the graph does not render correctly. I do not understand the error because if I copy and paste the return of php that descrivi above works normally

1 answer

1

Ai staff managed to solve the problem, so I realized I was passing the correct values but a small detail in my PHP code made all the difference, as ei was passing a string strings to the hightchart and he was expecting integer values used the function (int) php to convert an array to integer

        if($result){
            while($row = $result->fetch(PDO::FETCH_OBJ)){
                $data[] = array("{$row->descricao}", (int)$row->estoque);                   
            }
            return json_encode($data);

            //Exemplo saída para grafico hightchart tipo PIE valores strings com aspas duplas, valores inteiros sem aspas
            //ex.: '[["frango",15.000],["Calabresa",70.000],["Bacon",200.000]]';                
        }    insira o código aqui

A big hug to all....

  • Hug. All the best for you and your family.

Browser other questions tagged

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