Accepted answer

you need to edit the generatelabels property in your options :

options: {
    legend: {
        labels: {
            generatelabels: function(chart) {
                 // here

since it is quite a mess to create on your own a great template. i suggest using the same function as in the source code and then edit what is needed.

here are a small jsfiddle, where you can see how it works (edited lines - from 38 - are commented), and its result :

enter image description here


i wanted to let the user select from 100+ data sets, but rather than adding/removing them from my chart i decided to set the showline: false on any dataset that i want hidden. unfortunately the default legend would show all 100+. so in my solution i generate the legend manually, filtering out any dataset that has showline: false.

your settings will have this:

legend: {
  labels: {
    generatelabels: (a) => {

and you'll generate your own labels with a helper function:

function updatealllabels() {
  const mynewlabels = []; => {
    if (element.showline) {
  }); = mynewlabels;

and you'll generate the label with another function:

function generatelabel(data) {
  return {
    fillstyle: data.bordercolor,
    linewidth: 1,
    strokestyle: data.bordercolor,
    text: data.countyname, // i attach countryname to my datasets for convenience

now just don't forget to call the function whenever updating your chart:


happy graphing!


following on from tektiv's answer, i've modified it for es6 which my linter requires;

options: {
  legend: {
    labels: {
      generatelabels: (chart) => {
        const { data } = chart;
        if (data.labels.length && data.datasets.length) {
          return, i) => {
            const meta = chart.getdatasetmeta(0);
            const ds = data.datasets[0];
            const arc =[i];
            const custom = (arc && arc.custom) || {};
            const { getvalueatindexordefault } = chart.helpers;
            const arcopts = chart.options.elements.arc;
            const fill = custom.backgroundcolor ? custom.backgroundcolor : getvalueatindexordefault(ds.backgroundcolor, i, arcopts.backgroundcolor);
            const stroke = custom.bordercolor ? custom.bordercolor : getvalueatindexordefault(ds.bordercolor, i, arcopts.bordercolor);
            const bw = custom.borderwidth ? custom.borderwidth : getvalueatindexordefault(ds.borderwidth, i, arcopts.borderwidth);
            const value =[arc._datasetindex].data[arc._index];

            return {
              text: `${label}: ${value}`,
              fillstyle: fill,
              strokestyle: stroke,
              linewidth: bw,
              hidden: number.isnan([i]) ||[i].hidden,
              index: i,
        return [];


maybe this is a hacky solution, but for me seems simpler.

the filter parameter

chartjs legend options have a filter parameter. this is a function that is called for each legend item, and that returns true/false whether you want to show this item in the legend or not.

filter has 2 arguments:

  • legenditem : the legend item to show/omit. its properties are described here
  • data : the data object passed to the chart.

the hack

since js passes objects by reference, and filter is called for each legend item, then you can mutate the legenditem object to show the text that you want.

legend : {
  labels: {
    filter: (legenditem, data) => {
      // first, retrieve the data corresponding to that label
      const label = legenditem.text
      const labelindex = _.findindex(data.labels, (labelname) => labelname === label) // i'm using lodash here
      const qtd = data.datasets[0].data[labelindex]
      // second, mutate the legenditem to include the new text
      legenditem.text = `${legenditem.text} : ${qtd}`
      // third, the filter method expects a bool, so return true to show the modified legenditem in the legend
      return true

Related Query

More Query from same tag