set color family to pie chart - Javascript Chart.js

Javascript examples for Chart.js:Pie Chart

Description

set color family to pie chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from w w w .  j a v a2s  . c om
// Pie chart or doughnut chart with chart.js
var ctx = document.getElementById("myChart").getContext("2d");
var data = [
  //   { value: 300, color:"#F7464A", highlight: "#FF5A5E", label: "sample" },
] ;
// data is empty.
// we'll thow in between 10 and 50 items randomly generated
entries = 10 + Math.floor(Math.random() * 40);
for (i = 0; i < entries; i++) {
    r = Math.floor(Math.random() * 200);
    g = Math.floor(Math.random() * 200);
    b = Math.floor(Math.random() * 200);
    v = Math.floor(Math.random() * 500);
    c = 'rgb(' + r + ', ' + g + ', ' + b + ')';
    h = 'rgb(' + (r+20) + ', ' + (g+20) + ', ' + (b+20) + ')';
    data.push( {
      value : v,
      label : 'item ' + i,
      color: c,
      highlight: h
    }) ;
};
var options = { } ;
if ( entries % 2 ) {
var myChart = new Chart(ctx).Pie(data,options);
}
else {
var myChart = new Chart(ctx).Doughnut(data,options);
}
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" width="600" height="400"></canvas>  
   </body>
</html>

Related Tutorials