Skip to content

Commit da64e92

Browse files
authored
Use margin option in the callout check for being shown (#742)
* Use margin option in the callout check for being shown * fixes lint
1 parent 0e68647 commit da64e92

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

src/types/label.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function drawCallout(ctx, element) {
145145
const {pointX, pointY, options} = element;
146146
const callout = options.callout;
147147
const calloutPosition = callout && callout.display && resolveCalloutPosition(element, callout);
148-
if (!calloutPosition || element.inRange(pointX, pointY)) {
148+
if (!calloutPosition || isPointInRange(element, callout, calloutPosition)) {
149149
return;
150150
}
151151

@@ -256,3 +256,20 @@ function getLabelSize({x, y, width, height, options}) {
256256
height: height - padding.top - padding.bottom - options.borderWidth
257257
};
258258
}
259+
260+
function isPointInRange(element, callout, position) {
261+
const {pointX, pointY} = element;
262+
const margin = callout.margin;
263+
let x = pointX;
264+
let y = pointY;
265+
if (position === 'left') {
266+
x += margin;
267+
} else if (position === 'right') {
268+
x -= margin;
269+
} else if (position === 'top') {
270+
y += margin;
271+
} else if (position === 'bottom') {
272+
y -= margin;
273+
}
274+
return element.inRange(x, y);
275+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
module.exports = {
2+
tolerance: 0.0075,
3+
config: {
4+
type: 'scatter',
5+
options: {
6+
scales: {
7+
x: {
8+
min: 0,
9+
max: 10
10+
},
11+
y: {
12+
min: 0,
13+
max: 10
14+
}
15+
},
16+
plugins: {
17+
legend: false,
18+
annotation: {
19+
annotations: {
20+
test1: {
21+
type: 'label',
22+
xValue: 2.5,
23+
yValue: 7.5,
24+
backgroundColor: 'white',
25+
borderColor: 'red',
26+
borderWidth: 1,
27+
content: ['callout is', 'not drawn'],
28+
xAdjust: -10,
29+
yAdjust: 26,
30+
callout: {
31+
display: true,
32+
margin: 5,
33+
}
34+
},
35+
test2: {
36+
type: 'point',
37+
xValue: 2.5,
38+
yValue: 7.5,
39+
radius: 2,
40+
backgroundColor: 'red',
41+
borderColor: 'red'
42+
},
43+
test3: {
44+
type: 'label',
45+
xValue: 2.5,
46+
yValue: 2.5,
47+
backgroundColor: 'white',
48+
borderColor: 'red',
49+
borderWidth: 1,
50+
content: ['callout is', 'not drawn'],
51+
xAdjust: -10,
52+
yAdjust: -26,
53+
callout: {
54+
display: true,
55+
margin: 5,
56+
}
57+
},
58+
test4: {
59+
type: 'point',
60+
xValue: 2.5,
61+
yValue: 2.5,
62+
radius: 2,
63+
backgroundColor: 'red',
64+
borderColor: 'red'
65+
},
66+
test5: {
67+
type: 'label',
68+
xValue: 7.5,
69+
yValue: 2.5,
70+
backgroundColor: 'white',
71+
borderColor: 'red',
72+
borderWidth: 1,
73+
content: ['callout is', 'not drawn'],
74+
xAdjust: -44,
75+
callout: {
76+
display: true,
77+
margin: 5,
78+
}
79+
},
80+
test6: {
81+
type: 'point',
82+
xValue: 7.5,
83+
yValue: 2.5,
84+
radius: 2,
85+
backgroundColor: 'red',
86+
borderColor: 'red'
87+
},
88+
test7: {
89+
type: 'label',
90+
xValue: 7.5,
91+
yValue: 7.5,
92+
backgroundColor: 'white',
93+
borderColor: 'red',
94+
borderWidth: 1,
95+
content: ['callout is', 'not drawn'],
96+
xAdjust: 44,
97+
callout: {
98+
display: true,
99+
margin: 5,
100+
}
101+
},
102+
test8: {
103+
type: 'point',
104+
xValue: 7.5,
105+
yValue: 7.5,
106+
radius: 2,
107+
backgroundColor: 'red',
108+
borderColor: 'red'
109+
}
110+
}
111+
}
112+
}
113+
}
114+
},
115+
options: {
116+
spriteText: true
117+
}
118+
};
29.1 KB
Loading

0 commit comments

Comments
 (0)