Skip to content

Commit

Permalink
Update line-customTooltips.html (Re issue #4038 )
Browse files Browse the repository at this point in the history
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
  • Loading branch information
el-ee authored and etimberg committed Mar 22, 2017
1 parent 20a8328 commit c96ad66
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
9 changes: 5 additions & 4 deletions samples/tooltips/custom-line.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
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
Expand Down Expand Up @@ -95,12 +95,13 @@
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;
Expand Down
16 changes: 8 additions & 8 deletions samples/tooltips/custom-pie.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@

<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>
Expand Down Expand Up @@ -92,12 +91,13 @@
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;
Expand Down
11 changes: 7 additions & 4 deletions samples/tooltips/custom-points.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
<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,
});
Expand All @@ -60,8 +63,8 @@
$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",
});
});
}
Expand Down

0 comments on commit c96ad66

Please sign in to comment.