Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can labels/legends be added for all chart types in chart.js (chartjs.org)?

The documentation for chart.js mentions "legend templates" but gives no resources or examples of such legends. How can these be displayed?

like image 399
Nalin Avatar asked Dec 05 '13 10:12

Nalin


People also ask

How do I add a legend in chartJS?

js (3.6. 0), you can control the Legend display with the following code: const options = { plugins: { ... legend: { position: "right", // by default it's top }, ... }, };

How do I add labels to my legend?

In the Chart editor, under the 'Setup' tab, click the 'Add Label' box, under the 'Label' section. Select the cell range that includes your chart data. You should see your selected data displayed as labels in your chart.

What is legend position in chart?

The chart legend displays data about the datasets that are appearing on the chart.


2 Answers

You can include a legend template in the chart options:

//legendTemplate takes a template as a string, you can populate the template with values from your dataset  var options = {   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>'   }    //don't forget to pass options in when creating new Chart   var lineChart = new Chart(element).Line(data, options);    //then you just need to generate the legend   var legend = lineChart.generateLegend();    //and append it to your page somewhere   $('#chart').append(legend); 

You'll also need to add some basic css to get it looking ok.

like image 94
omgo Avatar answered Sep 29 '22 06:09

omgo


  1. The legend is part of the default options of the ChartJs library. So you do not need to explicitly add it as an option.

  2. The library generates the HTML. It is merely a matter of adding that to the your page. For example, add it to the innerHTML of a given DIV. (Edit the default options if you are editing the colors, etc)


<div>     <canvas id="chartDiv" height="400" width="600"></canvas>     <div id="legendDiv"></div> </div>  <script>    var data = {         labels: ["January", "February", "March", "April", "May", "June", "July"],         datasets: [             {                 label: "The Flash's Speed",                 fillColor: "rgba(220,220,220,0.2)",                 strokeColor: "rgba(220,220,220,1)",                 pointColor: "rgba(220,220,220,1)",                 pointStrokeColor: "#fff",                 pointHighlightFill: "#fff",                 pointHighlightStroke: "rgba(220,220,220,1)",                 data: [65, 59, 80, 81, 56, 55, 40]             },             {                 label: "Superman's Speed",                 fillColor: "rgba(151,187,205,0.2)",                 strokeColor: "rgba(151,187,205,1)",                 pointColor: "rgba(151,187,205,1)",                 pointStrokeColor: "#fff",                 pointHighlightFill: "#fff",                 pointHighlightStroke: "rgba(151,187,205,1)",                 data: [28, 48, 40, 19, 86, 27, 90]             }         ]     };      var myLineChart = new Chart(document.getElementById("chartDiv").getContext("2d")).Line(data);     document.getElementById("legendDiv").innerHTML = myLineChart.generateLegend(); </script> 
like image 37
ZaneDarken Avatar answered Sep 29 '22 06:09

ZaneDarken