]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Doughnut legends (only makes sense for first dataset).
authorEvert Timberg <evert.timberg@gmail.com>
Sun, 5 Jul 2015 18:56:22 +0000 (14:56 -0400)
committerEvert Timberg <evert.timberg@gmail.com>
Sun, 5 Jul 2015 18:56:22 +0000 (14:56 -0400)
samples/doughnut.html
src/charts/Chart.Doughnut.js

index f2c4282f848409b3fe35869f5660390e9a996f14..ae6bbc9e585926dff21e1862bab45bbd9047aec8 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);
@@ -38,7 +44,6 @@
     };
 
     var config = {
-        type: 'doughnut',
         data: {
             datasets: [{
                 data: [
         }
     };
 
+    function updateLegend() {
+        $legendContainer = $('#legendContainer');
+        $legendContainer.empty();
+        $legendContainer.append(window.myDoughnut.generateLegend());
+    }
+
     window.onload = function() {
         var ctx = document.getElementById("chart-area").getContext("2d");
-        window.myDoughnut = new Chart(ctx, config);
+        window.myDoughnut = Chart.Doughnut(ctx, config);
         console.log(window.myDoughnut);
+
+        updateLegend();
     };
 
     $('#randomizeData').click(function() {
         });
 
         window.myDoughnut.update();
+        updateLegend();
     });
 
     $('#addDataset').click(function() {
         }
 
         window.myDoughnut.addDataset(newDataset);
+        updateLegend();
     });
 
     $('#addData').click(function() {
             $.each(config.data.datasets, function(index, dataset) {
                 window.myDoughnut.addData(randomScalingFactor(), index, dataset.data.length, randomColor(0.7));
             });
+
+            updateLegend();
         }
     });
 
     $('#removeDataset').click(function() {
         window.myDoughnut.removeDataset(0);
+        updateLegend();
     });
 
     $('#removeData').click(function() {
         config.data.datasets.forEach(function(dataset, datasetIndex) {
             window.myDoughnut.removeData(datasetIndex, -1);
         });
+
+        updateLegend();
     });
     </script>
 </body>
index d01959d448fd799de08bb921806e6170e141a6e5..ccea714f31d0d937f1933f0c76b27fa45589e73a 100644 (file)
@@ -5,7 +5,12 @@
        var Chart = root.Chart;
        var helpers = Chart.helpers;
 
+       var defaultConfig = {
+               legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i = 0; i < data.datasets[0].data.length; i++){%><li><span style=\"background-color:<%=data.datasets[0].backgroundColor[i]%>\"><%if(data.labels && i < data.labels.length){%><%=data.labels[i]%><%}%></span></li><%}%></ul>",
+       };
+
        Chart.Doughnut = function(context, config) {
+               config.options = helpers.configMerge(defaultConfig, config.options);
                config.type = 'doughnut';
 
                return new Chart(context, config);