If I got it right, then the preferred idiom to produce a series of nodes as children of a parent is:
d3.select(parent).selectAll(child-selector).data(series-array).enter().append(child-node)
now here I am, ashamed and amazed that I can't come up with how to generate children if they *possibly* don't exist yet in some fast, concise and clean-looking d3 code, if these conditions apply:
1) you do not know [yet] if the given child node(s) exist, so the code must find out and adjust as it goes along (that is: under the hood, in d3, there's an if() condition in there somewhere, one way or another)
2) you do not have any data for the parent to drive the code, i.e. .data(series).enter() isn't part of the idiom to be.
#2 is optional; if d3 is designed to /require/ .data() then we can always succumb and fake a range.
In other circumstances I'd do this kind of HTML manipulation in mootools, but to load moo for just this bit of "if (!$('node')) create-node" feels quite dirty and I believe d3 is able to do it all by itself, but somehow I'm completely stuck and don't see the way. Database folks would say I'm looking for the UPSERT (update/insert) syntax in d3 here.
...
If you need a sample scenario to visualize what I'm saying here, here's a very basic exercise: imagine you start with html with an empty <div> like this:
<html>
<body>
<div id="parent"></div>
</body>
</html>
and then there's a bit of setInterval() repeating javascript which always executes the same code which initially adds a <p> child to the <div> with a bit of .text() where a random value is printed and in the next intervals that same code updates that <p> node with a new .text() value. No if()s (or a?b:c) in the user code.
Since this smells a lot like 'animation' it's important to note that it isn't, at least it's to be done in an explicit setInterval() -- otherwise you'ld be able to provide me with an d3.transition based answer which is precisely not what I'm looking for. ;'-)
I ask this generically as I don't 'see' the mechanism/idiom to use; that's what I'm looking for as this situation indicates to me that I don't grok an important-if-unidentified part of d3. d3 feels like design genius an I'm feeling pretty darn stupid right now.
Met vriendelijke groeten / Best regards,
Ger Hobbelt
--------------------------------------------------
web:
http://www.hobbelt.com/
http://www.hebbut.net/mail:
g...@hobbelt.commobile: +31-6-11 120 978
--------------------------------------------------