]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Legends for Line, Bar & Radar charts.
authorEvert Timberg <evert.timberg@gmail.com>
Sun, 5 Jul 2015 18:47:56 +0000 (14:47 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Sun, 5 Jul 2015 18:47:56 +0000 (14:47 -0400)
samples/bar.html
samples/line.html
samples/radar.html
src/charts/Chart.Scatter.js
src/core/core.controller.js
src/core/core.js

index 1d085208e4ce4385f634d7651ec707bff55fedc6..eb8796ac5cb4d1e1f8760eeccbe0f78d0de5f39b 100644 (file)
     <button id="removeDataset">Remove Dataset</button>
     <button id="addData">Add Data</button>
     <button id="removeData">Remove Data</button>
+    <div>
+        <h3>Legend</h3>
+        <div id="legendContainer">
+            
+        </div>
+    </div>
     <script>
     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, {
@@ -53,6 +66,8 @@
                 responsive: true,
             }
         });
+
+        updateLegend();
     };
 
     $('#randomizeData').click(function() {
@@ -65,6 +80,7 @@
 
         });
         window.myBar.update();
+        updateLegend();
     });
 
     $('#addDataset').click(function() {
@@ -79,6 +95,7 @@
         }
 
         window.myBar.addDataset(newDataset, 1);
+        updateLegend();
     });
 
     $('#addData').click(function() {
             for (var index = 0; index < barChartData.datasets.length; ++index) {
                 window.myBar.addData(randomScalingFactor(), index);
             }
+
+            updateLegend();
         }
     });
 
     $('#removeDataset').click(function() {
         window.myBar.removeDataset(0);
+        updateLegend();
     });
 
     $('#removeData').click(function() {
         barChartData.datasets.forEach(function(dataset, datasetIndex) {
             window.myBar.removeData(datasetIndex, -1);
         });
+        updateLegend();
     });
     </script>
 </body>
index edcca91ecfade5b6e2cd61c3333914f8da949812..28171cdea7448682b5732dcc4e662373d2758d28 100644 (file)
     <button id="removeDataset">Remove Dataset</button>
     <button id="addData">Add Data</button>
     <button id="removeData">Remove Data</button>
+    <div>
+        <h3>Legend</h3>
+        <div id="legendContainer">
+            
+        </div>
+    </div>
     <script>
     var randomScalingFactor = function() {
         return Math.round(Math.random() * 100 * (Math.random() > 0.5 ? -1 : 1));
     window.onload = function() {
         var ctx = document.getElementById("canvas").getContext("2d");
         window.myLine = new Chart(ctx, config);
+
+        updateLegend();
     };
 
+    function updateLegend() {
+        $legendContainer = $('#legendContainer');
+        $legendContainer.empty();
+        $legendContainer.append(window.myLine.generateLegend());
+    }
+
     $('#randomizeData').click(function() {
         $.each(config.data.datasets, function(i, dataset) {
             dataset.data = dataset.data.map(function() {
@@ -85,6 +99,7 @@
         });
 
         window.myLine.update();
+        updateLegend();
     });
 
     $('#addDataset').click(function() {
         }
 
         window.myLine.addDataset(newDataset);
+        updateLegend();
     });
 
     $('#addData').click(function() {
             for (var index = 0; index < config.data.datasets.length; ++index) {
                 window.myLine.addData(randomScalingFactor(), index);
             }
+
+            updateLegend();
         }
     });
 
     $('#removeDataset').click(function() {
         window.myLine.removeDataset(0);
+        updateLegend();
     });
 
     $('#removeData').click(function() {
         config.data.datasets.forEach(function(dataset, datasetIndex) {
             window.myLine.removeData(datasetIndex, -1);
         });
+
+        updateLegend();
     });
     </script>
 </body>
index 0605c76e7608828efc773cc3a7203b68f7b825de..b84b8458369c9ac573491fc809c968fc7a85c1e8 100644 (file)
     <button id="removeDataset">Remove Dataset</button>
     <button id="addData">Add Data</button>
     <button id="removeData">Remove Data</button>
+    ]<div>
+        <h3>Legend</h3>
+        <div id="legendContainer">
+            
+        </div>
+    </div>
     <script>
     var randomScalingFactor = function() {
         return Math.round(Math.random() * 100);
         }
     };
 
+    function updateLegend() {
+        $legendContainer = $('#legendContainer');
+        $legendContainer.empty();
+        $legendContainer.append(window.myRadar.generateLegend());
+    }
+
     window.onload = function() {
         window.myRadar = new Chart(document.getElementById("canvas"), config);
+        updateLegend();
     };
 
     $('#randomizeData').click(function() {
@@ -60,6 +73,7 @@
         });
 
         window.myRadar.update();
+        updateLegend();
     });
 
     $('#addDataset').click(function() {
@@ -78,6 +92,7 @@
         }
 
         window.myRadar.addDataset(newDataset);
+        updateLegend();
     });
 
     $('#addData').click(function() {
             for (var index = 0; index < config.data.datasets.length; ++index) {
                 window.myRadar.addData(randomScalingFactor(), index);
             }
+
+            updateLegend();
         }
     });
 
     $('#removeDataset').click(function() {
         window.myRadar.removeDataset(0);
+        updateLegend();
     });
 
     $('#removeData').click(function() {
         config.data.datasets.forEach(function(dataset, datasetIndex) {
             window.myRadar.removeData(datasetIndex, -1);
         });
+
+        updateLegend();
     });
     </script>
 </body>
index 3c784ff3212ac023f6dbc94e95b22229c430242b..8863ed5e6ddbc7e5cab0cabe5513a372d76b45a3 100644 (file)
@@ -23,9 +23,6 @@
                        }],
                },
 
-               //String - A legend template
-               legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].borderColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>",
-
                tooltips: {
                        template: "(<%= value.x %>, <%= value.y %>)",
                        multiTemplate: "<%if (datasetLabel){%><%=datasetLabel%>: <%}%>(<%= value.x %>, <%= value.y %>)",
index c7e29dcd3879af9a4f274f5124ca365d75536ce6..bd843631b03e1ea2dceeb2d3c082d16ea2a330f6 100644 (file)
                },
 
                generateLegend: function generateLegend() {
-                       return template(this.options.legendTemplate, this);
+                       return helpers.template(this.options.legendTemplate, this);
                },
 
                destroy: function destroy() {
index 85a99167148f8fab710fab4f2ad2e15a597afc26..42c214e1444a59b8acf1234aab20adfa92552132 100755 (executable)
                        defaultColor: 'rgba(0,0,0,0.1)',
 
                        // Element defaults defined in element extensions
-                       elements: {}
+                       elements: {},
 
+                       // Legend template string
+                       legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i = 0; i < data.datasets.length; i++){%><li><span style=\"background-color:<%=data.datasets[i].backgroundColor%>\"><%if(data.datasets[i].label){%><%=data.datasets[i].label%><%}%></span></li><%}%></ul>",
                },
        };