Chart.js/samples/bar.html

62 lines
2.3 KiB
HTML
Raw Normal View History

<!doctype html>
<html>
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>
<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>
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()]
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-06-15 03:15:10 +02:00
window.myBar = new Chart(ctx, {
2015-06-03 00:48:39 +02:00
data: barChartData,
options: {
responsive: 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-05-12 09:13:16 +02:00
</script>
</body>
</html>