Maybe you can check their documentation about Positioning and particulary the latest 3 sections.
ChartJS plugin datalabels - How to avoid labels to be out of the canvas
Hello,
I'm using ChartJS with the datalabels plugin.
https://chartjs-plugin-datalabels.netlify.app/
For the doughnut, I have chosen to display the labels around the circle. But if the labels are too long, a part is not displayed because out of the chart canvas.
How to avoid this ?
Thanks for your help.
V
@RemiM Yes already done, but the problem is that positioning is not sufficient.
What would be great is the possibility to wrap the label inside a div with fixed dimensions and force the label to display on several lines if needed.
@vincent15000 Couple ideas:
First, for multiline, you can use their built-in formatting option.
Moreover, you can probably come up with a working solution using Events and Options.
I found out their samples is a great place to start.
@RemiM I know these pages, ... well ... not sure that it helps me.
For the multiline labels, I think that I need to define the number or lines manually, however I'd like this to be done automatically. I will try this.
UPDATE => I just tried this multiline possibility, but it doesn't satisfy me.
I explain : with a doughnut, according to the position of the quarter, the legend can be on the left or on the right of the doughut, so just at the limit of the canvas. In these cases, even if I use multiline, the legend is troncated.
Perhaps the solution for these specific cases should be to move the legend just a little to the bottom or to the top to have more place.
I've made a full example that you can copy paste in their sample doughnut to check if this is what you are looking for, and play around to make the necessary changes:
What I've done:
- multi line text base on its length.
- anchor and align based on text length (center or end).
- the text is never truncate, it's always in the frame of the chart.
config:
{
type: 'doughnut',
dispay: true,
clamp: true,
data: {
labels: labels,
datasets: [{
data: Utils.numbers({
count: DATA_COUNT,
min: 0,
max: 3
}),
backgroundColor: Utils.colors({
color: Utils.color(0),
count: DATA_COUNT
}),
}]
},
options: {
plugins: {
datalabels: {
backgroundColor: function(context) {
return context.dataset.backgroundColor;
},
borderWidth: 2,
borderRadius: 5,
padding: 10,
borderColor: 'white',
color: 'white',
// offset: 10, // Moves labels outward
clip: false, // Prevents labels from being cut off
align: function(ctx) {
return ctx.chart.data.labels[ctx.dataIndex].length > 12 ? 'center' : 'end';
},
anchor: function(ctx) {
return ctx.chart.data.labels[ctx.dataIndex].length > 12 ? 'center' : 'end';
},
formatter: (value, ctx) => {
let label = ctx.chart.data.labels[ctx.dataIndex];
return label.split(' ').reduce((acc, word) => {
if (acc.length === 0 || acc[acc.length - 1].length + word.length > 12) {
acc.push(word);
} else {
acc[acc.length - 1] += ' ' + word;
}
return acc;
}, []).join('\n'); // Breaks text into multiple lines
}
}
},
// Core options
layout: {
padding: 25
},
}
}
setup:
var DATA_COUNT = 3;
var labels = ['Short Label',
'A Much Longer Label That Would Normally Be Cut Off',
'Another Example of a Really Long Label That Needs Wrapping'];
Utils.srand(4);
for (var i = 0; i < DATA_COUNT; ++i) {
labels.push('' + i);
}
@RemiM Thank you, I also did that yesterday to check how it could work, but it doesn't satisfy me.
As I said, I have a very little space for the quarters on the left and the right of the doughnut, so even 3 or 4 characters are to much. What would be nice is to move the legend a little up or down around the doughtnut.
@vincent15000 In that case, why you just don't put the anchor in the center no matter what—while keeping the logic for the formatter—, or eventually simply add some padding to your frame?
Technically that part will allow you to see your label on the far left and right of your doughnut:
layout: {
padding: 25
},
Please or to participate in this conversation.