2019-12-27 23:13:24 +01:00
|
|
|
<!doctype html>
|
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<title>Stacked Bar Chart</title>
|
2020-05-25 22:54:22 +02:00
|
|
|
<script src="../../dist/chart.min.js"></script>
|
2019-12-27 23:13:24 +01:00
|
|
|
<script src="../utils.js"></script>
|
|
|
|
<style>
|
|
|
|
canvas {
|
|
|
|
-moz-user-select: none;
|
|
|
|
-webkit-user-select: none;
|
|
|
|
-ms-user-select: none;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<div style="width: 75%">
|
|
|
|
<canvas id="canvas"></canvas>
|
|
|
|
</div>
|
|
|
|
<button id="randomizeData">Randomize Data</button>
|
|
|
|
<script>
|
|
|
|
var barChartData = {
|
|
|
|
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
|
|
|
|
datasets: [{
|
|
|
|
label: 'Dataset 1',
|
|
|
|
backgroundColor: window.chartColors.red,
|
|
|
|
data: [
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor()
|
|
|
|
]
|
|
|
|
}, {
|
|
|
|
label: 'Dataset 2',
|
|
|
|
backgroundColor: window.chartColors.blue,
|
|
|
|
data: [
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor()
|
|
|
|
]
|
|
|
|
}, {
|
|
|
|
label: 'Dataset 3',
|
|
|
|
backgroundColor: window.chartColors.green,
|
|
|
|
data: [
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor(),
|
|
|
|
randomScalingFactor()
|
|
|
|
]
|
|
|
|
}]
|
|
|
|
|
|
|
|
};
|
|
|
|
window.onload = function() {
|
|
|
|
var ctx = document.getElementById('canvas').getContext('2d');
|
|
|
|
window.myBar = new Chart(ctx, {
|
|
|
|
type: 'bar',
|
|
|
|
data: barChartData,
|
|
|
|
options: {
|
|
|
|
animation: (context) => {
|
|
|
|
if (context.active) {
|
|
|
|
return {
|
|
|
|
duration: 400
|
|
|
|
};
|
|
|
|
}
|
|
|
|
var delay = 0;
|
|
|
|
var dsIndex = context.datasetIndex;
|
|
|
|
var index = context.dataIndex;
|
2020-11-01 13:39:08 +01:00
|
|
|
if (context.dataPoint && !context.delayed) {
|
2019-12-27 23:13:24 +01:00
|
|
|
delay = index * 300 + dsIndex * 100;
|
2020-11-01 13:39:08 +01:00
|
|
|
context.delayed = true;
|
2019-12-27 23:13:24 +01:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
easing: 'linear',
|
|
|
|
duration: 600,
|
|
|
|
delay
|
|
|
|
};
|
|
|
|
},
|
2020-11-25 07:50:12 +01:00
|
|
|
plugins: {
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'Chart.js Bar Chart - Stacked'
|
|
|
|
},
|
|
|
|
tooltip: {
|
|
|
|
mode: 'index',
|
|
|
|
intersect: false
|
|
|
|
}
|
2019-12-27 23:13:24 +01:00
|
|
|
},
|
|
|
|
responsive: true,
|
|
|
|
scales: {
|
|
|
|
x: {
|
|
|
|
stacked: true,
|
|
|
|
},
|
|
|
|
y: {
|
|
|
|
stacked: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
document.getElementById('randomizeData').addEventListener('click', function() {
|
|
|
|
barChartData.datasets.forEach(function(dataset) {
|
|
|
|
dataset.data = dataset.data.map(function() {
|
|
|
|
return randomScalingFactor();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
window.myBar.update();
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
</html>
|