Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue #8 Move tooltip with mouse pointer #157

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/flamegraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,12 @@ export default function () {
g.exit()
.remove()

g.on('mouseover', function (d) {
if (tooltip) tooltip.show(d, this)
detailsHandler(labelHandler(d))
}).on('mouseout', function () {
g.on('mouseout', function () {
if (tooltip) tooltip.hide()
detailsHandler(null)
}).on('mousemove', function (d) {
if (tooltip) tooltip.show(d)
detailsHandler(labelHandler(d))
rohchakr marked this conversation as resolved.
Show resolved Hide resolved
})
})
}
Expand Down
20 changes: 16 additions & 4 deletions src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ function defaultLabel (d) {
}

export function defaultFlamegraphTooltip () {
var rootElement = select('body')
var tooltip = null
var html = defaultLabel
const rootElement = select('body')
const chartElement = select('#chart')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be passed to the tooltip init, since it can be something else.

const chartBoundingClientRect = chartElement._groups[0][0].getBoundingClientRect()
const chartRightBoundary = chartBoundingClientRect.x + chartBoundingClientRect.width
let tooltip = null
let html = defaultLabel

function tip () {
tooltip = rootElement
Expand All @@ -32,9 +35,18 @@ export function defaultFlamegraphTooltip () {
.style('opacity', 1)
.style('pointer-events', 'all')

const tooltipAreaWidth = tooltip.nodes()[0].getBoundingClientRect().width
const tooltipRightBoundary = event.pageX + 10 + tooltipAreaWidth

let tooltipLeft = event.pageX + 10

if (tooltipRightBoundary > chartRightBoundary) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we completely flip the tooltip to the right if boundary is exceeded? Not just shift so it fits?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea. Let me try...

tooltipLeft = tooltipLeft - tooltipAreaWidth - 12
}

tooltip
.html(html(d))
.style('left', event.pageX + 5 + 'px')
.style('left', tooltipLeft + 'px')
.style('top', event.pageY + 5 + 'px')

return tip
Expand Down