Skip to content

Commit

Permalink
first try for symlog.ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Apr 8, 2019
1 parent 8ef7906 commit aea855f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/log.js
Expand Up @@ -139,7 +139,5 @@ export default function log() {
return copy(scale, log()).base(scale.base());
};

initRange.apply(scale, arguments);

return scale;
return initRange.apply(scale, arguments);
}
4 changes: 1 addition & 3 deletions src/ordinal.js
Expand Up @@ -40,7 +40,5 @@ export default function ordinal() {
return ordinal(domain, range).unknown(unknown);
};

initRange.apply(scale, arguments);

return scale;
return initRange.apply(scale, arguments);
}
4 changes: 1 addition & 3 deletions src/pow.js
Expand Up @@ -40,9 +40,7 @@ export default function pow() {
return copy(scale, pow()).exponent(scale.exponent());
};

initRange.apply(scale, arguments);

return scale;
return initRange.apply(scale, arguments);
}

export function sqrt() {
Expand Down
96 changes: 93 additions & 3 deletions src/symlog.js
@@ -1,3 +1,5 @@
import {format} from "d3-format";
import nice from "./nice";
import {linearish} from "./linear";
import {copy, transformer} from "./continuous";
import {initRange} from "./init";
Expand All @@ -15,13 +17,101 @@ function transformSymexp(c) {
}

export function symlogish(transform) {
var c = 1, scale = transform(transformSymlog(c), transformSymexp(c));
var c = 1, forward = transformSymlog(c), invert = transformSymexp(c),
scale = linearish(transform(forward, invert));

function rescale() {
transform(forward = transformSymlog(c), invert = transformSymexp(c));
return scale;
}

scale.constant = function(_) {
return arguments.length ? transform(transformSymlog(c = +_), transformSymexp(c)) : c;
return arguments.length ? (c = +_, rescale()) : c;
};

function roundexp2_5(v) {
if (v == 0) return 0;
var r = v < 0;
if (r) v= -v;
var s = invert(v),
a = Math.log10(s),
a2 = Math.log10(s / 2),
a5 = Math.log10(s / 5),
b = -100 + Math.floor(100 + a),
b2 = -100 + Math.floor(100 + a2),
b5 = -100 + Math.floor(100 + a5),
c = Math.pow(10, b);
if (b5 === b) c *= 5;
else if (b2 === b) c *= 2;
return r ? -c : c;
}

scale.ticks = function(count) {
var d = scale.domain(),
u = d[0],
v = d[d.length - 1],
r;

if (isNaN(n) || n <= 0 || n === Infinity) return [];

if (r = v < u) i = u, u = v, v = i;

var i = forward(u),
j = forward(v),
n = count == null ? 10 : +count,
z, zN = [], zP = [], m, s0, rs, rv, s;

var step = (j-i) / n;

// negatives
if (i < 0) {
m = Math.min(0, j);
s0 = -Infinity;
for (s = i; s < m; s += step) {
rs = roundexp2_5(s);
rv = forward(rs);
if (rv - s0 >= step && rv <= -step) {
zN.push(rs);
s0 = rv;
}
}
}

// 0
if (i * j <= 0 && n) zN.push(0);

// positives
if (j > 0) {
m = Math.max(0, i);
s0 = +Infinity;
for (s = j; s > m; s -= step) {
rs = roundexp2_5(s);
rv = forward(rs);
if (rv - s0 <= -step && rv >= step) {
zP.unshift(rs);
s0 = rv;
}
}
}

z = [...zN, ...zP];
return r ? z.reverse() : z;
};

scale.tickFormat = function(count, specifier) {
if (specifier === undefined) specifier = "~g";
if (typeof specifier !== "function") specifier = format(specifier);
return specifier;
};

scale.nice = function() {
return scale.domain(nice(scale.domain(), {
floor: function(x) { return roundexp2_5(forward(x)); },
ceil: function(x) { return roundexp2_5(forward(x)); }
}));
};

return linearish(scale);
return scale;
}

export default function symlog() {
Expand Down

0 comments on commit aea855f

Please sign in to comment.