i know this question is old. but this might be useful for someone who is having the problem with legend. in addition to the answer given by zanedarken, i modified the chart.js file to show the legend in my pie chart. i changed the legendtemplate(which is declared many times for every chart type) just above these lines :

      //passing in a name registers this chart in the chart namespace
      name: "doughnut",
      //providing a defaults will also register the deafults in the chart namespace
      defaults: defaultconfig,

my legendtemplate is changed from

legendtemplate : "
<ul class=\ "<%=name.tolowercase()%>-legend\">
  <% for (var i=0; i<datasets.length; i++){%>
    <li><span style=\ "background-color:<%=datasets[i].strokecolor%>\"></span>


legendtemplate: "
<ul class=\ "<%=name.tolowercase()%>-legend\">
  <% for (var i=0; i<segments.length; i++){%>
    <li><span style=\ "-moz-border-radius:7px 7px 7px 7px; border-radius:7px 7px 7px 7px; margin-right:10px;width:15px;height:15px;display:inline-block;background-color:<%=segments[i].fillcolor%>\"> </span>
        <%=s egments[i].label%>


for line chart, i use the following codes.

first create custom style

    position: relative;
    width: 20px;
    height: 20px;
    border-radius: 3px;

then add this on your line options

var lineoptions = {
            legendtemplate : '<table>'
                            +'<% for (var i=0; i<datasets.length; i++) { %>'
                            +'<tr><td><div class=\"boxx\" style=\"background-color:<%=datasets[i].fillcolor %>\"></div></td>'
                            +'<% if (datasets[i].label) { %><td><%= datasets[i].label %></td><% } %></tr><tr height="5"></tr>'
                            +'<% } %>'
            multitooltiptemplate: "<%= datasetlabel %> - <%= value %>"

var ctx = document.getelementbyid("linechart").getcontext("2d");
var mynewchart = new chart(ctx).line(linedata, lineoptions);
document.getelementbyid('legenddiv').innerhtml = mynewchart.generatelegend();

don't forget to add

<div id="legenddiv"></div>

on your html where do you want to place your legend. that's it!


strangely, i didn't find anything about legends and labels in the chart.js documentation. it seems like you can't do it with chart.js alone.

i used which is extremely light, to generate the legends.


  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)

    <canvas id="chartdiv" height="400" width="600"></canvas>
    <div id="legenddiv"></div>

   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();


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++) { %>'
                    +'<span style=\"background-color:<%=datasets[i].linecolor%>\"></span>'
                    +'<% if (datasets[i].label) { %><%= datasets[i].label %><% } %>'
                +'<% } %>'

  //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

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

Related Query

More Query from same tag