package.src.pow.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of d3-scale Show documentation
Show all versions of d3-scale Show documentation
Encodings that map abstract data to visual representation.
The newest version!
import {linearish} from "./linear.js";
import {copy, identity, transformer} from "./continuous.js";
import {initRange} from "./init.js";
function transformPow(exponent) {
return function(x) {
return x < 0 ? -Math.pow(-x, exponent) : Math.pow(x, exponent);
};
}
function transformSqrt(x) {
return x < 0 ? -Math.sqrt(-x) : Math.sqrt(x);
}
function transformSquare(x) {
return x < 0 ? -x * x : x * x;
}
export function powish(transform) {
var scale = transform(identity, identity),
exponent = 1;
function rescale() {
return exponent === 1 ? transform(identity, identity)
: exponent === 0.5 ? transform(transformSqrt, transformSquare)
: transform(transformPow(exponent), transformPow(1 / exponent));
}
scale.exponent = function(_) {
return arguments.length ? (exponent = +_, rescale()) : exponent;
};
return linearish(scale);
}
export default function pow() {
var scale = powish(transformer());
scale.copy = function() {
return copy(scale, pow()).exponent(scale.exponent());
};
initRange.apply(scale, arguments);
return scale;
}
export function sqrt() {
return pow.apply(null, arguments).exponent(0.5);
}