After repeatedly inspecting and cracking the code and talking with experienced d3 people, I found out that d3.svg.axis() is a function (not an object or class), so it cannot be extended and not wrapped. Thus, to โexpandโ it, we will create a new axis, run the selection on the axis() base to select these marks, then copy all the properties from the axis() base in one fell swoop and return this to the expanded functional version.
var customXAxis = (function() { var base = d3.svg.axis(); // Select and apply a style to your tick marks var newAxis = function(selection) { selection.call(base); selection.selectAll('.tick', this) .classed("year", isYear); }; // Copy all the base axis methods like 'ticks', 'scale', etc. for(var key in base) { if (base.hasOwnProperty(key)) { d3.rebind(newAxis, base, key); } } return newAxis; })();
customXAxis now completely โinheritsโ the properties of the d3 axis components. You can safely do the following:
customXAxis .ticks(2) .scale(xScale) .tickPadding(50) .tickFormat(dateFormatter); canvas.append("g").call(customXAxis);
* Using the @HerbCaudill template and inspired by @meetamit ideas.
Demo: http://jsbin.com/kabonokeki/5/
Drakes
source share