Chartjs-How to give link for lable/Legend in chart.js? and if the value is 0 how to remove the legend?

0πŸ‘

βœ…

The legendTemplate option allows you to control your legend content, like so

...
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {
    responsive: true,
    legendTemplate: '<ul class="<%=name.toLowerCase()%>-legend"><% for (var i=0; i<segments.length; i++){ if (segments[i].value) { %><li><span style="background-color:<%=segments[i].fillColor%>"></span><%if(segments[i].label){%><a href="<%=links[i]%>"><%=segments[i].label%></a><%}%></li><%}}%></ul>'
});
myDoughnut.links = ["mylink1", "mylink2", "mylink3", "mylink4", "mylink5", "mylink6"];
...

1πŸ‘

In version 2.x, you have to use the prototype method generateLegend scroll down here: http://www.chartjs.org/docs/#advanced-usage-prototype-methods

{
  generateLegend: function(chartInstance) {
    //return html legend string here
  }
}

Leave a comment