Skip to content

fix misaligned range #1633

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

Merged
merged 2 commits into from
May 25, 2023
Merged
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
11 changes: 8 additions & 3 deletions src/scales/diverging.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
scaleDivergingPow,
scaleDivergingSymlog
} from "d3";
import {positive, negative} from "../defined.js";
import {negative, positive} from "../defined.js";
import {arrayify} from "../options.js";
import {warn} from "../warnings.js";
import {color, registry} from "./index.js";
import {flip, inferDomain, interpolatePiecewise, maybeInterpolator} from "./quantitative.js";
import {quantitativeScheme} from "./schemes.js";
import {registry, color} from "./index.js";
import {inferDomain, maybeInterpolator, flip, interpolatePiecewise} from "./quantitative.js";

function createScaleD(
key,
Expand All @@ -37,7 +39,10 @@ function createScaleD(
}
) {
pivot = +pivot;
domain = arrayify(domain);
let [min, max] = domain;
if (domain.length > 2) warn(`Warning: the diverging ${key} scale domain contains extra elements.`);

if (descending(min, max) < 0) ([min, max] = [max, min]), (reverse = !reverse);
min = Math.min(min, pivot);
max = Math.max(max, pivot);
Expand Down
28 changes: 21 additions & 7 deletions src/scales/quantitative.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ import {
interpolateNumber,
interpolateRgb,
interpolateRound,
min,
max,
median,
min,
quantile,
quantize,
reverse as reverseof,
scaleIdentity,
scaleLinear,
scaleLog,
scalePow,
scaleQuantile,
scaleSymlog,
scaleThreshold,
scaleIdentity,
ticks
} from "d3";
import {positive, negative, finite} from "../defined.js";
import {arrayify, constant, orderof, slice, maybeNiceInterval, maybeRangeInterval} from "../options.js";
import {finite, negative, positive} from "../defined.js";
import {arrayify, constant, maybeNiceInterval, maybeRangeInterval, orderof, slice} from "../options.js";
import {warn} from "../warnings.js";
import {color, length, opacity, radius, registry} from "./index.js";
import {ordinalRange, quantitativeScheme} from "./schemes.js";
import {registry, radius, opacity, color, length} from "./index.js";

export const flip = (i) => (t) => i(1 - t);
const unit = [0, 1];
Expand Down Expand Up @@ -82,6 +83,20 @@ export function createScaleQ(
if (type === "cyclical" || type === "sequential") type = "linear"; // shorthand for color schemes
reverse = !!reverse;

// If an explicit range is specified, ensure that the domain and range have
// the same length; truncate to whichever one is shorter.
if (range !== undefined) {
const n = (domain = arrayify(domain)).length;
const m = (range = arrayify(range)).length;
if (n > m) {
domain = domain.slice(0, m);
warn(`Warning: the ${key} scale domain contains extra elements.`);
} else if (m > n) {
range = range.slice(0, n);
warn(`Warning: the ${key} scale range contains extra elements.`);
}
}

// Sometimes interpolate is a named interpolator, such as "lab" for Lab color
// space. Other times interpolate is a function that takes two arguments and
// is used in conjunction with the range. And other times the interpolate
Expand Down Expand Up @@ -113,8 +128,7 @@ export function createScaleQ(
const [min, max] = extent(domain);
if (min > 0 || max < 0) {
domain = slice(domain);
if (orderof(domain) !== Math.sign(min)) domain[domain.length - 1] = 0;
// [2, 1] or [-2, -1]
if (orderof(domain) !== Math.sign(min)) domain[domain.length - 1] = 0; // [2, 1] or [-2, -1]
else domain[0] = 0; // [1, 2] or [-1, -2]
}
}
Expand Down
94 changes: 94 additions & 0 deletions test/output/colorMisalignedDivergingDomain.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<figure style="max-width: initial;"><svg class="plot-ramp" font-family="system-ui, sans-serif" font-size="10" width="240" height="50" viewBox="0 0 240 50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.plot-ramp {
display: block;
background: white;
height: auto;
height: intrinsic;
max-width: 100%;
overflow: visible;
}

.plot-ramp text {
white-space: pre;
}
</style>
<image x="0" y="18" width="240" height="10" preserveAspectRatio="none" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAABCAYAAAAxWXB3AAAABmJLR0QA/wD/AP+gvaeTAAAA2ElEQVQ4jXWLwZEDMAgDV1BIykv/BRjuYWxjz+XBSFoJffmkC1ziVfvJhduzsfK2emHFzFp+Tr68YS5kwnzm1cl7N7nc9mb3bsjs2pw8vZr/xaf6lbmyI+vMoTjWsjm0Hjt/mCEdPtVA7VeLTc21VzsTyMjGUgJKdxYpkRhIJCJh6puT44FYOSFeTQjy+My9X/4/HbFyY+VHHL78aD+bBcVz85UvFmy293F29/buo/Xx8H3V5cMzk4jYfGqQ2XwkGVF97IuMK89NkDEql45R3Xi6ftWPlw/+AP61XqZckQ8kAAAAAElFTkSuQmCC"></image>
<g transform="translate(0,28)" fill="none" text-anchor="middle" font-variant="tabular-nums">
<g class="tick" opacity="1" transform="translate(24.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">−4</text>
</g>
<g class="tick" opacity="1" transform="translate(72.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">−2</text>
</g>
<g class="tick" opacity="1" transform="translate(120.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">0</text>
</g>
<g class="tick" opacity="1" transform="translate(168.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">2</text>
</g>
<g class="tick" opacity="1" transform="translate(216.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">4</text>
</g>
</g>
</svg><svg class="plot" fill="currentColor" font-family="system-ui, sans-serif" font-size="10" text-anchor="middle" width="640" height="60" viewBox="0 0 640 60" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.plot {
display: block;
background: white;
height: auto;
height: intrinsic;
max-width: 100%;
}

.plot text,
.plot tspan {
white-space: pre;
}
</style>
<g aria-label="x-axis tick" fill="none" stroke="currentColor" transform="translate(25,0)">
<path transform="translate(26,30)" d="M0,0L0,6"></path>
<path transform="translate(80,30)" d="M0,0L0,6"></path>
<path transform="translate(134,30)" d="M0,0L0,6"></path>
<path transform="translate(188,30)" d="M0,0L0,6"></path>
<path transform="translate(242,30)" d="M0,0L0,6"></path>
<path transform="translate(296,30)" d="M0,0L0,6"></path>
<path transform="translate(350,30)" d="M0,0L0,6"></path>
<path transform="translate(404,30)" d="M0,0L0,6"></path>
<path transform="translate(458,30)" d="M0,0L0,6"></path>
<path transform="translate(512,30)" d="M0,0L0,6"></path>
<path transform="translate(566,30)" d="M0,0L0,6"></path>
</g>
<g aria-label="x-axis tick label" transform="translate(25,9.5)">
<text y="0.71em" transform="translate(26,30)">-5</text>
<text y="0.71em" transform="translate(80,30)">-4</text>
<text y="0.71em" transform="translate(134,30)">-3</text>
<text y="0.71em" transform="translate(188,30)">-2</text>
<text y="0.71em" transform="translate(242,30)">-1</text>
<text y="0.71em" transform="translate(296,30)">0</text>
<text y="0.71em" transform="translate(350,30)">1</text>
<text y="0.71em" transform="translate(404,30)">2</text>
<text y="0.71em" transform="translate(458,30)">3</text>
<text y="0.71em" transform="translate(512,30)">4</text>
<text y="0.71em" transform="translate(566,30)">5</text>
</g>
<g aria-label="cell">
<rect x="26" width="49" y="0" height="30" fill="rgb(103, 0, 31)"></rect>
<rect x="80" width="49" y="0" height="30" fill="rgb(172, 32, 47)"></rect>
<rect x="134" width="49" y="0" height="30" fill="rgb(213, 95, 80)"></rect>
<rect x="188" width="49" y="0" height="30" fill="rgb(241, 163, 133)"></rect>
<rect x="242" width="49" y="0" height="30" fill="rgb(251, 215, 196)"></rect>
<rect x="296" width="49" y="0" height="30" fill="rgb(242, 239, 238)"></rect>
<rect x="350" width="49" y="0" height="30" fill="rgb(205, 227, 238)"></rect>
<rect x="404" width="49" y="0" height="30" fill="rgb(143, 194, 221)"></rect>
<rect x="458" width="49" y="0" height="30" fill="rgb(75, 148, 196)"></rect>
<rect x="512" width="49" y="0" height="30" fill="rgb(34, 101, 163)"></rect>
<rect x="566" width="49" y="0" height="30" fill="rgb(5, 48, 97)"></rect>
</g>
<text x="640" y="20" dy="-1em" text-anchor="end" font-family="initial">⚠️<title>1 warning. Please check the console.</title></text>
</svg></figure>
98 changes: 98 additions & 0 deletions test/output/colorMisalignedLinearDomain.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<figure style="max-width: initial;"><svg class="plot-ramp" font-family="system-ui, sans-serif" font-size="10" width="240" height="50" viewBox="0 0 240 50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.plot-ramp {
display: block;
background: white;
height: auto;
height: intrinsic;
max-width: 100%;
overflow: visible;
}

