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

Confusion between commands.Bot and discord.Client | Which one should I use?

Whenever you look at YouTube tutorials or code from this website there is a real variation. Some developers use client = discord.Client(intents=intents) while the others use bot = commands.Bot(command_prefix="something", intents=intents) . Now I know slightly about the difference but I get errors from different places from my code when I use either of them and its confusing. Especially since there has a few changes over the years in discord.py it is hard to find the real difference. I tried sticking to discord.Client then I found that there are more features in commands.Bot . Then I found errors when using commands.Bot . An example of this is: When I try to use commands.Bot client = commands.Bot(command_prefix=">",intents=intents) async def load(): for filename in os.listdir("./Cogs"): if filename.endswith(".py"): client.load_extension(f"Cogs.{filename[:-3]}") The above doesnt giveany response from my Cogs ...

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

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...