]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
improve bar sample
authorVille Hämäläinen <hamalainen.ville.p@gmail.com>
Sun, 28 Feb 2016 08:00:01 +0000 (10:00 +0200)
committerVille Hämäläinen <hamalainen.ville.p@gmail.com>
Sun, 28 Feb 2016 08:00:01 +0000 (10:00 +0200)
- set title
- set same size canvas as other samples
- remove user select from canvas
- remove html legend
- fix x-axis labels when adding data

samples/bar.html

index c0c5e36f91c1a9475d49019089d8bb831377b396..9bdb366ac4e31a025e43c98f497d9ba6296a0650 100644 (file)
@@ -5,29 +5,27 @@
     <title>Bar Chart</title>
     <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
     <script src="../dist/Chart.bundle.js"></script>
-    <style type="text/css">
-        canvas {
-            border: 1px solid red;
-        }
+    <style>
+    canvas {
+        -moz-user-select: none;
+        -webkit-user-select: none;
+        -ms-user-select: none;
+    }
     </style>
 </head>
 
 <body>
-    <div id="container" style="width: 50%; height: 25%;">
-        <canvas id="canvas" height="450" width="600"></canvas>
+    <div id="container" style="width: 75%;">
+        <canvas id="canvas"></canvas>
     </div>
     <button id="randomizeData">Randomize Data</button>
     <button id="addDataset">Add Dataset</button>
     <button id="removeDataset">Remove Dataset</button>
     <button id="addData">Add Data</button>
     <button id="removeData">Remove Data</button>
-    <button id="show">Show</button>
-    <div>
-        <h3>Legend</h3>
-        <div id="legendContainer">
-        </div>
-    </div>
     <script>
+        var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
+
         var randomScalingFactor = function() {
             return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
         };
 
         };
 
-        function updateLegend() {
-            $legendContainer = $('#legendContainer');
-            $legendContainer.empty();
-            $legendContainer.append(window.myBar.generateLegend());
-        }
-
         window.onload = function() {
             var ctx = document.getElementById("canvas").getContext("2d");
             window.myBar = new Chart(ctx, {
                     },
                     title: {
                         display: true,
-                        text: 'Our 3 Favorite Datasets'
+                        text: 'Chart.js Bar Chart'
                     }
                 }
             });
 
-            updateLegend();
         };
 
         $('#randomizeData').click(function() {
 
             });
             window.myBar.update();
-            updateLegend();
         });
 
         $('#addDataset').click(function() {
 
             barChartData.datasets.push(newDataset);
             window.myBar.update();
-            updateLegend();
         });
 
         $('#addData').click(function() {
             if (barChartData.datasets.length > 0) {
-                barChartData.labels.push('data #' + barChartData.labels.length);
+                var month = MONTHS[barChartData.labels.length % MONTHS.length];
+                barChartData.labels.push(month);
 
                 for (var index = 0; index < barChartData.datasets.length; ++index) {
                     //window.myBar.addData(randomScalingFactor(), index);
                 }
 
                 window.myBar.update();
-                updateLegend();
             }
         });
 
         $('#removeDataset').click(function() {
             barChartData.datasets.splice(0, 1);
             window.myBar.update();
-            updateLegend();
         });
 
         $('#removeData').click(function() {
             });
 
             window.myBar.update();
-            updateLegend();
-        });
-
-        $('#show').click(function() {
-            document.getElementById('container').style.display = '';
         });
     </script>
 </body>