Chart.js/samples/doughnut.html

177 lines
5.1 KiB
HTML
Raw Normal View History

<!doctype html>
<html>
<head>
<title>Doughnut Chart</title>
2016-02-12 07:45:25 +01:00
<script src="../dist/Chart.bundle.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<div id="canvas-holder" style="width:75%">
<canvas id="chart-area" />
</div>
<button id="randomizeData">Randomize Data</button>
<button id="addDataset">Add Dataset</button>
<button id="removeDataset">Remove Dataset</button>
<button id="addData">Add Data</button>
<button id="removeData">Remove Data</button>
<script>
var randomScalingFactor = function() {
2015-06-03 22:14:23 +02:00
return Math.round(Math.random() * 100);
};
var randomColorFactor = function() {
2015-06-03 22:14:23 +02:00
return Math.round(Math.random() * 255);
};
var randomColor = function(opacity) {
return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
};
2015-06-03 22:14:23 +02:00
var config = {
type: 'doughnut',
2015-06-03 22:14:23 +02:00
data: {
datasets: [{
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
],
backgroundColor: [
"#F7464A",
"#46BFBD",
"#FDB45C",
"#949FB1",
"#4D5360",
],
2015-12-05 02:32:18 +01:00
label: 'Dataset 1'
2015-05-16 06:34:08 +02:00
}, {
hidden: true,
2015-06-03 22:14:23 +02:00
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
],
backgroundColor: [
"#F7464A",
"#46BFBD",
"#FDB45C",
"#949FB1",
"#4D5360",
],
2015-12-05 02:32:18 +01:00
label: 'Dataset 2'
2015-05-16 06:34:08 +02:00
}, {
2015-06-03 22:14:23 +02:00
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
],
backgroundColor: [
"#F7464A",
"#46BFBD",
"#FDB45C",
"#949FB1",
"#4D5360",
],
2015-12-05 02:32:18 +01:00
label: 'Dataset 3'
2015-06-03 22:14:23 +02:00
}],
labels: [
"Red",
"Green",
"Yellow",
"Grey",
"Dark Grey"
]
},
options: {
2015-12-04 04:03:39 +01:00
responsive: true,
legend: {
position: 'top',
},
title: {
display: true,
2016-02-28 09:16:09 +01:00
text: 'Chart.js Doughnut Chart'
}
2015-06-03 22:14:23 +02:00
}
2015-05-16 06:34:08 +02:00
};
2014-06-29 19:36:04 +02:00
window.onload = function() {
var ctx = document.getElementById("chart-area").getContext("2d");
window.myDoughnut = new Chart(ctx, config);
};
$('#randomizeData').click(function() {
$.each(config.data.datasets, function(i, dataset) {
dataset.data = dataset.data.map(function() {
return randomScalingFactor();
});
dataset.backgroundColor = dataset.backgroundColor.map(function() {
return randomColor(0.7);
2015-06-03 23:18:42 +02:00
});
});
window.myDoughnut.update();
});
$('#addDataset').click(function() {
var newDataset = {
backgroundColor: [],
data: [],
2015-12-05 02:32:18 +01:00
label: 'New dataset ' + config.data.datasets.length,
};
for (var index = 0; index < config.data.labels.length; ++index) {
newDataset.data.push(randomScalingFactor());
newDataset.backgroundColor.push(randomColor(0.7));
}
config.data.datasets.push(newDataset);
window.myDoughnut.update();
});
$('#addData').click(function() {
if (config.data.datasets.length > 0) {
config.data.labels.push('data #' + config.data.labels.length);
$.each(config.data.datasets, function(index, dataset) {
dataset.data.push(randomScalingFactor());
dataset.backgroundColor.push(randomColor(0.7));
});
window.myDoughnut.update();
}
});
$('#removeDataset').click(function() {
config.data.datasets.splice(0, 1);
window.myDoughnut.update();
});
$('#removeData').click(function() {
config.data.labels.splice(-1, 1); // remove the label first
config.data.datasets.forEach(function(dataset, datasetIndex) {
dataset.data.pop();
dataset.backgroundColor.pop();
});
window.myDoughnut.update();
});
</script>
</body>
2014-06-29 19:36:04 +02:00
</html>