Skip to content

Commit 11e9d1d

Browse files
modify tooltip so it is always at least 6 pixels away from screen edges
1 parent 61cab23 commit 11e9d1d

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

assets/src/css/dashboard.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
html, body { height: 100%; overflow-y: scroll; }
2+
13
/* standalone specific styles */
24
body.ka-dashboard {
35
background: #f1f1f1;

assets/src/js/imports/chart.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var chart = document.querySelector('#ka-chart');
22
if (chart) {
33
var tooltip = document.querySelector('.ka-chart--tooltip');
4+
var arrow = document.querySelector('.ka-chart--tooltip-arrow');
45
var bars = chart.querySelectorAll('.bars g');
56
var barWidth;
67

@@ -21,12 +22,26 @@ if (chart) {
2122
// set tooltip position relative to top-left of document
2223
tooltip.style.display = 'block';
2324
var scrollY = window.pageYOffset !== undefined ? window.pageYOffset : window.scrollTop
24-
var scrollX = window.pageXOffset !== undefined ? window.pageXOffset : window.scrollLeft
25+
var scrollX = 0; //window.pageXOffset !== undefined ? window.pageXOffset : window.scrollLeft
2526
var styles = e.target.parentElement.getBoundingClientRect() // <g> element
26-
var left = Math.round(styles.left + scrollX - 0.5 * tooltip.clientWidth + 0.5 * barWidth) + 'px';
27-
var top = Math.round(styles.top + scrollY - tooltip.clientHeight) + 'px';
28-
tooltip.style.left = left;
29-
tooltip.style.top = top;
27+
var left = Math.round(styles.left + scrollX - 0.5 * tooltip.clientWidth + 0.5 * barWidth);
28+
var top = Math.round(styles.top + scrollY - tooltip.clientHeight);
29+
var offCenter = 0;
30+
31+
// if tooltip goes off the screen, position it a bit off center
32+
if (left < 6) {
33+
offCenter = -left + 6;
34+
} else if (left + tooltip.clientWidth > document.body.clientWidth - 6) {
35+
offCenter = document.body.clientWidth - (left + tooltip.clientWidth) + 6;
36+
}
37+
38+
// shift tooltip to the right (or left)
39+
left += offCenter;
40+
41+
// shift arrow to the left (or right)
42+
arrow.style.marginLeft = offCenter === 0 ? 'auto' : ((0.5 * tooltip.clientWidth) - 6 - offCenter) + 'px';
43+
tooltip.style.left = left + 'px';
44+
tooltip.style.top = top + 'px';
3045
})
3146
}
3247

0 commit comments

Comments
 (0)