2015-06-13 01:10:11 +02:00
|
|
|
<!doctype html>
|
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
2015-06-13 19:57:55 +02:00
|
|
|
<title>Stacked Bar Chart</title>
|
2015-06-13 01:10:11 +02:00
|
|
|
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
|
|
|
|
<script src="../Chart.js"></script>
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<div style="width: 50%">
|
|
|
|
<canvas id="canvas" height="450" width="600"></canvas>
|
|
|
|
</div>
|
|
|
|
<button id="randomizeData">Randomize Data</button>
|
|
|
|
<script>
|
2015-09-23 03:31:26 +02:00
|
|
|
var randomScalingFactor = function() {
|
|
|
|
return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
|
|
|
|
};
|
|
|
|
var randomColorFactor = function() {
|
|
|
|
return Math.round(Math.random() * 255);
|
|
|
|
};
|
2015-06-13 01:10:11 +02:00
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
var barChartData = {
|
|
|
|
labels: ["January", "February", "March", "April", "May", "June", "July"],
|
|
|
|
datasets: [{
|
|
|
|
label: 'Dataset 1',
|
|
|
|
backgroundColor: "rgba(220,220,220,0.5)",
|
|
|
|
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
|
|
|
|
}, {
|
|
|
|
label: 'Dataset 2',
|
|
|
|
backgroundColor: "rgba(151,187,205,0.5)",
|
|
|
|
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
|
|
|
|
}, {
|
|
|
|
label: 'Dataset 3',
|
|
|
|
backgroundColor: "rgba(151,187,205,0.5)",
|
|
|
|
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
|
|
|
|
}]
|
2015-06-13 01:10:11 +02:00
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
};
|
|
|
|
window.onload = function() {
|
|
|
|
var ctx = document.getElementById("canvas").getContext("2d");
|
|
|
|
window.myBar = new Chart(ctx, {
|
|
|
|
type: 'bar',
|
|
|
|
data: barChartData,
|
|
|
|
options: {
|
2015-11-29 19:33:24 +01:00
|
|
|
tooltips: {
|
|
|
|
mode: 'label'
|
|
|
|
},
|
2015-09-23 03:31:26 +02:00
|
|
|
responsive: true,
|
|
|
|
scales: {
|
|
|
|
xAxes: [{
|
|
|
|
stacked: true,
|
|
|
|
}],
|
|
|
|
yAxes: [{
|
|
|
|
stacked: true
|
|
|
|
}]
|
|
|
|
}
|
2015-06-13 03:15:15 +02:00
|
|
|
}
|
2015-09-23 03:31:26 +02:00
|
|
|
});
|
|
|
|
};
|
2015-06-13 01:10:11 +02:00
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
$('#randomizeData').click(function() {
|
|
|
|
$.each(barChartData.datasets, function(i, dataset) {
|
|
|
|
dataset.backgroundColor = 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',.7)';
|
|
|
|
dataset.data = [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()];
|
2015-06-13 01:10:11 +02:00
|
|
|
|
2015-09-23 03:31:26 +02:00
|
|
|
});
|
|
|
|
window.myBar.update();
|
2015-06-13 01:10:11 +02:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
</html>
|