简体   繁体   English

更改图表 js 中的图例文本导致问题

[英]Changing legend text in chart js causing issues

I'm trying to shorten my chart legends, so I'm using the beforeRender plugin to change the text of the legend.我正在尝试缩短图表图例,因此我使用beforeRender插件来更改图例的文本。 However, once the chart is drawn, it seems to keep the original label size (see below snippet, legends are offset to the left instead of in the centre and if you click on orange, it toggles one of the earlier items).但是,一旦绘制图表,它似乎会保持原始标签大小(参见下面的片段,图例向左偏移而不是在中心,如果单击橙色,它会切换较早的项目之一)。

The same happens if I try in beforeDraw and if I use beforeInit or beforeLayout , the legends don't seem to have been created yet.如果我尝试使用beforeDraw并且如果我使用beforeInitbeforeLayout发生同样的情况,图例似乎还没有创建。

Is there any way to get the chart to redraw the legends with the new text?有没有办法让图表用新文本重绘图例? Or change the legend text so that it is rendered properly?或者更改图例文本以使其正确呈现?

 var ctx = document.getElementById("canvas").getContext("2d"); window.myBar = new Chart(ctx, { type: 'pie', data: { labels: ['red - some other text that appears after', 'blue - some other text that appears after', 'green - some other text that appears after', 'orange - some other text that appears after'], datasets: [{ data: [4, 2, 10, 3], backgroundColor: ['red', 'blue', 'green', 'orange'], }], }, options: { responsive: true, }, plugins: [{ beforeRender: chart => { chart.legend.legendItems.forEach(label => { const labelParts = label.text.split(' - '); const newLabel = label; newLabel.text = labelParts[0]; return newLabel; }); }, }], });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.js"></script> <div style="width: 100%"> <canvas id="canvas"></canvas> </div>

A better approach would be to use a custom generateLabels function like so:更好的方法是使用自定义的generateLabels函数,如下所示:

 var ctx = document.getElementById("canvas").getContext("2d"); window.myBar = new Chart(ctx, { type: 'pie', data: { labels: ['red - some other text that appears after', 'blue - some other text that appears after', 'green - some other text that appears after', 'orange - some other text that appears after'], datasets: [{ data: [4, 2, 10, 3], backgroundColor: ['red', 'blue', 'green', 'orange'], }], }, options: { responsive: true, legend: { labels: { generateLabels: function(chart) { const data = chart.data; if (data.labels.length && data.datasets.length) { return data.labels.map(function(label, i) { const meta = chart.getDatasetMeta(0); const style = meta.controller.getStyle(i); return { text: label.split(' - ')[0], fillStyle: style.backgroundColor, strokeStyle: style.borderColor, lineWidth: style.borderWidth, hidden: isNaN(data.datasets[0].data[i]) || meta.data[i].hidden, // Extra data used for toggling the correct item index: i }; }); } return []; } } } }, });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.js"></script> <div style="width: 100%"> <canvas id="canvas"></canvas> </div>

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

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