I am having issues with my tooltip not following my mouse position on only the enter selection.
Attached is a diagram of the DOM structure of my code, where the highlighted portions represent what I am trying to select and modify. I used element as a placeholder.
In my case, g.child
represents a tooltip that moves alongside (x, y) mouse co-ordinates. My main problem is that entering g.child
, it doesn't behave as expected (i.e.: doesn't move and remains static in one position). However after updating g.child
and element.child
behaves as expected (i.e.: moves alongside the mouse position).
I wrote this code under these assumptions and welcome input if they're incorrect:
selectAll(selector)
works recursively and flattens the selection, whereasselect(selector)
selects the first match but propagates the data forward.- by chaining
svg.selectAll('g.parent).select('g.child').select('element.child')
, I would be maintaining hierarchy while selecting the final child element, giving me the ability to update it. - since the data is bound
parents
and specified by a unique identifier, I did not re-define usingdata()
further down the variable chain to children elements as it's bound and propagated throughparents
. - I did not need to include an exit portion as I am not getting rid of any existing elements, only updating whatever is already entered.
Here is the adapted example code for my problem. I have only included the entering and updating bits of the script:
// dummy data
let data = [
{ key: 1,
light = #ffffff,
dark = #000000 },
...
, // note: n represents the nth value
{ key: n,
light = #eeeeee,
dark = #222222 }
]
//=== D3 ====
const svg = d3.select('svg')
const parents = svg
.selectAll('g.parent')
.data(data, d => d.key) // maintaining object constancy using identifier
// ENTER
const enterParents = parents
.enter()
.append('g')
.attr('class', 'parent')
.attr('width', 800)
.attr('height', 360)
enterParents
.append('element')
.attr('class', 'parent')
.attr('fill', d => d.light)
// entering nested child group g.child per g.parent
const children = parents.select('g.child')
enterChild = enterParents
.append('g')
.attr('class', 'child')
enterChild
.append('element')
.class('child')
.attr('fill', d => d.light)
// UPDATE
parents
.select('element.parent')
.attr('fill', d => d.dark)
children
.select('element.child')
.attr('fill', d => d.dark)
// adding event listener for mouse position hovering over any g.parent
parents
.on('mousemove', function(e) {
const mouse = d3.pointer(e)
const mouseX = mouse[0]
const mouseY = mouse[1]
children
.attr('transform', `translate( ${mouseX}, ${mouseY})`)
})
}
The tooltip background updates as expected, but my only issue is the tooltip not following the mouse on mousemove. It is static on entering but after updating it moves alongside the mouse.
Would my problem with the tooltip position not moving with the mouse be a result of how groups are selected, stored, and called as variables?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/kKRYGa3
Comments
Post a Comment