Created
July 24, 2015 00:49
-
-
Save mgold/48960da9509457dfb9e1 to your computer and use it in GitHub Desktop.
This file contains 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
/* d3.selection.translate() - an unofficial add-on | |
Translate an SVG selection by passing in two arguments, a two-value array, or | |
a function that produces a two-value array when called with the usual arguments. | |
Pass no arguments to retrieve the current translation. | |
Caveats: Will clobber any other SVG transforms. Does not work with transitions. | |
*/ | |
d3.selection.prototype.translate = function(a, b) { | |
if (typeof a === "function"){ | |
return this.each(function(){ | |
var x = a.apply(this, arguments); | |
if (x == null) return this.removeAttribute("transform"); | |
else return this.setAttribute("transform", "translate(" + x + ")"); | |
}) | |
} | |
return arguments.length === 0 | |
? d3.transform(this.attr("transform")).translate | |
: arguments.length === 1 | |
? this.attr("transform", "translate(" + a + ")") | |
: this.attr("transform", "translate(" + [a, b] + ")") | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment