Topic: How to have pie chart legend
rajneesh ojha asked 8 years ago
Marta Szymanska staff pro premium answered 8 years ago
<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;
}
rajneesh ojha answered 8 years ago
Marta Szymanska staff pro premium answered 8 years ago
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
- User: Free
- Premium support: No
- Technology: General Bootstrap questions
- MDB Version: -
- Device: -
- Browser: -
- OS: -
- Provided sample code: No
- Provided link: No