简体   繁体   English

Chart.js 图例填充-左

[英]Chart.js Legend Padding-Left

I have been playing with the solution found here from user Frosty Z (chart.js 2.5+) to try and apply padding to my legend.我一直在使用用户 Frosty Z(chart.js 2.5+) 在此处找到的解决方案,尝试将填充应用于我的图例。 The solution works great if the legend is above the chart but trying to add padding in between the chart and legend when the legend is on the left right side is proving difficult.如果图例位于图表上方,则该解决方案效果很好,但是当图例位于左侧右侧时,尝试在图表和图例之间添加填充被证明是困难的。

    plugins: [{
        beforeInit: function(pdChart, options) {
            pdChart.legend.afterFit = function() {
                this.width = this.width + 50;
            };
        }
    }],

The picture below depicts my legend as of right now.下图描绘了我现在的传奇。 Again I am hoping to add space between the legend and the chart.我再次希望在图例和图表之间添加空间。

在此处输入图像描述

You can generate a custom HTML legend using legendCallback together with some CSS .您可以使用legendCallback和一些CSS生成自定义HTML图例

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>';
}

Note that the default legend must be disabled through the option legend.display: false .请注意,必须通过选项legend.display: false禁用默认图例。

To make this still behave the same as standard Chart.js charts, the function onLegendClicked is invoked when a mouse click occurs on a legend label.为了使其与标准 Chart.js 图表的行为仍然相同,当鼠标单击图例onLegendClicked时,将调用 function onLegendClicked。 This function toggles the hidden state of individual datasets and changes label text style between normal and strike-through.此 function 切换各个数据集的隐藏 state 并在正常和删除线之间更改 label 文本样式。

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

The padding of the legend is defined through CSS as follows:图例的padding通过CSS定义如下:

#legend {
  padding-left: 20px;
  padding-top: 10px;
}

Please take a look at below runnable code sample and see how it works for your particular case.请查看下面的可运行代码示例,看看它如何适用于您的特定情况。

 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, ['A', 'B', 'C', 'D']: datasets: [ { label, 'Dataset 1': data, [205, 275, 359, 329]: borderColor, "#fd7730": backgroundColor, "#fd7730": fill, false }: { label, 'Dataset 2': data, [262, 302, 290, 180]: borderColor, "#ffd35c": backgroundColor, "#ffd35c": fill, false }: { label, 'Dataset 3': data, [359, 329, 262, 302]: borderColor, "#3fc6f3": backgroundColor, "#3fc6f3": fill, false }: { label, 'Dataset 4': data, [105, 175, 259, 129]: borderColor, "#28a745": backgroundColor, "#28a745": fill, false }: { label, 'Dataset 5': data, [189, 222, 201, 158]: borderColor, "#488cf2": backgroundColor, "#488cf2": fill, false } ] }: 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: { yAxes: [{ ticks: { beginAtZero, true: stepSize; 100 } }] } } }). document.getElementById("legend").innerHTML = chart;generateLegend();
 #chart-wrapper { display: flex; width: 60%; } ul { list-style-type: none; margin: 0; padding: 0; } #legend { padding-left: 20px; padding-top: 10px; } #legend li { cursor: pointer; display: flex; padding: 0 10px 5px 0; } #legend li span { white-space: nowrap; 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"> <canvas id="myChart" height="100"></canvas> <div id="legend"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM