Skip to content

Commit ba2a68b

Browse files
committed
Allow tickFormat to be specified as a constant.
This was basically supported already, but there was a slight bug in the truthy check for tickFormat. Now we check for null rather than falsiness.
1 parent 4e96a35 commit ba2a68b

4 files changed

Lines changed: 33 additions & 21 deletions

File tree

d3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3506,7 +3506,7 @@ d3.svg.axis = function() {
35063506

35073507
// Ticks.
35083508
var ticks = scale.ticks.apply(scale, tickArguments_),
3509-
tickFormat = tickFormat_ || scale.tickFormat.apply(scale, tickArguments_);
3509+
tickFormat = tickFormat_ == null ? scale.tickFormat.apply(scale, tickArguments_) : tickFormat_;
35103510

35113511
// Minor ticks.
35123512
var subticks = d3_svg_axisSubdivide(scale, ticks, tickSubdivide),

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/svg/axis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ d3.svg.axis = function() {
1515

1616
// Ticks.
1717
var ticks = scale.ticks.apply(scale, tickArguments_),
18-
tickFormat = tickFormat_ || scale.tickFormat.apply(scale, tickArguments_);
18+
tickFormat = tickFormat_ == null ? scale.tickFormat.apply(scale, tickArguments_) : tickFormat_;
1919

2020
// Minor ticks.
2121
var subticks = d3_svg_axisSubdivide(scale, ticks, tickSubdivide),

test/svg/axis-test.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ suite.addBatch({
191191
assert.equal(aa[0][0], b);
192192
assert.equal(aa[0][1], 42);
193193
},
194+
"passes any arguments to the scale's tickFormat function": function(axis) {
195+
var b = {},
196+
x = d3.scale.linear(),
197+
a = axis().scale(x).ticks(b, 42),
198+
g = d3.select("body").html("").append("svg:g"),
199+
aa = [];
200+
201+
x.tickFormat = function() {
202+
aa.push(arguments);
203+
return String;
204+
};
205+
206+
g.call(a);
207+
assert.equal(aa.length, 1);
208+
assert.equal(aa[0].length, 2);
209+
assert.equal(aa[0][0], b);
210+
assert.equal(aa[0][1], 42);
211+
},
194212
"affects the generated ticks": function(axis) {
195213
var a = axis().ticks(20),
196214
g = d3.select("body").html("").append("svg:g").call(a),
@@ -240,29 +258,23 @@ suite.addBatch({
240258
var t = g.selectAll("g text");
241259
assert.equal(t.text(), "foo-0");
242260
},
243-
"passes any arguments to the scale's tick format function": function(axis) {
244-
var b = {},
245-
x = d3.scale.linear(),
246-
a = axis().scale(x).ticks(b, 42),
247-
g = d3.select("body").html("").append("svg:g"),
248-
aa = [];
249-
250-
x.tickFormat = function() {
251-
aa.push(arguments);
252-
return String;
253-
};
254-
255-
g.call(a);
256-
assert.equal(aa.length, 1);
257-
assert.equal(aa[0].length, 2);
258-
assert.equal(aa[0][0], b);
259-
assert.equal(aa[0][1], 42);
260-
},
261261
"affects the generated tick labels": function(axis) {
262262
var a = axis().tickFormat(d3.format("+.2%")),
263263
g = d3.select("body").html("").append("svg:g").call(a),
264264
t = g.selectAll("g text");
265265
assert.equal(t.text(), "+0.00%");
266+
},
267+
"can be set to a constant": function(axis) {
268+
var a = axis().tickFormat("I'm a tick!"),
269+
g = d3.select("body").html("").append("svg:g").call(a),
270+
t = g.selectAll("g text");
271+
assert.equal(t.text(), "I'm a tick!");
272+
},
273+
"can be set to a falsey constant": function(axis) {
274+
var a = axis().tickFormat(""),
275+
g = d3.select("body").html("").append("svg:g").call(a),
276+
t = g.selectAll("g text");
277+
assert.equal(t.text(), "");
266278
}
267279
},
268280

0 commit comments

Comments
 (0)