chartjs / chartjs/chartjs-plugin-datalabels

When using with zoom plugin, `clip: true` affect the labels on the edge

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

Description

See this demo: https://codesandbox.io/s/chart-js-data-labels-plugin-clip-issue-mqkrgd?file=/src/App.tsx It use zoom and datalabels

When using with zoom, you have to set `clip: true` on datalabels, otherwise the label will overlap with the yAxis
image
But if did this, the first and the last bar label get cut.
image
Because it is beyond the chart area.
image
The [document](https://chartjs-plugin-datalabels.netlify.app/guide/positioning.html#clipping) tells me the `clip` is calling CanvasRenderingContext2D.clip(), so I then took a look at the [source code](https://github.com/chartjs/chartjs-plugin-datalabels/blob/master/src/label.js#L334-L343) here:
```
if (model.clip) {
area = model.area;
ctx.beginPath();
ctx.rect(
area.left,
area.top,
area.right - area.left,
area.bottom - area.top);
ctx.clip();
}
```
I guess maybe we can allow a paddingOffset to slightly extend the rect area, to let customer to adjust? like:
```
var paddingOffset = modal.paddingOffset;
if (model.clip) {
area = model.area;
ctx.beginPath();
ctx.rect(
area.left - paddingOffset,
area.top - paddingOffset,
area.right + paddingOffset * 2 - area.left,
area.bottom + paddingOffset * 2 - area.top);
ctx.clip();
}
```

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.