JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js"></script>
<canvas id="myChart" width="600" height="400"></canvas>
JavaScript
// 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() * 1000
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);
}