chartjs / chartjs/chartjs-plugin-datalabels

Data label alignment issue

Open
#305 3 comments 1 reaction 0 assignees View on GitHub
needs test case support
Dominant language
JavaScript
Stars
920
Forks
507
PR merge metrics
No merged PRs in 30d

Description

Hi,
I have an alignment issue in the data label of the stacked bar chart. When toggling legends, data labels getting overlap with the bar and also have alignment issues in the case of negative values.
![Screenshot 2022-01-19 at 14-53-41 “What If” Opportunity (TBA)](https://user-images.githubusercontent.com/60467617/150102254-bce8942b-a51b-4c6c-997d-6af71571cb71.png)

Here is my code:
plugins: {


datalabels: {

anchor:'end',
align: 'top',
color: '#737373',
font: {
weight: 'bold'
},
offset: (value,ctx)=> {

return '10'
},
formatter: (value, ctx) => {

let datasets = ctx.chart.data.datasets.filter(
(ds, datasetIndex) => ctx.chart.isDatasetVisible(datasetIndex)
)

if (ctx.datasetIndex === datasets.length - 1 ) {

let sum = 0;
datasets.map(dataset => {
sum += dataset.data[ctx.dataIndex];
});
if(sum >0){
return '$'+Math.round(sum);
}else{
return '-$'+Math.round(Math.abs(sum));
}

}
else {
return '';
}
}
},

}
},
Any help would be greatly appreciated!
Thanks!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.