Generate different colors Google Chart

Asked

Viewed 191 times

0

I’m having doubts about how to generate colors directly in config when executing the code and where to add colors to be generated , if in this case I should generate another graphic 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);

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: https://developers.google.com/chart/interactive/docs/lines#fullhtml

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

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

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

Browser other questions tagged

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