score:1

Accepted answer

you can generate custom html legend using legendcallback together with some css.

legendcallback: chart => {
  let html = '<ul>';
  chart.data.datasets.foreach((ds, i) => {
    html += '<li>' +
      '<span style="width: 36px; height: 14px; background-color:' + ds.backgroundcolor + '; border:' + ds.borderwidth + 'px solid ' + ds.bordercolor + '" onclick="onlegendclicked(event, \'' + i + '\')">&nbsp;</span>' +
      '<span id="legend-label-' + i + '" onclick="onlegendclicked(event, \'' + i + '\')">' +
      ds.label + '</span>' +
      '</li>';
  });
  return html + '</ul>';
},

to make this behave the same as standard chart.js charts, the function onlegendclicked is invoked when a mouse click occurs on a legend label. this function toggles the hidden state of individual datasets and changes label text style between normal and strike-through.

function onlegendclicked(e, i) {
  const hidden = !chart.data.datasets[i].hidden;
  chart.data.datasets[i].hidden = hidden;
  const legendlabelspan = document.getelementbyid("legend-label-" + i);
  legendlabelspan.style.textdecoration = hidden ? 'line-through' : '';
  chart.update();
};

please take a look at your amended code and see how it works.

const linechartcolors = ["#000000", "#fd7730", "#ffd35c", "#3fc6f3", "#28a745", "#488cf2", "#4755d3", "#9768c9", "#f2748d", "#f287e7", '#992499', '#6bd69e'];
const legendlabels = ['aug', 'sep', 'oct', 'nov', 'dec', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul'];
const datapoints = [
  [205, 275, 359, 329, 262, 302, 290, 323, 279, 238, 307, 245],
  [16, 13, 14, 11, 23, 11, 24, 23, 15, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0],
  [169, 194, 261, 193, 151, 158, 128, 143, 163, 173, 139, 208],
  [8, 5, 8, 2, 4, 4, 0, 0, 0, 0, 0, 0],
  [0, 0, 19, 36, 7, 35, 27, 30, 13, 0, 0, 0],
  [0, 47, 30, 54, 59, 48, 41, 38, 65, 24, 44, 37],
  [12, 16, 27, 33, 18, 46, 70, 89, 23, 41, 71, 0]
];
var suppliers = ["total", "starkey", "resound", "widex", "rexton", "unitron", "phonak", "signia"];

function onlegendclicked(e, i) {
  const hidden = !chart.data.datasets[i].hidden;
  chart.data.datasets[i].hidden = hidden;
  const legendlabelspan = document.getelementbyid("legend-label-" + i);
  legendlabelspan.style.textdecoration = hidden ? 'line-through' : '';
  chart.update();
};

const chart = new chart('mychart', {
  type: 'line',
  data: {
    labels: legendlabels,
    datasets: datapoints.map((e, i) => ({
      backgroundcolor: linechartcolors[i],
      bordercolor: linechartcolors[i],
      fill: false,
      data: e,
      label: suppliers[i],
      linetension: 0.2,
    }))
  },
  options: {
    legend: {
      display: false
    },
    legendcallback: chart => {
      let html = '<ul>';
      chart.data.datasets.foreach((ds, i) => {
        html += '<li>' +
          '<span style="width: 36px; height: 14px; background-color:' + ds.backgroundcolor + '; border:' + ds.borderwidth + 'px solid ' + ds.bordercolor + '" onclick="onlegendclicked(event, \'' + i + '\')">&nbsp;</span>' +
          '<span id="legend-label-' + i + '" onclick="onlegendclicked(event, \'' + i + '\')">' +
          ds.label + '</span>' +
          '</li>';
      });
      return html + '</ul>';
    },
    scales: {
      xaxes: [{
        scalelabel: {
          display: true,
          labelstring: 'month'
        }
      }],
      yaxes: [{
        ticks: {
          beginatzero: true
        },
        scalelabel: {
          display: true,
          labelstring: "units sold"
        }
      }]
    },
    plugins: {
      datalabels: {
        display: false
      }
    }
  }
});
document.getelementbyid("legend").innerhtml = chart.generatelegend();
#chart-wrapper {
  display: flex;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

#legend li {
  cursor: pointer;
  display: flex;
  padding: 0 10px 5px 0;
}

#legend li span {
  padding-left: 8px;
  font-family: arial, helvetica, sans-serif;
  font-size: 12px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.min.js"></script>
<div id="chart-wrapper">
  <div id="legend"></div>
  <canvas id="mychart" height="75"></canvas>
</div>


Related Query

More Query from same tag