2013-03-18 00:57:50 +01:00
|
|
|
<!doctype html>
|
|
|
|
<html>
|
2015-05-16 04:54:01 +02:00
|
|
|
|
2015-05-12 09:13:16 +02:00
|
|
|
<head>
|
|
|
|
<title>Bar Chart</title>
|
2015-05-20 09:03:45 +02:00
|
|
|
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
|
2015-05-12 09:13:16 +02:00
|
|
|
<script src="../Chart.js"></script>
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
2015-05-24 00:26:34 +02:00
|
|
|
<div style="width: 50%">
|
2015-05-12 09:13:16 +02:00
|
|
|
<canvas id="canvas" height="450" width="600"></canvas>
|
|
|
|
</div>
|
|
|
|
<button id="randomizeData">Randomize Data</button>
|
|
|
|
<script>
|
2015-05-16 04:54:01 +02:00
|
|
|
var randomScalingFactor = function() {
|
2015-05-20 09:03:45 +02:00
|
|
|
return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
|
2015-05-12 09:13:16 +02:00
|
|
|
};
|
|
|
|
var randomColorFactor = function() {
|
2015-05-20 09:03:45 +02:00
|
|
|
return Math.round(Math.random() * 255);
|
2015-05-12 09:13:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var barChartData = {
|
|
|
|
labels: ["January", "February", "March", "April", "May", "June", "July"],
|
|
|
|
datasets: [{
|
2015-05-12 20:55:48 +02:00
|
|
|
label: 'Dataset 1',
|
2015-05-12 09:13:16 +02:00
|
|
|
backgroundColor: "rgba(220,220,220,0.5)",
|
|
|
|
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
|
|
|
|
}, {
|
2015-05-12 20:55:48 +02:00
|
|
|
label: 'Dataset 2',
|
2015-05-12 09:13:16 +02:00
|
|
|
backgroundColor: "rgba(151,187,205,0.5)",
|
|
|
|
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
|
2015-05-20 09:03:45 +02:00
|
|
|
}, {
|
|
|
|
label: 'Dataset 3',
|
|
|
|
backgroundColor: "rgba(151,187,205,0.5)",
|
|
|
|
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
|
|
|
|
}, {
|
|
|
|
label: 'Dataset 4',
|
|
|
|
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-05-12 09:13:16 +02:00
|
|
|
}]
|
|
|
|
|
2015-05-20 09:03:45 +02:00
|
|
|
};
|
2015-05-12 09:13:16 +02:00
|
|
|
window.onload = function() {
|
|
|
|
var ctx = document.getElementById("canvas").getContext("2d");
|
2015-05-20 15:03:22 +02:00
|
|
|
window.myBar = new Chart(ctx).Bar({
|
|
|
|
data: barChartData,
|
|
|
|
options: {
|
|
|
|
responsive: true,
|
|
|
|
hoverMode: 'label',
|
|
|
|
scaleBeginAtZero: false,
|
|
|
|
hoverAnimationDuration: 400,
|
|
|
|
stacked: true,
|
|
|
|
}
|
2015-05-12 09:13:16 +02:00
|
|
|
});
|
2015-05-20 09:03:45 +02:00
|
|
|
};
|
2015-05-12 09:13:16 +02:00
|
|
|
|
|
|
|
$('#randomizeData').click(function() {
|
2015-05-20 09:03:45 +02:00
|
|
|
$.each(barChartData.datasets, function(i, dataset) {
|
|
|
|
dataset.backgroundColor = 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',.7)';
|
|
|
|
dataset.data = [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()];
|
2015-05-12 09:13:16 +02:00
|
|
|
|
2015-05-20 09:03:45 +02:00
|
|
|
});
|
2015-05-12 09:13:16 +02:00
|
|
|
window.myBar.update();
|
2015-01-08 18:13:14 +01:00
|
|
|
});
|
2015-05-12 09:13:16 +02:00
|
|
|
</script>
|
|
|
|
</body>
|
2015-05-16 04:54:01 +02:00
|
|
|
|
2013-03-18 00:57:50 +01:00
|
|
|
</html>
|