Skip to content

Commit

Permalink
Implement radial.unknown and convenience constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Sep 2, 2019
1 parent e7b8f15 commit 1e751e8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/continuous.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ export function transformer() {
};
}

export default function continuous(transform, untransform) {
return transformer()(transform, untransform);
export default function continuous() {
return transformer()(identity, identity);
}
4 changes: 2 additions & 2 deletions src/linear.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ticks, tickIncrement} from "d3-array";
import continuous, {copy, identity} from "./continuous.js";
import continuous, {copy} from "./continuous.js";
import {initRange} from "./init.js";
import tickFormat from "./tickFormat.js";

Expand Down Expand Up @@ -60,7 +60,7 @@ export function linearish(scale) {
}

export default function linear() {
var scale = continuous(identity, identity);
var scale = continuous();

scale.copy = function() {
return copy(scale, linear());
Expand Down
28 changes: 18 additions & 10 deletions src/radial.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import linear, {linearish} from "./linear.js";
import continuous from "./continuous.js";
import {initRange} from "./init.js";
import {linearish} from "./linear.js";
import number from "./number.js";

function square(x) {
return (x < 0 ? -1 : 1) * x * x;
return Math.sign(x) * x * x;
}

function unsquare(x) {
return (x < 0 ? -1 : 1) * Math.sqrt(Math.abs(x));
return Math.sign(x) * Math.sqrt(Math.abs(x));
}

export default function radial() {
var squared = linear(),
var squared = continuous(),
range = [0, 1],
round = false;
round = false,
unknown;

function scale(x) {
var y = unsquare(squared(x));
return round ? Math.round(y) : y;
return isNaN(y) ? unknown : round ? Math.round(y) : y;
}

scale.invert = function(y) {
Expand All @@ -39,13 +42,18 @@ export default function radial() {
return arguments.length ? (squared.clamp(_), scale) : squared.clamp();
};

scale.unknown = function(_) {
return arguments.length ? (unknown = _, scale) : unknown;
};

scale.copy = function() {
return radial()
.domain(squared.domain())
.range(range)
return radial(squared.domain(), range)
.round(round)
.clamp(squared.clamp());
.clamp(squared.clamp())
.unknown(unknown);
};

initRange.apply(scale, arguments);

return linearish(scale);
}
4 changes: 2 additions & 2 deletions src/time.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {bisector, tickStep} from "d3-array";
import {timeYear, timeMonth, timeWeek, timeDay, timeHour, timeMinute, timeSecond, timeMillisecond} from "d3-time";
import {timeFormat} from "d3-time-format";
import continuous, {copy, identity} from "./continuous.js";
import continuous, {copy} from "./continuous.js";
import {initRange} from "./init.js";
import nice from "./nice.js";

Expand All @@ -22,7 +22,7 @@ function number(t) {
}

export function calendar(year, month, week, day, hour, minute, second, millisecond, format) {
var scale = continuous(identity, identity),
var scale = continuous(),
invert = scale.invert,
domain = scale.domain;

Expand Down
30 changes: 30 additions & 0 deletions test/radial-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ tape("scaleRadial() has the expected defaults", function(test) {
test.end();
});

tape("scaleRadial(range) sets the range", function(test) {
var s = scale.scaleRadial([100, 200]);
test.deepEqual(s.domain(), [0, 1]);
test.deepEqual(s.range(), [100, 200]);
test.equal(s(0.5), 158.11388300841898);
test.end();
});

tape("scaleRadial(domain, range) sets the range", function(test) {
var s = scale.scaleRadial([1, 2], [10, 20]);
test.deepEqual(s.domain(), [1, 2]);
test.deepEqual(s.range(), [10, 20]);
test.equal(s(1.5), 15.811388300841896);
test.end();
});

tape("radial(x) maps a domain value x to a range value y", function(test) {
test.equal(scale.scaleRadial().range([1, 2])(0.5), 1.5811388300841898);
test.end();
Expand Down Expand Up @@ -48,3 +64,17 @@ tape("radial(x) can map a bilinear domain with two values to the corresponding r
test.equal(s.invert( 1.5), 3.25);
test.end();
});

tape("radial(NaN) returns undefined", function(test) {
var s = scale.scaleRadial();
test.strictEqual(s(NaN), undefined);
test.strictEqual(s(undefined), undefined);
test.strictEqual(s("foo"), undefined);
test.strictEqual(s({}), undefined);
test.end();
});

tape("radial.unknown(unknown)(NaN) returns the specified unknown value", function(test) {
test.strictEqual(scale.scaleRadial().unknown("foo")(NaN), "foo");
test.end();
});

0 comments on commit 1e751e8

Please sign in to comment.