Chart.js/samples/charts/pie.html

98 lines
2.8 KiB
HTML
Raw Normal View History

<!doctype html>
<html>
2015-06-03 22:14:23 +02:00
<head>
<title>Pie Chart</title>
<script src="../../dist/Chart.bundle.js"></script>
<script src="../utils.js"></script>
2015-06-03 22:14:23 +02:00
</head>
2015-06-03 22:14:23 +02:00
<body>
<div id="canvas-holder" style="width:40%">
<canvas id="chart-area" />
2015-06-03 22:14:23 +02:00
</div>
<button id="randomizeData">Randomize Data</button>
<button id="addDataset">Add Dataset</button>
<button id="removeDataset">Remove Dataset</button>
2015-06-03 22:14:23 +02:00
<script>
var randomScalingFactor = function() {
return Math.round(Math.random() * 100);
};
2015-06-03 23:18:42 +02:00
var config = {
2015-06-16 06:47:20 +02:00
type: 'pie',
2015-06-03 23:18:42 +02:00
data: {
datasets: [{
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
],
backgroundColor: [
window.chartColors.red,
window.chartColors.orange,
window.chartColors.yellow,
window.chartColors.green,
window.chartColors.blue,
2015-06-03 23:18:42 +02:00
],
label: 'Dataset 1'
2015-06-03 23:18:42 +02:00
}],
labels: [
"Red",
"Orange",
2015-06-03 23:18:42 +02:00
"Yellow",
"Green",
"Blue"
2015-06-03 23:18:42 +02:00
]
},
options: {
responsive: true
2015-06-03 22:14:23 +02:00
}
2015-06-03 23:18:42 +02:00
};
2015-06-03 22:14:23 +02:00
window.onload = function() {
var ctx = document.getElementById("chart-area").getContext("2d");
2015-06-16 06:47:20 +02:00
window.myPie = new Chart(ctx, config);
2015-06-03 22:14:23 +02:00
};
2014-06-29 19:36:04 +02:00
document.getElementById('randomizeData').addEventListener('click', function() {
config.data.datasets.forEach(function(dataset) {
dataset.data = dataset.data.map(function() {
return randomScalingFactor();
2015-06-03 23:18:42 +02:00
});
2015-06-03 22:14:23 +02:00
});
2015-06-03 22:14:23 +02:00
window.myPie.update();
});
var colorNames = Object.keys(window.chartColors);
document.getElementById('addDataset').addEventListener('click', function() {
var newDataset = {
backgroundColor: [],
data: [],
label: 'New dataset ' + config.data.datasets.length,
};
for (var index = 0; index < config.data.labels.length; ++index) {
newDataset.data.push(randomScalingFactor());
var colorName = colorNames[index % colorNames.length];;
var newColor = window.chartColors[colorName];
newDataset.backgroundColor.push(newColor);
}
config.data.datasets.push(newDataset);
window.myPie.update();
});
document.getElementById('removeDataset').addEventListener('click', function() {
config.data.datasets.splice(0, 1);
window.myPie.update();
});
2015-06-03 22:14:23 +02:00
</script>
</body>
</html>