Skip to content

Commit 2af1d30

Browse files
committed
Merge branch 'interpolate-precision' into 3.4.13
2 parents 669b934 + 037063c commit 2af1d30

8 files changed

Lines changed: 28 additions & 25 deletions

File tree

d3.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5605,9 +5605,9 @@
56055605
}
56065606
d3.interpolateNumber = d3_interpolateNumber;
56075607
function d3_interpolateNumber(a, b) {
5608-
b -= a = +a;
5608+
a = +a, b = +b;
56095609
return function(t) {
5610-
return a + b * t;
5610+
return a * (1 - t) + b * t;
56115611
};
56125612
}
56135613
d3.interpolateString = d3_interpolateString;
@@ -5906,15 +5906,15 @@
59065906
};
59075907
}
59085908
function d3_uninterpolateNumber(a, b) {
5909-
b = b - (a = +a) ? 1 / (b - a) : 0;
5909+
b = (b -= a = +a) || 1 / b;
59105910
return function(x) {
5911-
return (x - a) * b;
5911+
return (x - a) / b;
59125912
};
59135913
}
59145914
function d3_uninterpolateClamp(a, b) {
5915-
b = b - (a = +a) ? 1 / (b - a) : 0;
5915+
b = (b -= a = +a) || 1 / b;
59165916
return function(x) {
5917-
return Math.max(0, Math.min(1, (x - a) * b));
5917+
return Math.max(0, Math.min(1, (x - a) / b));
59185918
};
59195919
}
59205920
d3.layout = {};

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/interpolate/number.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
d3.interpolateNumber = d3_interpolateNumber;
22

33
function d3_interpolateNumber(a, b) {
4-
b -= a = +a;
5-
return function(t) { return a + b * t; };
4+
a = +a, b = +b;
5+
return function(t) { return a * (1 - t) + b * t; };
66
}

src/interpolate/uninterpolate.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function d3_uninterpolateNumber(a, b) {
2-
b = b - (a = +a) ? 1 / (b - a) : 0;
3-
return function(x) { return (x - a) * b; };
2+
b = (b -= a = +a) || 1 / b;
3+
return function(x) { return (x - a) / b; };
44
}
55

66
function d3_uninterpolateClamp(a, b) {
7-
b = b - (a = +a) ? 1 / (b - a) : 0;
8-
return function(x) { return Math.max(0, Math.min(1, (x - a) * b)); };
7+
b = (b -= a = +a) || 1 / b;
8+
return function(x) { return Math.max(0, Math.min(1, (x - a) / b)); };
99
}

