Skip to content

Commit

Permalink
Merge pull request #1990 from COzero/feature/add-max-min-tick-format
Browse files Browse the repository at this point in the history
feat($axis): Add ability to set different tick format for max/min
  • Loading branch information
liquidpele authored Apr 29, 2017
2 parents 052ef1f + c2e32fa commit f78139d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/models/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ nv.models.axis = function() {
, fontSize = undefined
, duration = 250
, dispatch = d3.dispatch('renderEnd')
, tickFormatMaxMin
;
axis
.scale(scale)
Expand Down Expand Up @@ -106,7 +107,8 @@ nv.models.axis = function() {
.attr('y', -axis.tickPadding())
.attr('text-anchor', 'middle')
.text(function(d,i) {
var v = fmt(d);
var formatter = tickFormatMaxMin || fmt;
var v = formatter(d);
return ('' + v).match('NaN') ? '' : v;
});
axisMaxMin.watchTransition(renderWatch, 'min-max top')
Expand All @@ -123,7 +125,7 @@ nv.models.axis = function() {
var rotateLabelsRule = '';
if (rotateLabels%360) {
//Reset transform on ticks so textHeight can be calculated correctly
xTicks.attr('transform', '');
xTicks.attr('transform', '');
//Calculate the longest xTick width
xTicks.each(function(d,i){
var box = this.getBoundingClientRect();
Expand Down Expand Up @@ -181,7 +183,8 @@ nv.models.axis = function() {
.attr('transform', rotateLabelsRule)
.style('text-anchor', rotateLabels ? (rotateLabels%360 > 0 ? 'start' : 'end') : 'middle')
.text(function(d,i) {
var v = fmt(d);
var formatter = tickFormatMaxMin || fmt;
var v = formatter(d);
return ('' + v).match('NaN') ? '' : v;
});
axisMaxMin.watchTransition(renderWatch, 'min-max bottom')
Expand Down Expand Up @@ -216,7 +219,8 @@ nv.models.axis = function() {
.attr('x', axis.tickPadding())
.style('text-anchor', 'start')
.text(function(d, i) {
var v = fmt(d);
var formatter = tickFormatMaxMin || fmt;
var v = formatter(d);
return ('' + v).match('NaN') ? '' : v;
});
axisMaxMin.watchTransition(renderWatch, 'min-max right')
Expand Down Expand Up @@ -260,7 +264,8 @@ nv.models.axis = function() {
.attr('x', -axis.tickPadding())
.attr('text-anchor', 'end')
.text(function(d,i) {
var v = fmt(d);
var formatter = tickFormatMaxMin || fmt;
var v = formatter(d);
return ('' + v).match('NaN') ? '' : v;
});
axisMaxMin.watchTransition(renderWatch, 'min-max right')
Expand Down Expand Up @@ -331,9 +336,9 @@ nv.models.axis = function() {
and the arithmetic trick below solves that.
*/
return !parseFloat(Math.round(d * 100000) / 1000000) && (d !== undefined)
})
})
.classed('zero', true);

//store old scales for use in transitions on update
scale0 = scale.copy();

Expand Down Expand Up @@ -364,6 +369,7 @@ nv.models.axis = function() {
ticks: {get: function(){return ticks;}, set: function(_){ticks=_;}},
width: {get: function(){return width;}, set: function(_){width=_;}},
fontSize: {get: function(){return fontSize;}, set: function(_){fontSize=_;}},
tickFormatMaxMin: {get: function(){return tickFormatMaxMin;}, set: function(_){tickFormatMaxMin=_;}},

// options that require extra logic in the setter
margin: {get: function(){return margin;}, set: function(_){
Expand Down
15 changes: 15 additions & 0 deletions test/mocha/axis.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,18 @@ describe 'NVD3', ->

tick = builder.$ '.nv-y.nv-axis .tick.zero'
tick.length.should.equal 1, 'y axis zero'

it 'default tick format for max/min should be integer based', ->
axis = builder.model.xAxis
builder.model.update()
minAxisText = builder.$('.nv-axisMaxMin.nv-axisMaxMin-x.nv-axisMin-x text')[0].textContent

minAxisText.should.equal('-1')

it 'tickFormatMaxMin should change tick format of max/min', ->
axis = builder.model.xAxis
axis.tickFormatMaxMin(d3.format(',.2f'))
builder.model.update()
minAxisText = builder.$('.nv-axisMaxMin.nv-axisMaxMin-x.nv-axisMin-x text')[0].textContent

minAxisText.should.equal('-1.00')

0 comments on commit f78139d

Please sign in to comment.