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
Hug. All the best for you and your family.
– Diego Souza