Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:46:15+00:00 2026-06-17T17:46:15+00:00

With D3, I’m finding myself doing this a lot: selectAll(‘foo’).data([‘foo’]).enter().append(‘foo’) I’d like to simply

  • 0

With D3, I’m finding myself doing this a lot:

selectAll('foo').data(['foo']).enter().append('foo')

I’d like to simply add a node if it doesn’t already exist, because I need to do updates at different places in the DOM tree, and the data I have handy isn’t exactly parallel to the DOM.

Is this a sign that I should reformulate my data so that it is parallel, or is there a less silly pattern that folks use for this kind of ‘create if missing’ thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:46:16+00:00Added an answer on June 17, 2026 at 5:46 pm

    D3 implements a JOIN + INSERT/UPDATE/DELETE pattern well known from the DB world. In d3 you first select some DOM elements and then join it with the data:

    //join existing 'g.class' DOM elements with `data` elements
    var join = d3.select('g.class').data(data)   
    
    //get the join pairs that did not have 'g.class' join partner and
    //INSERT new 'g.class' DOM elements into the "empty slots"
    join.enter().append('g').attr('class', 'class')
    
    //get the join pairs that did not have a `data` element as join partner and
    //DELETE the existing 'g.class' DOM elements
    join.exit().remove()
    
    //get the join pairs that have a `data` element join partner and
    //UPDATE the 'g.class' DOM elements
    join.attr("name","value")
    

    You see, if you have data that nicely fits your UI requirements you can write very maintainable code. If you try hacks outside this pattern, your UI code will make you very sad soon. You should preprocess your data to fit the needs of the UI.

    D3 provides some preprocessors for some use cases. For example the treemap layout function flattens a hierarchical data set to a list treemap.nodes, which you can then use as simple list-based data set to draw a rectangle for each element. The treemap layout also computes all x,y,width,height values for you. You just draw the rects and do not care about the hierarchy anymore.

    In the same way you can develop your own helper functions to

    1. convert your data to a better consumable format and
    2. try to enrich the data with “hints” for the UI, how to draw it

    These “hints” may comprise geometry values, label texts, colors, and basically everything that you cannot directly derive from looking at a single data element (such as the treemap geometry), and that would require you to correlate each element with some/all other elements (e.g., determining the nesting depth of a node in a tree). Doing such a tasks in one preprocessing step allows you write cleaner and faster code for that task and separates the data processing from the drawing of the UI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.