.plot-ramp text {
white-space: pre;
}
</style>
<image x="0" y="18" width="240" height="10" preserveAspectRatio="none" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAABCAYAAAAxWXB3AAAABmJLR0QA/wD/AP+gvaeTAAAAGElEQVQ4jWP8z8Dw/z8DI8MoHsWjeORhABlhAQ9+GPb9AAAAAElFTkSuQmCC"></image>
<g transform="translate(0,28)" fill="none" text-anchor="middle" font-variant="tabular-nums">
<g class="tick" opacity="1" transform="translate(0.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">0</text>
</g>
<g class="tick" opacity="1" transform="translate(48.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">2</text>
</g>
<g class="tick" opacity="1" transform="translate(96.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">4</text>
</g>
<g class="tick" opacity="1" transform="translate(144.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">6</text>
</g>
<g class="tick" opacity="1" transform="translate(192.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">8</text>
</g>
<g class="tick" opacity="1" transform="translate(240.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">10</text>
</g>
</g>
</svg><svg class="plot" fill="currentColor" font-family="system-ui, sans-serif" font-size="10" text-anchor="middle" width="640" height="60" viewBox="0 0 640 60" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.plot {
display: block;
background: white;
height: auto;
height: intrinsic;
max-width: 100%;
}

.plot text,
.plot tspan {
white-space: pre;
}
</style>
<g aria-label="x-axis tick" fill="none" stroke="currentColor" transform="translate(25,0)">
<path transform="translate(26,30)" d="M0,0L0,6"></path>
<path transform="translate(80,30)" d="M0,0L0,6"></path>
<path transform="translate(134,30)" d="M0,0L0,6"></path>
<path transform="translate(188,30)" d="M0,0L0,6"></path>
<path transform="translate(242,30)" d="M0,0L0,6"></path>
<path transform="translate(296,30)" d="M0,0L0,6"></path>
<path transform="translate(350,30)" d="M0,0L0,6"></path>
<path transform="translate(404,30)" d="M0,0L0,6"></path>
<path transform="translate(458,30)" d="M0,0L0,6"></path>
<path transform="translate(512,30)" d="M0,0L0,6"></path>
<path transform="translate(566,30)" d="M0,0L0,6"></path>
</g>
<g aria-label="x-axis tick label" transform="translate(25,9.5)">
<text y="0.71em" transform="translate(26,30)">0</text>
<text y="0.71em" transform="translate(80,30)">1</text>
<text y="0.71em" transform="translate(134,30)">2</text>
<text y="0.71em" transform="translate(188,30)">3</text>
<text y="0.71em" transform="translate(242,30)">4</text>
<text y="0.71em" transform="translate(296,30)">5</text>
<text y="0.71em" transform="translate(350,30)">6</text>
<text y="0.71em" transform="translate(404,30)">7</text>
<text y="0.71em" transform="translate(458,30)">8</text>
<text y="0.71em" transform="translate(512,30)">9</text>
<text y="0.71em" transform="translate(566,30)">10</text>
</g>
<g aria-label="cell">
<rect x="26" width="49" y="0" height="30" fill="rgb(255, 0, 0)"></rect>
<rect x="80" width="49" y="0" height="30" fill="rgb(230, 0, 26)"></rect>
<rect x="134" width="49" y="0" height="30" fill="rgb(204, 0, 51)"></rect>
<rect x="188" width="49" y="0" height="30" fill="rgb(179, 0, 77)"></rect>
<rect x="242" width="49" y="0" height="30" fill="rgb(153, 0, 102)"></rect>
<rect x="296" width="49" y="0" height="30" fill="rgb(128, 0, 128)"></rect>
<rect x="350" width="49" y="0" height="30" fill="rgb(102, 0, 153)"></rect>
<rect x="404" width="49" y="0" height="30" fill="rgb(77, 0, 179)"></rect>
<rect x="458" width="49" y="0" height="30" fill="rgb(51, 0, 204)"></rect>
<rect x="512" width="49" y="0" height="30" fill="rgb(26, 0, 230)"></rect>
<rect x="566" width="49" y="0" height="30" fill="rgb(0, 0, 255)"></rect>
</g>
<text x="640" y="20" dy="-1em" text-anchor="end" font-family="initial">⚠️<title>1 warning. Please check the console.</title></text>
</svg></figure>
98 changes: 98 additions & 0 deletions test/output/colorMisalignedLinearDomainReverse.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<figure style="max-width: initial;"><svg class="plot-ramp" font-family="system-ui, sans-serif" font-size="10" width="240" height="50" viewBox="0 0 240 50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.plot-ramp {
display: block;
background: white;
height: auto;
height: intrinsic;
max-width: 100%;
overflow: visible;
}

