Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A helper to avoid configuration property accessor boilerplate when creating D3 "reusable chart"–style widgets.
// helper for <>
function _makeChart() {
var state = Object.create(null),
viz = this.bind(state); (k) {
viz[k] = function (v) {
if (!arguments.length) return state[k];
state[k] = v;
return viz;
return viz;
// --- example usage ---
// define a (really simple) resuable chart
var chartFactory = _makeChart.bind(function (sel) {
var scale = d3.scale.linear().range([0,]);
sel.text(function (d) { return "Scaled value is: " + scale(d); });
}, 'foo', 'bar');
// use it like any other…
var myChart = chartFactory().foo(42).bar('none');'.the.things').data([1,-1,Math.PI]).call(function (sel) {
sel.enter().append('p').classed('the', true).classed('things', true);
sel.exit().remove();; // (usually chart would draw small multiples using *nested* arrays)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment