Pie Chart Legend settings - Javascript Chart.js

Javascript examples for Chart.js:Legend

Description

Pie Chart Legend settings

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){// ww  w.  j  ava  2 s  .com
var tempData = {
    labels: ["EM TRAMITACAO", "ENCERRADO", "DEFERIDO"],
    datasets: [{
        label: "Status",
        data: [2, 6, 0],
        backgroundColor: ["#339966", "#144FFF", "#006666"]
    }]
};
var ctx = document.getElementById("pieStatus");
var chartInstance = new Chart(ctx, {
    type: 'pie',
    data: tempData,
    options: {
        title: {
            display: true,
            fontsize: 14,
            text: 'Total de Pedidos por Situa??o'
        },
        legend: {
            display: true,
            position: 'bottom',
            labels: {
                generateLabels: function(chart) {
                    var data = chart.data;
                    if (data.labels.length && data.datasets.length) {
                        return data.labels.map(function(label, i) {
                            var meta = chart.getDatasetMeta(0);
                            var ds = data.datasets[0];
                            var arc = meta.data[i];
                            var custom = arc && arc.custom || {};
                            var getValueAtIndexOrDefault = Chart.helpers.getValueAtIndexOrDefault;
                            var arcOpts = chart.options.elements.arc;
                            var fill = custom.backgroundColor ? custom.backgroundColor : getValueAtIndexOrDefault(ds.backgroundColor, i, arcOpts.backgroundColor);
                            var stroke = custom.borderColor ? custom.borderColor : getValueAtIndexOrDefault(ds.borderColor, i, arcOpts.borderColor);
                            var bw = custom.borderWidth ? custom.borderWidth : getValueAtIndexOrDefault(ds.borderWidth, i, arcOpts.borderWidth);
                     var value = chart.config.data.datasets[arc._datasetIndex].data[arc._index];
                            return {
                                text: label + " : " + value,
                                fillStyle: fill,
                                strokeStyle: stroke,
                                lineWidth: bw,
                                hidden: isNaN(ds.data[i]) || meta.data[i].hidden,
                                index: i
                            };
                        });
                    } else {
                        return [];
                    }
                }
            }
        }
    }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="pieStatus" height="200"></canvas>  
   </body>
</html>

Related Tutorials