Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Legend with ChartJS v2.0

I'm trying to create a custom legend template in ChartJS v2.0. In v1* of ChartJS I simply added a property to the new Chart constructor such as...

legendTemplate : '<ul>' +'<% for (var i=0; i<datasets.length; i++) { %>' +'<li>' +'<span style=\"background-color:<%=datasets[i].lineColor%>\"></span>' +'<% if (datasets[i].label) { %><%= datasets[i].label %><% } %>' +'</li>' +'<% } %>' +'</ul>' 

I can't seem to find any documentation in v2.0 for this option. Is it even available anymore? Can anyone show an example of how to accomplish this?

Thank you!

Update - Working code below

legendCallback: function(chart) {                 console.log(chart.data);                 var text = [];                 text.push('<ul>');                 for (var i=0; i<chart.data.datasets[0].data.length; i++) {                     text.push('<li>');                     text.push('<span style="background-color:' + chart.data.datasets[0].backgroundColor[i] + '">' + chart.data.datasets[0].data[i] + '</span>');                     if (chart.data.labels[i]) {                         text.push(chart.data.labels[i]);                     }                     text.push('</li>');                 }                 text.push('</ul>');                 return text.join("");             } 
like image 266
Phil Avatar asked May 03 '16 13:05

Phil


People also ask

How do you customize a legend in Chartjs?

Adding a legend in chartJS 3.0+ requires the use of plugins. Basically you can create a custom plugin when you instantiate the chart. For example: const context = document.

How do you hide a legend in Chartjs?

To remove legend on charts with Chart. js v2 and JavaScript, we can set the options. legend to false . const chart1 = new Chart(canvas, { type: "pie", data: data, options: { legend: { display: false, }, tooltips: { enabled: false, }, }, });

How do I add a label in Chartjs?

All you have to do is define an options object and add the label wherever and however you want it in the tooltip. In your data items, you have to add the desired label property and value and that's all. data = [ { value: 480000, color:"#F7464A", highlight: "#FF5A5E", label: "Tobacco" } ];


2 Answers

If you guys run through this post and tried the posted answer and didn't work, try this one:

  legendCallback: function(chart) {     var text = [];     text.push('<ul>');     for (var i=0; i<chart.data.datasets.length; i++) {       console.log(chart.data.datasets[i]); // see what's inside the obj.       text.push('<li>');       text.push('<span style="background-color:' + chart.data.datasets[i].borderColor + '">' + chart.data.datasets[i].label + '</span>');       text.push('</li>');     }     text.push('</ul>');     return text.join("");   }, 

Then add this below:

document.getElementById('chart-legends').innerHTML = myChart.generateLegend(); 

To create the legends. Make sure you have an element <div id="chart-legends"></div>

like image 164
Robin Carlo Catacutan Avatar answered Sep 19 '22 20:09

Robin Carlo Catacutan


There is a legendCallback function:

legendCallback Function function (chart) { }
Function to generate a legend. Receives the chart object to generate a legend from. Default implementation returns an HTML string.

Details can be found here

see this issue for the default legend callback:

legendCallback: function(chart) {      var text = [];      text.push('<ul class="' + chart.id + '-legend">');      for (var i = 0; i < chart.data.datasets.length; i++) {          text.push('<li><span style="background-color:' +                     chart.data.datasets[i].backgroundColor +                     '"></span>');          if (chart.data.datasets[i].label) {              text.push(chart.data.datasets[i].label);          }          text.push('</li>');      }      text.push('</ul>');      return text.join('');  } 
like image 43
Craicerjack Avatar answered Sep 17 '22 20:09

Craicerjack