.plot-ramp text {
white-space: pre;
}
</style>
<image x="0" y="18" width="240" height="10" preserveAspectRatio="none" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAABCAYAAAAxWXB3AAAABmJLR0QA/wD/AP+gvaeTAAAAGElEQVQ4jWP8z8Dw/z8DI8MoHsWjeORhABlhAQ9+GPb9AAAAAElFTkSuQmCC"></image>
<g transform="translate(0,28)" fill="none" text-anchor="middle" font-variant="tabular-nums">
<g class="tick" opacity="1" transform="translate(0.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">10</text>
</g>
<g class="tick" opacity="1" transform="translate(48.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">8</text>
</g>
<g class="tick" opacity="1" transform="translate(96.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">6</text>
</g>
<g class="tick" opacity="1" transform="translate(144.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">4</text>
</g>
<g class="tick" opacity="1" transform="translate(192.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">2</text>
</g>
<g class="tick" opacity="1" transform="translate(240.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">0</text>
</g>
</g>
</svg><svg class="plot" fill="currentColor" font-family="system-ui, sans-serif" font-size="10" text-anchor="middle" width="640" height="60" viewBox="0 0 640 60" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<style>
.plot {
display: block;
background: white;
height: auto;
height: intrinsic;
max-width: 100%;
}

