Skip to main content

Proper method of chaining and selecting nested group elements for D3 updating?

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.

enter image description here

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, whereas select(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 using data() further down the variable chain to children elements as it's bound and propagated through parents.
  • 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

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data