Skip to content

Instantly share code, notes, and snippets.

@seliopou
Created December 18, 2012 21:57
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 seliopou/4332420 to your computer and use it in GitHub Desktop.
Save seliopou/4332420 to your computer and use it in GitHub Desktop.
A reusable chart combinator for D3.
var chart = (function() {
function copy(o) {
return Object.keys(o).reduce(function(no, k) {
no[k] = o[k];
return no;
}, {});
}
return function(defaults, _cstr) {
return function() {
var attrs = {};
var my = function(selection) {
return _cstr(selection, copy(attrs));
};
d3.entries(defaults).forEach(function(e) {
my[e.key] = function(value) {
if (arguments.length == 0) { return attrs[e.key]; }
attrs[e.key] = value;
return my;
}
attrs[e.key] = e.value;
});
return my;
};
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment