Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exp scale #48

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export {
point as scalePoint
} from "./src/band";

export {
default as scaleExp
} from "./src/exp";

export {
default as scaleIdentity
} from "./src/identity";
Expand Down
45 changes: 45 additions & 0 deletions src/exp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import constant from "./constant";
import {linearish} from "./linear";
import {logp, powp} from "./log";
import {default as continuous, copy, deinterpolateLinear} from "./continuous";

export default function exp() {
var base = Math.E,
log = logp(base),
pow = powp(base),
scale = continuous(deinterpolate, interpolate).range([1, base]),
domain = scale.domain;

function rescale() {
log = logp(base);
pow = powp(base);
return scale;
}

function deinterpolate(a, b) {
a = pow(a);
b = pow(b) - a;
if (!b) return constant(b);
return function(x) {
return (pow(x) - a) / b;
};
}

function interpolate(a, b) {
a = pow(a);
b = pow(b) - a;
return function(t) {
return log(b * t + a);
};
}

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

scale.copy = function() {
return copy(scale, exp().base(base));
};

return linearish(scale);
};
6 changes: 3 additions & 3 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ function pow10(x) {
return isFinite(x) ? +("1e" + x) : x < 0 ? 0 : x;
}

function powp(base) {
export function powp(base) {
return base === 10 ? pow10
: base === Math.E ? Math.exp
: function(x) { return Math.pow(base, x); };
}

function logp(base) {
export function logp(base) {
return base === Math.E ? Math.log
: base === 10 && Math.log10
|| base === 2 && Math.log2
|| (base = Math.log(base), function(x) { return Math.log(x) / base; });
}
};

function reflect(f) {
return function(x) {
Expand Down
66 changes: 66 additions & 0 deletions test/exp-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var tape = require("tape"),
scale = require("../");

require("./inDelta");

tape("scaleExp() has the expected defaults", function(test) {
var s = scale.scaleExp();
test.deepEqual(s.domain(), [0, 1]);
test.deepEqual(s.range(), [1, Math.E]);
test.equal(s.clamp(), false);
test.equal(s.base(), Math.E);
test.end();
});

tape("exp(x) maps a domain value x to a range value y", function(test) {
var s = scale.scaleExp();
test.equal(s(0), 1);
test.equal(s(1), Math.E);
test.equal(s(2), Math.E * Math.E);

s = s.base(10);
s = s.domain([0, 5]).range([1e0, 1e5]);
test.equal(s(0), 1e0);
test.equal(s(1), 1e1);
test.equal(s(2), 1e2);
test.equal(s(3), 1e3);
test.equal(s(4), 1e4);
test.equal(s(5), 1e5);

s = s.domain([0, 5]).range([1e5, 1e10]);
test.equal(s(0), 1e5);
test.equal(s(1), 1e6);
test.equal(s(2), 1e7);
test.equal(s(3), 1e8);
test.equal(s(4), 1e9);
test.equal(s(5), 1e10);

s = s.domain([5, 10]).range([1e2, 1e7]);
test.equal(s(5), 1e2);
test.equal(s(6), 1e3);
test.equal(s(7), 1e4);
test.equal(s(8), 1e5);
test.equal(s(9), 1e6);
test.equal(s(10), 1e7);

test.end();
});

tape("exp(x) maps an empty domain to the range start", function(test) {
test.equal(scale.scaleExp().domain([0, 0]).range([1, 2])(0), 1);
test.equal(scale.scaleExp().domain([0, 0]).range([2, 1])(1), 2);
test.end();
});

tape("exp.invert(y) maps a range value y to a domain value x", function(test) {
var s = scale.scaleExp();
test.inDelta(s.invert(1), 0);
test.inDelta(s.invert(Math.E), 1);
test.inDelta(s.invert(Math.E*Math.E), 2);

s.base(10);
test.inDelta(s.invert(0.01), -2);
test.inDelta(s.invert(1), 0);
test.inDelta(s.invert(100), 2);
test.end();
});