Skip to content

Commit f78139d

Browse files
authored
Merge pull request #1990 from COzero/feature/add-max-min-tick-format
feat($axis): Add ability to set different tick format for max/min
2 parents 052ef1f + c2e32fa commit f78139d

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/models/axis.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ nv.models.axis = function() {
2222
, fontSize = undefined
2323
, duration = 250
2424
, dispatch = d3.dispatch('renderEnd')
25+
, tickFormatMaxMin
2526
;
2627
axis
2728
.scale(scale)
@@ -106,7 +107,8 @@ nv.models.axis = function() {
106107
.attr('y', -axis.tickPadding())
107108
.attr('text-anchor', 'middle')
108109
.text(function(d,i) {
109-
var v = fmt(d);
110+
var formatter = tickFormatMaxMin || fmt;
111+
var v = formatter(d);
110112
return ('' + v).match('NaN') ? '' : v;
111113
});
112114
axisMaxMin.watchTransition(renderWatch, 'min-max top')
@@ -123,7 +125,7 @@ nv.models.axis = function() {
123125
var rotateLabelsRule = '';
124126
if (rotateLabels%360) {
125127
//Reset transform on ticks so textHeight can be calculated correctly
126-
xTicks.attr('transform', '');
128+
xTicks.attr('transform', '');
127129
//Calculate the longest xTick width
128130
xTicks.each(function(d,i){
129131
var box = this.getBoundingClientRect();
@@ -181,7 +183,8 @@ nv.models.axis = function() {
181183
.attr('transform', rotateLabelsRule)
182184
.style('text-anchor', rotateLabels ? (rotateLabels%360 > 0 ? 'start' : 'end') : 'middle')
183185
.text(function(d,i) {
184-
var v = fmt(d);
186+
var formatter = tickFormatMaxMin || fmt;
187+
var v = formatter(d);
185188
return ('' + v).match('NaN') ? '' : v;
186189
});
187190
axisMaxMin.watchTransition(renderWatch, 'min-max bottom')
@@ -216,7 +219,8 @@ nv.models.axis = function() {
216219
.attr('x', axis.tickPadding())
217220
.style('text-anchor', 'start')
218221
.text(function(d, i) {
219-
var v = fmt(d);
222+
var formatter = tickFormatMaxMin || fmt;
223+
var v = formatter(d);
220224
return ('' + v).match('NaN') ? '' : v;
221225
});
222226
axisMaxMin.watchTransition(renderWatch, 'min-max right')
@@ -260,7 +264,8 @@ nv.models.axis = function() {
260264
.attr('x', -axis.tickPadding())
261265
.attr('text-anchor', 'end')
262266
.text(function(d,i) {
263-
var v = fmt(d);
267+
var formatter = tickFormatMaxMin || fmt;
268+
var v = formatter(d);
264269
return ('' + v).match('NaN') ? '' : v;
265270
});
266271
axisMaxMin.watchTransition(renderWatch, 'min-max right')
@@ -331,9 +336,9 @@ nv.models.axis = function() {
331336
and the arithmetic trick below solves that.
332337
*/
333338
return !parseFloat(Math.round(d * 100000) / 1000000) && (d !== undefined)
334-
})
339+
})
335340
.classed('zero', true);
336-
341+
337342
//store old scales for use in transitions on update
338343
scale0 = scale.copy();
339344

@@ -364,6 +369,7 @@ nv.models.axis = function() {
364369
ticks: {get: function(){return ticks;}, set: function(_){ticks=_;}},
365370
width: {get: function(){return width;}, set: function(_){width=_;}},
366371
fontSize: {get: function(){return fontSize;}, set: function(_){fontSize=_;}},
372+
tickFormatMaxMin: {get: function(){return tickFormatMaxMin;}, set: function(_){tickFormatMaxMin=_;}},
367373

368374
// options that require extra logic in the setter
369375
margin: {get: function(){return margin;}, set: function(_){

test/mocha/axis.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,18 @@ describe 'NVD3', ->
153153

154154
tick = builder.$ '.nv-y.nv-axis .tick.zero'
155155
tick.length.should.equal 1, 'y axis zero'
156+
157+
it 'default tick format for max/min should be integer based', ->
158+
axis = builder.model.xAxis
159+
builder.model.update()
160+
minAxisText = builder.$('.nv-axisMaxMin.nv-axisMaxMin-x.nv-axisMin-x text')[0].textContent
161+
162+
minAxisText.should.equal('-1')
163+
164+
it 'tickFormatMaxMin should change tick format of max/min', ->
165+
axis = builder.model.xAxis
166+
axis.tickFormatMaxMin(d3.format(',.2f'))
167+
builder.model.update()
168+
minAxisText = builder.$('.nv-axisMaxMin.nv-axisMaxMin-x.nv-axisMin-x text')[0].textContent
169+
170+
minAxisText.should.equal('-1.00')

0 commit comments

Comments
 (0)