| 1 | import {interpolate, interpolateRound} from "d3-interpolate";
|
|---|
| 2 | import {identity} from "./continuous.js";
|
|---|
| 3 | import {initInterpolator} from "./init.js";
|
|---|
| 4 | import {linearish} from "./linear.js";
|
|---|
| 5 | import {loggish} from "./log.js";
|
|---|
| 6 | import {symlogish} from "./symlog.js";
|
|---|
| 7 | import {powish} from "./pow.js";
|
|---|
| 8 |
|
|---|
| 9 | function transformer() {
|
|---|
| 10 | var x0 = 0,
|
|---|
| 11 | x1 = 1,
|
|---|
| 12 | t0,
|
|---|
| 13 | t1,
|
|---|
| 14 | k10,
|
|---|
| 15 | transform,
|
|---|
| 16 | interpolator = identity,
|
|---|
| 17 | clamp = false,
|
|---|
| 18 | unknown;
|
|---|
| 19 |
|
|---|
| 20 | function scale(x) {
|
|---|
| 21 | return x == null || isNaN(x = +x) ? unknown : interpolator(k10 === 0 ? 0.5 : (x = (transform(x) - t0) * k10, clamp ? Math.max(0, Math.min(1, x)) : x));
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | scale.domain = function(_) {
|
|---|
| 25 | return arguments.length ? ([x0, x1] = _, t0 = transform(x0 = +x0), t1 = transform(x1 = +x1), k10 = t0 === t1 ? 0 : 1 / (t1 - t0), scale) : [x0, x1];
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | scale.clamp = function(_) {
|
|---|
| 29 | return arguments.length ? (clamp = !!_, scale) : clamp;
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | scale.interpolator = function(_) {
|
|---|
| 33 | return arguments.length ? (interpolator = _, scale) : interpolator;
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | function range(interpolate) {
|
|---|
| 37 | return function(_) {
|
|---|
| 38 | var r0, r1;
|
|---|
| 39 | return arguments.length ? ([r0, r1] = _, interpolator = interpolate(r0, r1), scale) : [interpolator(0), interpolator(1)];
|
|---|
| 40 | };
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | scale.range = range(interpolate);
|
|---|
| 44 |
|
|---|
| 45 | scale.rangeRound = range(interpolateRound);
|
|---|
| 46 |
|
|---|
| 47 | scale.unknown = function(_) {
|
|---|
| 48 | return arguments.length ? (unknown = _, scale) : unknown;
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | return function(t) {
|
|---|
| 52 | transform = t, t0 = t(x0), t1 = t(x1), k10 = t0 === t1 ? 0 : 1 / (t1 - t0);
|
|---|
| 53 | return scale;
|
|---|
| 54 | };
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | export function copy(source, target) {
|
|---|
| 58 | return target
|
|---|
| 59 | .domain(source.domain())
|
|---|
| 60 | .interpolator(source.interpolator())
|
|---|
| 61 | .clamp(source.clamp())
|
|---|
| 62 | .unknown(source.unknown());
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | export default function sequential() {
|
|---|
| 66 | var scale = linearish(transformer()(identity));
|
|---|
| 67 |
|
|---|
| 68 | scale.copy = function() {
|
|---|
| 69 | return copy(scale, sequential());
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | return initInterpolator.apply(scale, arguments);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | export function sequentialLog() {
|
|---|
| 76 | var scale = loggish(transformer()).domain([1, 10]);
|
|---|
| 77 |
|
|---|
| 78 | scale.copy = function() {
|
|---|
| 79 | return copy(scale, sequentialLog()).base(scale.base());
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | return initInterpolator.apply(scale, arguments);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | export function sequentialSymlog() {
|
|---|
| 86 | var scale = symlogish(transformer());
|
|---|
| 87 |
|
|---|
| 88 | scale.copy = function() {
|
|---|
| 89 | return copy(scale, sequentialSymlog()).constant(scale.constant());
|
|---|
| 90 | };
|
|---|
| 91 |
|
|---|
| 92 | return initInterpolator.apply(scale, arguments);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | export function sequentialPow() {
|
|---|
| 96 | var scale = powish(transformer());
|
|---|
| 97 |
|
|---|
| 98 | scale.copy = function() {
|
|---|
| 99 | return copy(scale, sequentialPow()).exponent(scale.exponent());
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 | return initInterpolator.apply(scale, arguments);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | export function sequentialSqrt() {
|
|---|
| 106 | return sequentialPow.apply(null, arguments).exponent(0.5);
|
|---|
| 107 | }
|
|---|