Skip to content

Commit eee842c

Browse files
authored
Remove clipping for label to box annotation size (#764)
1 parent c7435ea commit eee842c

File tree

6 files changed

+10
-31
lines changed

6 files changed

+10
-31
lines changed

docs/guide/migrationV2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ leave: function({element}) {
4848
},
4949
```
5050

51+
`chartjs-plugin-annotation` plugin version 2 removes the clipping of the label to the box annotation size.
52+
5153
`chartjs-plugin-annotation` plugin version 2 hides the following methods in the `line` annotation element because they should be used only internally:
5254

5355
* `intersects`

src/annotation.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,37 +141,34 @@ export default {
141141
};
142142

143143
function draw(chart, caller, clip) {
144-
const {ctx, canvas, chartArea} = chart;
144+
const {ctx, chartArea} = chart;
145145
const {visibleElements} = chartStates.get(chart);
146-
let area = {left: 0, top: 0, width: canvas.width, height: canvas.height};
147146

148147
if (clip) {
149148
clipArea(ctx, chartArea);
150-
area = chartArea;
151149
}
152150

153-
const drawableElements = getDrawableElements(visibleElements, caller, area).sort((a, b) => a.element.options.z - b.element.options.z);
151+
const drawableElements = getDrawableElements(visibleElements, caller).sort((a, b) => a.options.z - b.options.z);
154152

155-
for (const item of drawableElements) {
156-
item.element.draw(chart.ctx, item.area);
153+
for (const element of drawableElements) {
154+
element.draw(chart.ctx, chartArea);
157155
}
158156

159157
if (clip) {
160158
unclipArea(ctx);
161159
}
162160
}
163161

164-
function getDrawableElements(elements, caller, area) {
162+
function getDrawableElements(elements, caller) {
165163
const drawableElements = [];
166164
for (const el of elements) {
167165
if (el.options.drawTime === caller) {
168-
drawableElements.push({element: el, area});
166+
drawableElements.push(el);
169167
}
170168
if (el.elements && el.elements.length) {
171-
const box = 'getBoundingBox' in el ? el.getBoundingBox() : area;
172169
for (const sub of el.elements) {
173170
if (sub.options.display && sub.options.drawTime === caller) {
174-
drawableElements.push({element: sub, area: box});
171+
drawableElements.push(sub);
175172
}
176173
}
177174
}

src/types/box.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ export default class BoxAnnotation extends Element {
2020
ctx.restore();
2121
}
2222

23-
getBoundingBox() {
24-
const {x, y, width, height} = this.getProps(['x', 'y', 'width', 'height']);
25-
const label = this.options.label;
26-
const padding = toPadding(label.padding);
27-
const borderWidth = this.options.borderWidth;
28-
const halfBorder = borderWidth / 2;
29-
return {
30-
left: x + halfBorder + padding.left,
31-
top: y + halfBorder + padding.top,
32-
width: width - borderWidth - padding.width,
33-
height: height - borderWidth - padding.height
34-
};
35-
}
36-
3723
get label() {
3824
return this.elements && this.elements[0];
3925
}

src/types/label.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class LabelAnnotation extends Element {
1515
return getElementCenterPoint(this, useFinalPosition);
1616
}
1717

18-
draw(ctx, area) {
18+
draw(ctx) {
1919
const options = this.options;
2020
if (!options.display || !options.content) {
2121
return;
@@ -24,12 +24,6 @@ export default class LabelAnnotation extends Element {
2424
translate(ctx, this.getCenterPoint(), this.rotation);
2525
drawCallout(ctx, this);
2626
drawBox(ctx, this, options);
27-
if (area) {
28-
// clip
29-
ctx.beginPath();
30-
ctx.rect(area.left, area.top, area.width, area.height);
31-
ctx.clip();
32-
}
3327
drawLabel(ctx, getLabelSize(this), options);
3428
ctx.restore();
3529
}

test/fixtures/box/labelMultiline.png

3.19 KB
Loading

test/fixtures/box/labelPadding.png

446 Bytes
Loading

0 commit comments

Comments
 (0)