Created
January 14, 2013 18:21
-
-
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()....
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm a JavaScript novice, but I noticed that I had to add a
self = this;
reference to setthis.attr(k,v);
in my case. I'm using CoffeeScript and the Meteor framework.Thanks for the gist!