Skip to content

Use margin option in the callout check for being shown #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/types/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function drawCallout(ctx, element) {
const {pointX, pointY, options} = element;
const callout = options.callout;
const calloutPosition = callout && callout.display && resolveCalloutPosition(element, callout);
if (!calloutPosition || element.inRange(pointX, pointY)) {
if (!calloutPosition || isPointInRange(element, callout, calloutPosition)) {
return;
}

Expand Down Expand Up @@ -256,3 +256,20 @@ function getLabelSize({x, y, width, height, options}) {
height: height - padding.top - padding.bottom - options.borderWidth
};
}

function isPointInRange(element, callout, position) {
const {pointX, pointY} = element;
const margin = callout.margin;
let x = pointX;
let y = pointY;
if (position === 'left') {
x += margin;
} else if (position === 'right') {
x -= margin;
} else if (position === 'top') {
y += margin;
} else if (position === 'bottom') {
y -= margin;
}
return element.inRange(x, y);
}
118 changes: 118 additions & 0 deletions test/fixtures/label/calloutNoShown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
module.exports = {
tolerance: 0.0075,
config: {
type: 'scatter',
options: {
scales: {
x: {
min: 0,
max: 10
},
y: {
min: 0,
max: 10
}
},
plugins: {
legend: false,
annotation: {
annotations: {
test1: {
type: 'label',
xValue: 2.5,
yValue: 7.5,
backgroundColor: 'white',
borderColor: 'red',
borderWidth: 1,
content: ['callout is', 'not drawn'],
xAdjust: -10,
yAdjust: 26,
callout: {
display: true,
margin: 5,
}
},
test2: {
type: 'point',
xValue: 2.5,
yValue: 7.5,
radius: 2,
backgroundColor: 'red',
borderColor: 'red'
},
test3: {
type: 'label',
xValue: 2.5,
yValue: 2.5,
backgroundColor: 'white',
borderColor: 'red',
borderWidth: 1,
content: ['callout is', 'not drawn'],
xAdjust: -10,
yAdjust: -26,
callout: {
display: true,
margin: 5,
}
},
test4: {
type: 'point',
xValue: 2.5,
yValue: 2.5,
radius: 2,
backgroundColor: 'red',
borderColor: 'red'
},
test5: {
type: 'label',
xValue: 7.5,
yValue: 2.5,
backgroundColor: 'white',
borderColor: 'red',
borderWidth: 1,
content: ['callout is', 'not drawn'],
xAdjust: -44,
callout: {
display: true,
margin: 5,
}
},
test6: {
type: 'point',
xValue: 7.5,
yValue: 2.5,
radius: 2,
backgroundColor: 'red',
borderColor: 'red'
},
test7: {
type: 'label',
xValue: 7.5,
yValue: 7.5,
backgroundColor: 'white',
borderColor: 'red',
borderWidth: 1,
content: ['callout is', 'not drawn'],
xAdjust: 44,
callout: {
display: true,
margin: 5,
}
},
test8: {
type: 'point',
xValue: 7.5,
yValue: 7.5,
radius: 2,
backgroundColor: 'red',
borderColor: 'red'
}
}
}
}
}
},
options: {
spriteText: true
}
};
Binary file added test/fixtures/label/calloutNoShown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.