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...

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...