question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTML legend example

See original GitHub issue

Hello, I love this plugin to be able to create charts. However, I’ve looked and cannot understand or find a way to use the legendCallback function to create an HTML legend.

Can you provide a link or an example of how to use the legendCallback or chartInstance.generateLegend? I have read the documentation and scoured the issues, but am not familiar on how to customize this code. What does the HTML legend look like?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

32reactions
jcopperfieldcommented, Dec 23, 2017

@sensaetions The default generateLegend() function will generate a <ul class="0-legend"> for the chart, which you can style using the css descriptor [class="0-legend"]. The 0 is the chart.id. In the following example you’ll find a chart with a HTML generated legend, which mimics the default legend. chartjs-html-legend

CSS

[class="0-legend"] {
  list-style: none;
  cursor: pointer;
  padding-left: 0;
}
[class="0-legend"] li {
  display: inline-block;
  padding: 0 5px;
}
[class="0-legend"] li.hidden {
  text-decoration: line-through;
}
[class="0-legend"] li span {
  border-radius: 5px;
  display: inline-block;
  height: 10px;
  margin-right: 10px;
  width: 10px;
}

JavaScript to create HTML legend

var myLegendContainer = document.getElementById("myChartLegend");

// generate HTML legend
myLegendContainer.innerHTML = myChart.generateLegend();

// bind onClick event to all LI-tags of the legend
var legendItems = myLegendContainer.getElementsByTagName('li');
for (var i = 0; i < legendItems.length; i += 1) {
  legendItems[i].addEventListener("click", legendClickCallback, false);
}

JavaScript legend onClick callback

function legendClickCallback(event) {
  event = event || window.event;

  var target = event.target || event.srcElement;
  while (target.nodeName !== 'LI') {
      target = target.parentElement;
  }
  var parent = target.parentElement;
  var chartId = parseInt(parent.classList[0].split("-")[0], 10);
  var chart = Chart.instances[chartId];
  var index = Array.prototype.slice.call(parent.children).indexOf(target);

  chart.legend.options.onClick.call(chart, event, chart.legend.legendItems[index]);
  if (chart.isDatasetVisible(index)) {
    target.classList.remove('hidden');
  } else {
    target.classList.add('hidden');
  }
}

default legendCallback

https://github.com/chartjs/Chart.js/blob/ce1fc28b743d518add5c89653108a287f6aa911d/src/plugins/plugin.legend.js#L67-L80

Edit: modified legendClickCallback to call the chart onClick-event.

1reaction
LeeLenaleeecommented, Jul 17, 2021

@MadalinaCi you can use a custom inline plugin for this: https://jsfiddle.net/Leelenaleee/drk3unhs/4/

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML legend tag - W3Schools
Definition and Usage. The <legend> tag defines a caption for the <fieldset> element. Browser Support. Element.
Read more >
The Field Set Legend element - HTML - MDN Web Docs
The <legend> HTML element represents a caption for the content of its parent <fieldset> . Try it. HTML Demo: <legend>. Reset. HTML CSS...
Read more >
HTML <legend> Tag - GeeksforGeeks
The legend tag is used to define the title for the child contents. The legend elements are the parent element. This tag is...
Read more >
HTML: <legend> tag - TechOnTheNet
The HTML <legend> tag is used to generate a caption for the <fieldset> tag when grouping related items in an HTML form. This...
Read more >
HTML legend Tag - Javatpoint
HTML <legend> tag is used to insert a title or caption to its parent element such as <fieldset>. The <legend> element must be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found