I'm trying to display a PieChart, however, if the search in the database returns only one row, the chart is displayed. If you return more than one line, the graph does not appear.
function that generates graph data:
public function grpAplicacaoRecursos() {
// Estrutura basica do grafico
$grafico = array(
'dados' => array(
'cols' => array(
array('type' => 'string', 'label' => 'Descricao'),
array('type' => 'number', 'label' => 'Valor')
),
'rows' => array()
),
'config' => array(
'title' => 'Para onde vai meu dinheiro',
'width' => 400,
'height' => 250
)
);
$dataAtual = new DateTime('now');
$inicioUltimaSemana = date('Y-m-d', strtotime($dataAtual->format('Y-m-d').' -365 days'));
$dados = $this->registry->conn->fetchAll('SELECT c.descricao descricao, sum(m.valor) valor '.
'FROM movimento_conta m left join conta c on m.conta_id = c.conta_id where m.data '.
'between ? and ? group by descricao ', array($inicioUltimaSemana, $dataAtual->format('Y-m-d')));
foreach ($dados as $row) {
$grafico['dados']['rows'][] = array('c' => array(
array('v' => $row['descricao']),
array('v' => $row['valor'])
));
}
// Enviar dados na forma de JSON
header( 'Cache-Control: no-cache' );
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($grafico);
}
function return:
{
"dados":{
"cols":[
{
"type":"string",
"label":"Descricao"
},
{
"type":"number",
"label":"Valor"
}
],
"rows":[
{
"c":[
{
"v":"teste1"
},
{
"v":"554.890"
}
]
},
{
"c":[
{
"v":"teste2"
},
{
"v":"2556.000"
}
]
}
]
},
"config":{
"title":"Para onde vai meu dinheiro",
"width":400,
"height":250
}
}
script on the page that displays the graphic:
<script type="text/javascript">
google.load('visualization', '1.0', {'packages': ['corechart']});
google.setOnLoadCallback(function() {
var json_text = $.ajax({url: "/index/grpAplicacaoRecursos", dataType: "json", async: false}).responseText;
var json = eval("(" + json_text + ")");
var dados = new google.visualization.DataTable(json.dados);
var chart = new google.visualization.PieChart(document.getElementById('area_grafico2'));
chart.draw(dados, json.config);
});
</script>
Can anyone tell me what I'm doing wrong?