Skip to content

Instantly share code, notes, and snippets.

@bunnymatic
Created January 14, 2013 18:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bunnymatic/4532098 to your computer and use it in GitHub Desktop.
Save bunnymatic/4532098 to your computer and use it in GitHub Desktop.
d3 extensions: attrs() method - allow specification of attrs as a hash instead chained attr().attr().attr()....
if(d3 && d3.selection && d3.selection.prototype) {
d3sp = d3.selection.prototype;
/** add extension functions to d3.selection */
/** attrs - set many attrs on a data node given a hash of attributes
usage:
selection.append('text')
.attrs({
x: -18,
y: -17,
fill:'pink',
});
*/
d3sp.attrs = function(attrs) {
for(var k in attrs) {
var v = attrs[k]
this.attr(k,v);
}
return this;
};
}
@nick-merrill
Copy link

I'm a JavaScript novice, but I noticed that I had to add a self = this; reference to set this.attr(k,v); in my case. I'm using CoffeeScript and the Meteor framework.

Thanks for the gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment