Generate different colors Google Chart

0

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);
    
asked by anonymous 30.11.2016 / 21:04

1 answer

0

Resolved - Resolution

To use custom colors in the chart, you need to pass them in the config array as shown in the API: link

Within the config array, you pass the "series" option which must be an array with the colors you intend to use.

So just create the array "series" before the while (empty), then include that line within the while:

$ graph ['config'] ['series'] [] = $ obj-> color;

    
02.12.2016 / 11:19