]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Bar stacked sample
authorEvert Timberg <evert.timberg@gmail.com>
Fri, 12 Jun 2015 23:10:11 +0000 (19:10 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Fri, 12 Jun 2015 23:10:11 +0000 (19:10 -0400)
samples/bar-stacked.html [new file with mode: 0644]

diff --git a/samples/bar-stacked.html b/samples/bar-stacked.html
new file mode 100644 (file)
index 0000000..da9e4fb
--- /dev/null
@@ -0,0 +1,62 @@
+<!doctype html>
+<html>
+
+<head>
+    <title>Bar Chart</title>
+    <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>
+    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);
+    };
+
+    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()]
+        }]
+
+    };
+    window.onload = function() {
+        var ctx = document.getElementById("canvas").getContext("2d");
+        window.myBar = new Chart(ctx).Bar({
+            data: barChartData,
+            options: {
+                responsive: true,
+                stacked: true,
+            }
+        });
+    };
+
+    $('#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()];
+
+        });
+        window.myBar.update();
+    });
+    </script>
+</body>
+
+</html>