Skip to content

Commit d2c18fc

Browse files
authored
Fix the usage of the backgroundColor callback on label (#671)
1 parent 0f357a8 commit d2c18fc

File tree

5 files changed

+79
-21
lines changed

5 files changed

+79
-21
lines changed

src/types/label.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {drawBox, drawLabel, measureLabelSize, getChartPoint, getRectCenterPoint, toPosition, setBorderStyle, getSize, inBoxRange, isBoundToPoint, getChartRect, getRelativePosition} from '../helpers';
2-
import {color, toPadding} from 'chart.js/helpers';
2+
import {toPadding} from 'chart.js/helpers';
33
import {Element} from 'chart.js';
44

55
export default class LabelAnnotation extends Element {
@@ -18,9 +18,7 @@ export default class LabelAnnotation extends Element {
1818
}
1919
const {labelX, labelY, labelWidth, labelHeight, options} = this;
2020
drawCallout(ctx, this);
21-
if (this.boxVisible) {
22-
drawBox(ctx, this, options);
23-
}
21+
drawBox(ctx, this, options);
2422
drawLabel(ctx, {x: labelX, y: labelY, width: labelWidth, height: labelHeight}, options);
2523
}
2624

@@ -30,16 +28,13 @@ export default class LabelAnnotation extends Element {
3028
const padding = toPadding(options.padding);
3129
const labelSize = measureLabelSize(chart.ctx, options);
3230
const boxSize = measureRect(point, labelSize, options, padding);
33-
const bgColor = color(options.backgroundColor);
34-
const boxVisible = options.borderWidth > 0 || (bgColor && bgColor.valid && bgColor.rgb.a > 0);
35-
31+
const hBorderWidth = options.borderWidth / 2;
3632
const properties = {
37-
boxVisible,
3833
pointX: point.x,
3934
pointY: point.y,
4035
...boxSize,
41-
labelX: boxSize.x + padding.left + (options.borderWidth / 2),
42-
labelY: boxSize.y + padding.top + (options.borderWidth / 2),
36+
labelX: boxSize.x + padding.left + hBorderWidth,
37+
labelY: boxSize.y + padding.top + hBorderWidth,
4338
labelWidth: labelSize.width,
4439
labelHeight: labelSize.height
4540
};

test/fixtures/label/boxNotVisible.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
options: {
66
scales: {
77
x: {
8+
display: false,
89
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
910
},
1011
y: {
@@ -18,25 +19,25 @@ module.exports = {
1819
annotations: {
1920
text1: {
2021
type: 'label',
21-
xValue: 'January',
22-
yValue: 20,
22+
xValue: 'April',
23+
yValue: 4,
24+
content: 'no borderWidth, no backgroundColor'
25+
},
26+
text2: {
27+
type: 'label',
28+
xValue: 'April',
29+
yValue: 12,
2330
backgroundColor: 'missing',
2431
borderWidth: 0,
25-
content: 'This is my text',
26-
position: {
27-
x: 'end'
28-
}
32+
content: ['borderWidth: 0, wrong backgroundColor', 'use default fill style: #000 (black)']
2933
},
3034
text3: {
3135
type: 'label',
32-
xValue: 'May',
36+
xValue: 'April',
3337
yValue: 20,
3438
backgroundColor: 'rgba(250,250,250,0)',
3539
borderWidth: 0,
36-
content: 'This is my text',
37-
position: {
38-
x: 'start'
39-
}
40+
content: 'borderWidth: 0, backgroundColor: rgba(250,250,250,0)'
4041
}
4142
}
4243
}
-1.37 KB
Loading
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function gradient({chart: {ctx}, element}) {
2+
const g = ctx.createLinearGradient(element.x, element.y, element.x + element.width, element.y + element.height);
3+
g.addColorStop(0, 'red');
4+
g.addColorStop(1, 'black');
5+
return g;
6+
}
7+
8+
module.exports = {
9+
tolerance: 0.0060,
10+
config: {
11+
type: 'bar',
12+
options: {
13+
scales: {
14+
x: {
15+
display: false,
16+
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
17+
},
18+
y: {
19+
display: false,
20+
min: 0,
21+
max: 25
22+
}
23+
},
24+
plugins: {
25+
annotation: {
26+
annotations: {
27+
label1: {
28+
type: 'label',
29+
xMin: 1.5,
30+
xMax: 3.5,
31+
yMin: 5,
32+
yMax: 10,
33+
content: 'Label on gradient',
34+
color: 'white',
35+
backgroundColor: gradient,
36+
},
37+
label2: {
38+
type: 'label',
39+
xMin: 'July',
40+
xMax: 'May',
41+
yMin: 11,
42+
yMax: 15,
43+
content: 'Label on gradient',
44+
color: 'white',
45+
backgroundColor: gradient
46+
},
47+
label3: {
48+
type: 'label',
49+
xMin: 0,
50+
xMax: 'May',
51+
yMin: 20,
52+
yMax: 16,
53+
content: 'Label on gradient',
54+
color: 'white',
55+
backgroundColor: gradient
56+
}
57+
}
58+
}
59+
}
60+
}
61+
}
62+
};
14 KB
Loading

0 commit comments

Comments
 (0)