test/interpolate/interpolate-test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ suite.addBatch({
1010

1111
"when b is a number": {
1212
"interpolates numbers": function(d3) {
13-
assert.strictEqual(d3.interpolate(2, 12)(.4), 6);
13+
assert.strictEqual(d3.interpolate(2, 12)(.25), 4.5);
1414
},
1515
"coerces a to a number": function(d3) {
1616
assert.strictEqual(d3.interpolate("", 1)(.5), .5);
17-
assert.strictEqual(d3.interpolate("2", 12)(.4), 6);
18-
assert.strictEqual(d3.interpolate([2], 12)(.4), 6);
17+
assert.strictEqual(d3.interpolate("2", 12)(.25), 4.5);
18+
assert.strictEqual(d3.interpolate([2], 12)(.25), 4.5);
1919
}
2020
},
2121

@@ -79,11 +79,11 @@ suite.addBatch({
7979

8080
"when b is an array": {
8181
"interpolates each element in b": function(d3) {
82-
assert.strictEqual(JSON.stringify(d3.interpolate([2, 4], [12, 24])(.4)), "[6,12]");
82+
assert.strictEqual(JSON.stringify(d3.interpolate([2, 4], [12, 24])(.25)), "[4.5,9]");
8383
},
8484
"interpolates arrays, even when both a and b are coercible to numbers": function(d3) {
85-
assert.strictEqual(JSON.stringify(d3.interpolate([2], [12])(.4)), "[6]");
86-
assert.strictEqual(JSON.stringify(d3.interpolate([[2]], [[12]])(.4)), "[[6]]");
85+
assert.strictEqual(JSON.stringify(d3.interpolate([2], [12])(.25)), "[4.5]");
86+
assert.strictEqual(JSON.stringify(d3.interpolate([[2]], [[12]])(.25)), "[[4.5]]");
8787
},
8888
"reuses the returned array during interpolation": function(d3) {
8989
var i = d3.interpolate([2], [12]);
@@ -93,10 +93,10 @@ suite.addBatch({
9393

9494
"when b is an object": {
9595
"interpolates each property in b": function(d3) {
96-
assert.deepEqual(d3.interpolate({foo: 2, bar: 4}, {foo: 12, bar: 24})(.4), {foo: 6, bar: 12});
96+
assert.deepEqual(d3.interpolate({foo: 2, bar: 4}, {foo: 12, bar: 24})(.25), {foo: 4.5, bar: 9});
9797
},
9898
"interpolates numbers if b is coercible to a number (!isNaN(+b))": function(d3) {
99-
assert.strictEqual(d3.interpolate(new Number(2), new Number(12))(.4), 6);
99+
assert.strictEqual(d3.interpolate(new Number(2), new Number(12))(.25), 4.5);
100100
assert.strictEqual(d3.interpolate(new Date(2012, 0, 1), new Date(2013, 0, 1))(.5), +new Date(2012, 6, 2, 1));
101101
assert.strictEqual(d3.interpolate(1, null)(.4), .6); // +null = 0
102102
assert.isNaN(d3.interpolate("blue", null)(.4));

test/interpolate/number-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ suite.addBatch({
88
"interpolateNumber": {
99
topic: load("interpolate/number").expression("d3.interpolateNumber"),
1010
"interpolates numbers": function(interpolate) {
11-
assert.strictEqual(interpolate(2, 12)(.4), 6);
12-
assert.strictEqual(interpolate(2, 12)(.6), 8);
11+
assert.strictEqual(interpolate(2, 12)(.25), 4.5);
12+
assert.strictEqual(interpolate(2, 12)(.75), 9.5);
1313
},
1414
"coerces strings to numbers": function(interpolate) {
15-
assert.strictEqual(interpolate("2", "12")(.4), 6);
15+
assert.strictEqual(interpolate("2", "12")(.25), 4.5);
1616
}
1717
}
1818
});

test/interpolate/string-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ suite.addBatch({
1212
assert.strictEqual(interpolate(" 10/20 30", "50/10 100 ")(.4), "26/16 58 ");
1313
},
1414
"coerces objects to strings": function(interpolate) {
15-
assert.strictEqual(interpolate({toString: function() { return "2px"; }}, {toString: function() { return "12px"; }})(.4), "6px");
15+
assert.strictEqual(interpolate({toString: function() { return "2px"; }}, {toString: function() { return "12px"; }})(.25), "4.5px");
1616
},
1717
"preserves non-numbers in string b": function(interpolate) {
1818
assert.strictEqual(interpolate(" 10/20 30", "50/10 foo ")(.2), "18/18 foo ");

test/scale/linear-test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ suite.addBatch({
152152
assert.inDelta(x.invert(new Date(1990, 6, 2, 13)), .5, 1e-6);
153153
var x = d3.scale.linear().range(["#000", "#fff"]);
154154
assert.isNaN(x.invert("#999"));
155+
var x = d3.scale.linear().range([0, "#fff"]);
156+
assert.isNaN(x.invert("#999"));
157+
assert.isNaN(x.invert(1));
155158
},
156159
"can invert a polylinear descending domain": function(d3) {
157160
var x = d3.scale.linear().domain([4, 2, 1]).range([1, 2, 4]);

0 commit comments

Comments
 (0)