.plot text,
.plot tspan {
white-space: pre;
}
</style>
<g aria-label="x-axis tick" fill="none" stroke="currentColor" transform="translate(25,0)">
<path transform="translate(26,30)" d="M0,0L0,6"></path>
<path transform="translate(80,30)" d="M0,0L0,6"></path>
<path transform="translate(134,30)" d="M0,0L0,6"></path>
<path transform="translate(188,30)" d="M0,0L0,6"></path>
<path transform="translate(242,30)" d="M0,0L0,6"></path>
<path transform="translate(296,30)" d="M0,0L0,6"></path>
<path transform="translate(350,30)" d="M0,0L0,6"></path>
<path transform="translate(404,30)" d="M0,0L0,6"></path>
<path transform="translate(458,30)" d="M0,0L0,6"></path>
<path transform="translate(512,30)" d="M0,0L0,6"></path>
<path transform="translate(566,30)" d="M0,0L0,6"></path>
</g>
<g aria-label="x-axis tick label" transform="translate(25,9.5)">
<text y="0.71em" transform="translate(26,30)">0</text>
<text y="0.71em" transform="translate(80,30)">1</text>
<text y="0.71em" transform="translate(134,30)">2</text>
<text y="0.71em" transform="translate(188,30)">3</text>
<text y="0.71em" transform="translate(242,30)">4</text>
<text y="0.71em" transform="translate(296,30)">5</text>
<text y="0.71em" transform="translate(350,30)">6</text>
<text y="0.71em" transform="translate(404,30)">7</text>
<text y="0.71em" transform="translate(458,30)">8</text>
<text y="0.71em" transform="translate(512,30)">9</text>
<text y="0.71em" transform="translate(566,30)">10</text>
</g>
<g aria-label="cell">
<rect x="26" width="49" y="0" height="30" fill="rgb(0, 0, 255)"></rect>
<rect x="80" width="49" y="0" height="30" fill="rgb(26, 0, 230)"></rect>
<rect x="134" width="49" y="0" height="30" fill="rgb(51, 0, 204)"></rect>
<rect x="188" width="49" y="0" height="30" fill="rgb(77, 0, 179)"></rect>
<rect x="242" width="49" y="0" height="30" fill="rgb(102, 0, 153)"></rect>
<rect x="296" width="49" y="0" height="30" fill="rgb(128, 0, 128)"></rect>
<rect x="350" width="49" y="0" height="30" fill="rgb(153, 0, 102)"></rect>
<rect x="404" width="49" y="0" height="30" fill="rgb(179, 0, 77)"></rect>
<rect x="458" width="49" y="0" height="30" fill="rgb(204, 0, 51)"></rect>
<rect x="512" width="49" y="0" height="30" fill="rgb(230, 0, 26)"></rect>
<rect x="566" width="49" y="0" height="30" fill="rgb(255, 0, 0)"></rect>
</g>
<text x="640" y="20" dy="-1em" text-anchor="end" font-family="initial">⚠️<title>1 warning. Please check the console.</title></text>
</svg></figure>
Loading