I am having doubts about how to generate the colors directly in the config when executing the code and where to add the colors to be generated, if in this case I should generate another graphical array? array('colors' => $obj->color)
or should I add another while to the config?
$grafico['config'][] = array('c' => array(
array('v' => $obj->color),
array('v' => (int)$obj->total)
));
// Estrutura basica do grafico
$grafico = array(
'dados' => array(
'cols' => array(
array('type' => 'string', 'label' => 'dados'),
array('type' => 'number', 'label' => 'Total')
),
'rows' => array()
),
'config' => array(
//'pieSliceText'=> 'label',
'is3D' => 'true',
'colors' => '', // Aqui as cores vindas da database
'title' => 'Quantidade de eventos',
'width' => 800,
'height' => 300
)
);
// Consultar dados no BD
$pdo = new PDO('mysql:host=localhost;dbname=ccs', 'cc', 'xxx);
$sql = 'SELECT category.*,(SELECT COUNT(*) FROM event WHERE category_id = category.id) as total FROM category';
$stmt = $pdo->query($sql);
while ($obj = $stmt->fetchObject()) {
$grafico['dados']['rows'][] = array('c' => array(
array('v' => $obj->name),
array('v' => (int)$obj->total)
));
}
// Enviar dados na forma de JSON
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($grafico);
exit(0);