Skip to content

Commit cd651a6

Browse files
authored
Merge pull request #33 from EOX-A/tooltip_fix
Tooltip fix
2 parents e01fc19 + 7d003c9 commit cd651a6

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "D3.TimeSlider",
3-
"version": "1.6.4",
3+
"version": "1.6.5-rc.0",
44
"description": "A D3.js based time slider",
55
"homepage": "https://github.com/EOX-A/d3.TimeSlider",
66
"author": {

src/d3.timeslider.coffee

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,21 +262,21 @@ class TimeSlider extends EventEmitter
262262
if @brushTooltip
263263
# setup hover events on brush
264264
@gBrush.selectAll('.resize.w')
265-
.on('mouseover', =>
265+
.on('mouseover', =>
266266
@tooltipBrushMin.transition()
267267
.duration(100)
268268
.style('opacity', 0.9))
269-
.on('mouseout', =>
269+
.on('mouseout', =>
270270
if !@brushing
271271
@tooltipBrushMin.transition()
272272
.duration(100)
273273
.style('opacity', 0))
274274
@gBrush.selectAll('.resize.e')
275-
.on('mouseover', =>
275+
.on('mouseover', =>
276276
@tooltipBrushMax.transition()
277277
.duration(100)
278278
.style('opacity', 0.9))
279-
.on('mouseout', =>
279+
.on('mouseout', =>
280280
if !@brushing
281281
@tooltipBrushMax.transition()
282282
.duration(100)
@@ -418,7 +418,7 @@ class TimeSlider extends EventEmitter
418418
e = new Date(e.getTime()+(d/2))
419419
[low, high] = @scales.x.domain()
420420
if @options.displayLimit != null and
421-
(e - s) > @options.displayLimit * 1000
421+
(e - s) > @options.displayLimit * 1000
422422
[s, e] = @scales.x.domain()
423423
@center(s,e)
424424
)

src/utils.coffee

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ bisect = (array, x, lo = 0, hi = array.length) ->
1818
return lo
1919

2020
insort = (array, x) ->
21-
array.splice(bisect(array, x), 0, x);
21+
array.splice(bisect(array, x), 0, x)
2222

2323
intersects = (a, b) ->
2424
return a[0] <= b[1] and b[0] <= a[1]
@@ -110,19 +110,43 @@ parseDuration = (duration) ->
110110
offsetDate = (date, seconds) ->
111111
return new Date(date.getTime() + seconds * 1000)
112112

113-
centerTooltipOn = (tooltip, target, dir = 'center', offset = [0, 0]) ->
113+
114+
findParentByClass = (element, className) ->
115+
# traverse up the DOM tree until requested parent node is found
116+
while element.classList
117+
if element.classList.contains(className)
118+
return element
119+
element = element.parentNode
120+
121+
122+
centerTooltipOn = (tooltip, target, dir = 'center', offset = [0, 0], viewportClassName = 'timeslider') ->
114123
rect = target.getBoundingClientRect()
115124
tooltipRect = tooltip[0][0].getBoundingClientRect()
125+
126+
viewport = findParentByClass(target, viewportClassName)
127+
128+
if viewport
129+
# clip the x-axis rectangle bounds to the viewport extent
130+
# to make the tooltip always visible
131+
viewportRect = viewport.getBoundingClientRect()
132+
xLeft = Math.max(rect.left, viewportRect.left)
133+
xRight = Math.min(rect.right, viewportRect.right)
134+
else
135+
# default to the rectangle bounds if the viewport cannot be found
136+
xLeft = rect.left
137+
xRight = rect.right
138+
116139
if dir == 'left'
117-
xOff = rect.left
140+
xOff = xLeft
118141
else if dir == 'right'
119-
xOff = rect.right
142+
xOff = xRight
120143
else
121-
xOff = rect.left + rect.width / 2
144+
xOff = xLeft + (xRight - xLeft) / 2
122145
tooltip
123146
.style('left', xOff - tooltipRect.width / 2 + offset[0] + "px")
124147
.style('top', (rect.top - tooltipRect.height) + offset[1] + "px")
125148

149+
126150
module.exports =
127151
split: split
128152
bisect: bisect

0 commit comments

Comments
 (0)