Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc6b0b1

Browse files
committed
fix: tooltip position out of view area
1 parent c7e516e commit bc6b0b1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

js/custom-tooltips.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,26 @@ function CustomTooltips(tooltipModel) {
120120
tooltip.appendChild(tooltipBody)
121121
}
122122

123+
const position = this._chart.canvas.getBoundingClientRect()
124+
123125
const positionY = this._chart.canvas.offsetTop
124126
const positionX = this._chart.canvas.offsetLeft
125127

128+
let positionLeft = positionX + tooltipModel.caretX
129+
const positionTop = positionY + tooltipModel.caretY
130+
// eslint-disable-next-line
131+
const halfWidth = tooltipModel.width / 2
132+
133+
if (positionLeft + halfWidth > position.width) {
134+
positionLeft -= halfWidth
135+
} else if (positionLeft < halfWidth) {
136+
positionLeft += halfWidth
137+
}
138+
126139
// Display, position, and set styles for font
127140
tooltip.style.opacity = 1
128-
tooltip.style.left = `${positionX + tooltipModel.caretX}px`
129-
tooltip.style.top = `${positionY + tooltipModel.caretY}px`
141+
tooltip.style.left = `${positionLeft}px`
142+
tooltip.style.top = `${positionTop}px`
130143
}
131144

132145
export default CustomTooltips

0 commit comments

Comments
 (0)