Skip to content

Instantly share code, notes, and snippets.

@mgold
Created July 24, 2015 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgold/48960da9509457dfb9e1 to your computer and use it in GitHub Desktop.
Save mgold/48960da9509457dfb9e1 to your computer and use it in GitHub Desktop.
/* 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