Dynamic append element name

1,178 views
Skip to first unread message

evlapix

unread,
Aug 29, 2011, 5:12:46 PM8/29/11
to d3...@googlegroups.com
I'm about to implement a new feature in my graph and thought I might be able to take a data source like this:

[ { type: "circle" }, { type: "square" }, { type: "triangle" } ]

...and dynamically populate the selection.append() value. Something like this:

selection.append(function(d) { return d.type; });

I read in the documentation that this feature might be available in the future: https://github.com/mbostock/d3/wiki/Selections#wiki-append

So I'll have to find another way. I'm tempted to add the feature myself, but knowing that each element uses different attributes, I'm doubtful it would yield the results I'm looking for. 

Additionally, I did consider to break the data up, but I'm using the force layout and so the data source needs to remain together. 

Andy ideas or inspiration would be appreciated. Thanks.

Mike Bostock

unread,
Aug 29, 2011, 5:20:39 PM8/29/11
to d3...@googlegroups.com
See this thread:

http://groups.google.com/group/d3-js/browse_thread/thread/9e0734603d6ba703

Interestingly, you can use the new selector functions in v2 to create
and append elements dynamically as part of the select operator. But
I'm not sure I'd recommend it, as you probably want to assign
different attributes or styles to different types, as well. But, if
you really want to create types dynamically, it would look something
like this:

selection.select(function(d) {
return this.appendChild(document.createElementNS(d3.ns.prefix.svg, d.type));
});

Mike

evlapix

unread,
Aug 30, 2011, 11:09:20 PM8/30/11
to d3...@googlegroups.com
You nailed it Mike. Thanks.
Reply all
Reply to author
Forward
0 new messages