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