Change color pie chart php

2

Good afternoon! I'm creating a pie chart, the data I'm looking for from the database.
But I do not know how to change the colors of the sectors of the pie chart, to make it clear I will show here what I want. Home The code is as follows:

include "../classif/BD/ligabd.php"; ... '     

  google.charts.load("current", {packages:["corechart"]});;
  google.charts.setOnLoadCallback(drawChart);

  function drawChart() 
  {
   var data = google.visualization.arrayToDataTable([
    ['classificacao','Number'],
    <?php
    while ($row=mysqli_fetch_array($result)) 
    {
      echo "['".$row["classificacao"]."', ".$row["number"]."],";
    }
    ?>
    ]);
   var options = {
    title:'Classificações do refeitório', 
    is3D:true
  };
  var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  chart.draw(data,options);
}

  Home   Graph of cafeteria classifications       ' Home The output is as follows:


WhatIintendedwasinsteadof"yellow" being the color blue, the color was yellow, where it says green, instead of being red it was green, and so on ...

    
asked by anonymous 19.06.2018 / 14:18

1 answer

3

You have a colors field in options .

Example

var options = {
  // .. outras opcoes
  colors: ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6']
};

chart.draw(data, options);

If you do not control the order of the data but want a specific item to have a certain color, you can change your while a bit.

<?php
$colors = [];
while ($row=mysqli_fetch_array($result)) 
{
  switch ($row['classificacao']) {
    case 'amarelo' : $colors[] = 'yellow'; break; // 'amarelo' deve ser o item que queres que seja amarelo. 'yellow' deve por o código da cor que queres
    case 'vermelho': $colors[] = 'red';    break;
    case 'azul'    : $colors[] = 'blue';   break;
  }
  echo "['".$row["classificacao"]."', ".$row["number"]."],";
}
?>
var options = {
  title:'Classificações do refeitório', 
  is3D:true,
  colors: <?php echo json_encode($colors) ?>
};
    
19.06.2018 / 14:21