title: Chartjs random colors for each part of pie chart with data dynamically from database
body: <p>I want to set dynamically a color for each part of Pie Chart. Since the chart is dynamically created from database I want for each part that is added to the chart(from database) a different color.</p>
  
  <p>I was trying to do this:</p>
  
  <pre><code>$(document).ready(function() {
  $.ajax({
  url: "http://localhost/chartjs/projects_chart.php",
  method: "GET",
  success: function(data) {
      console.log(data);
      var ict_unit = [];
      var efficiency = [];
      var dynamicColors = function() {
          var r = Math.floor(Math.random() * 255);
          var g = Math.floor(Math.random() * 255);
          var b = Math.floor(Math.random() * 255);
          return "rgb(" + r + "," + g + "," + b + ")";
      };
  
      for (var i in data) {
          ict_unit.push("ICT Unit " + data[i].ict_unit);
          efficiency.push(data[i].efficiency);
           var coloR=dynamicColors();
      }
      var chartData = {
  
          labels: ict_unit,
          datasets: [{
              label: 'Efficiency ',
              //strokeColor:backGround,
  
              backgroundColor: [coloR],
  
              borderColor: 'rgba(200, 200, 200, 0.75)',
              //hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
              hoverBorderColor: 'rgba(200, 200, 200, 1)',
              data: efficiency
          }]
      };
  
      var ctx = $("#my-canvas");
      var barGraph = new Chart(ctx, {
          type: 'pie',
          data: chartData
      })
  },
  error: function(data) {
  
      console.log(data);
     },
    });
   });
  </code></pre>
  
  <p>But I only get one color for the first part of the pie chart.</p>
  
  <p>Could you help me?</p>