How to have pie chart legend


Topic: How to have pie chart legend

rajneesh ojha asked 8 years ago

I was wondering how can i add legend in pie chart like this http://www.chartjs.org/docs/#doughnut-pie-chart-introduction

Marta Szymanska staff pro premium answered 8 years ago

If you want add legend in pie chart paste this code in HTML file:
<canvas id="myChart"></canvas>
            <div id="legendDiv"></div>
then in SCRIPT paste this code:
$(function () {
    
var data = [
    {
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red"
    },
    {
        value: 50,
        color: "#46BFBD",
        highlight: "#5AD3D1",
        label: "Green"
    },
    {
        value: 100,
        color: "#FDB45C",
        highlight: "#FFC870",
        label: "Yellow"
    }
]

   
    var option = {
    responsive: true,
        
       
    };
   
    // Get the context of the canvas element we want to select
    var ctx = document.getElementById("myChart").getContext('2d');
    
var myPieChart = new Chart(ctx).Pie(data,option);
    document.getElementById("legendDiv").innerHTML = myPieChart.generateLegend();
    
});
And in the end in CSS file paste this code:
ul.pie-legend {
            text-align: center;
            margin-top: 2rem;
        }
        ul.pie-legend li {
           display:inline-block;
           margin-right: 10px; 
        }
        ul.pie-legend li span {
            padding: 5px 10px;
            color: #fff;
        }

Thank you Marta, It seems this feature is undocumented or am i unable to find this in documentation?

Marta Szymanska staff pro premium answered 8 years ago

Yes, it is undocumented but it will be.
Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: General Bootstrap questions
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: No
  • Provided link: No