Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

charts-chartjs-legend.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. (() => {
  2. const getOrCreateLegendList = (chart, id) => {
  3. const legendContainer = document.getElementById(id);
  4. let listContainer = legendContainer.querySelector('ul');
  5. if (!listContainer) {
  6. listContainer = document.createElement('ul');
  7. listContainer.classList.add('custom-legend-ul');
  8. legendContainer.appendChild(listContainer);
  9. }
  10. return listContainer;
  11. };
  12. const htmlLegendPlugin = {
  13. id: 'htmlLegend',
  14. afterUpdate(chart, args, options) {
  15. const ul = getOrCreateLegendList(chart, options.containerID);
  16. while (ul.firstChild) {
  17. ul.firstChild.remove();
  18. }
  19. const items = chart.options.plugins.legend.labels.generateLabels(chart);
  20. items.forEach(item => {
  21. const li = document.createElement('li');
  22. li.classList.add('custom-legend-li');
  23. li.onclick = () => {
  24. chart.setDatasetVisibility(item.datasetIndex, !chart.isDatasetVisible(item.datasetIndex));
  25. chart.update();
  26. };
  27. const boxSpan = document.createElement('span');
  28. boxSpan.classList.add('legend-box');
  29. boxSpan.style.backgroundColor = item.fillStyle;
  30. const textContainer = document.createElement('span');
  31. textContainer.textContent = item.text;
  32. textContainer.style.textDecoration = item.hidden ? 'line-through' : '';
  33. li.appendChild(boxSpan);
  34. li.appendChild(textContainer);
  35. ul.appendChild(li);
  36. });
  37. }
  38. };
  39. window.LegendUtils = {
  40. getOrCreateLegendList,
  41. htmlLegendPlugin
  42. };
  43. })();