Topic: How to have pie chart legend

rajneesh ojha free asked 6 years ago

I was wondering how can i add legend in pie chart like this

Marta Wierzbicka staff answered 6 years ago

Yes, it is undocumented but it will be.

rajneesh ojha free answered 6 years ago

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

Marta Wierzbicka staff answered 6 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,
        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 {
           margin-right: 10px; 
        ul.pie-legend li span {
            padding: 5px 10px;
            color: #fff;

Please insert min. 20 characters.


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


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