chartjs / chartjs/chartjs-plugin-datalabels

Horizontal stacked barchart with negative values label position is off

Open
#407 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
920
Forks
507
PR merge metrics
No merged PRs in 30d

Description

Screenshot 2024-01-16 at 10 51 38
The sum of the bar is shown wrong when there are negative values it the stack and last stack value is 0 (which is so it is not shown in bar on the image)

```
datalabels: {
formatter: (value, ctx) => {
const datasets = ctx.chart.data.datasets;
if (datasets.indexOf(ctx.dataset) === datasets.length - 1) {
let sum = 0;
datasets.map((dataset) => {
sum += dataset.data[ctx.dataIndex] as number;
});
return sum.toFixed(2);
} else {
return '';
}
},
anchor: 'end',
align: 'end',
},
```

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.