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