One thing you can do is create custom sub-segments by adding methods to d3.selection.prototype . You can create a selection.first() method that selects the first item in the selection and a selection.last() method that selects the last item. For example:
d3.selection.prototype.first = function() { return d3.select(this[0][0]); }; d3.selection.prototype.last = function() { var last = this.size() - 1; return d3.select(this[0][last]); };
This will allow you to do the following:
var tickLabels = svg.selectAll('.tick text'); tickLabels.first() .attr('transform','translate(4,0)'); tickLabels.last() .attr('transform','translate(-4,0)');
Of course, you need to make sure that you have only one axis, if you do. Otherwise, specify the axis in your initial selection:
var tickLabels = svg.selectAll('.axis.x .tick text');
HERE is an example.
jshanley
source share