Add window scroll position to tooltip position calculation so they show up in the intended place instead of potentially off-screen! Moved tooltips inside the canvas parent container since they are being positioned in terms of its location
tooltipEl = document.createElement('div');
tooltipEl.id = 'chartjs-tooltip';
tooltipEl.innerHTML = "<table></table>"
- document.body.appendChild(tooltipEl);
+ this._chart.canvas.parentNode.appendChild(tooltipEl);
}
// Hide if no tooltip
tableRoot.innerHTML = innerHtml;
}
- var position = this._chart.canvas.getBoundingClientRect();
+ var positionY = this._chart.canvas.offsetTop;
+ var positionX = this._chart.canvas.offsetLeft;
// Display, position, and set styles for font
tooltipEl.style.opacity = 1;
- tooltipEl.style.left = position.left + tooltip.caretX + 'px';
- tooltipEl.style.top = position.top + tooltip.caretY + 'px';
+ tooltipEl.style.left = positionX + tooltip.caretX + 'px';
+ tooltipEl.style.top = positionY + tooltip.caretY + 'px';
tooltipEl.style.fontFamily = tooltip._fontFamily;
tooltipEl.style.fontSize = tooltip.fontSize;
tooltipEl.style.fontStyle = tooltip._fontStyle;
<body>
<div id="canvas-holder" style="width: 300px;">
- <canvas id="chart-area" width="300" height="300" />
- </div>
-
- <div id="chartjs-tooltip">
- <table></table>
+ <canvas id="chart-area" width="300" height="300"></canvas>
+ <div id="chartjs-tooltip">
+ <table></table>
+ </div>
</div>
<script>
tableRoot.innerHTML = innerHtml;
}
- var position = this._chart.canvas.getBoundingClientRect();
+ var positionY = this._chart.canvas.offsetTop;
+ var positionX = this._chart.canvas.offsetLeft;
// Display, position, and set styles for font
tooltipEl.style.opacity = 1;
- tooltipEl.style.left = position.left + tooltip.caretX + 'px';
- tooltipEl.style.top = position.top + tooltip.caretY + 'px';
+ tooltipEl.style.left = positionX + tooltip.caretX + 'px';
+ tooltipEl.style.top = positionY + tooltip.caretY + 'px';
tooltipEl.style.fontFamily = tooltip._fontFamily;
tooltipEl.style.fontSize = tooltip.fontSize;
tooltipEl.style.fontStyle = tooltip._fontStyle;
<body>
<div id="canvas-holder1" style="width:75%;">
<canvas id="chart1"></canvas>
+ <div class="chartjs-tooltip" id="tooltip-0"></div>
+ <div class="chartjs-tooltip" id="tooltip-1"></div>
</div>
- <div class="chartjs-tooltip" id="tooltip-0"></div>
- <div class="chartjs-tooltip" id="tooltip-1"></div>
<script>
var customTooltips = function (tooltip) {
$(this._chart.canvas).css("cursor", "pointer");
+ var positionY = this._chart.canvas.offsetTop;
+ var positionX = this._chart.canvas.offsetLeft;
+
$(".chartjs-tooltip").css({
opacity: 0,
});
$tooltip.html(content);
$tooltip.css({
opacity: 1,
- top: dataPoint.y + "px",
- left: dataPoint.x + "px",
+ top: positionY + dataPoint.y + "px",
+ left: positionX + dataPoint.x + "px",
});
});
}