d3.eesur = {}; // sub-namespace to the d3 namespace d3.eesur.line = function module() { // add a line module // default variable _values in a closure var w = 400, h = 400, colour = 'black', x1 = 0, x2 = 100; function exports(_selection) { _selection.each(function(_data) { var yValue = function (d) { return d.value; }; var line = d3.select(this) .append('svg') .attr({ width: w, height: h, class: 'line-example' }) .selectAll('line') .data(_data); line .enter() .append('line') .attr({ stroke: colour, x1: x1, x2: x2, y1: yValue, y2: yValue }); // update code here // line line .exit() .remove(); }); } // public api – getters and setters at the same time (will either get a new value or set the default variables) exports.w = function(_value) { if (!arguments.length) return w; w = _value; return this; }; exports.h = function(_value) { if (!arguments.length) return h; h = _value; return this; }; exports.colour = function(_value) { if (!arguments.length) return colour; colour = _value; return this; }; exports.x1 = function(_value) { if (!arguments.length) return x1; x1 = _value; return this; }; exports.x2 = function(_value) { if (!arguments.length) return x2; x2 = _value; return this; }; return exports; };