Skip to content

Commit

Permalink
fix(core): fix the previous commit (snapping)
Browse files Browse the repository at this point in the history
Tests failed, and the approach wasn't particularly good
  • Loading branch information
tuner committed Apr 11, 2022
1 parent aaebdc2 commit a0341b0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/genome/scaleIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export default function scaleIndex() {
*/
const scale = /** @type {any} */ (scaleFunction);

scale.invert = (y) => ((y - range[0]) / rangeSpan) * domainSpan + domain[0];
scale.invert = (y) =>
((y - range[0]) / rangeSpan) * domainSpan + domain[0] - align;

// @ts-expect-error
scale.domain = function (_) {
Expand Down
28 changes: 22 additions & 6 deletions packages/core/src/genome/scaleIndex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,37 @@ import scaleIndex from "./scaleIndex";
test("Scale with defaults works as expected", () => {
const scale = scaleIndex();

expect(scale(-1)).toEqual(-1);
expect(scale(0)).toEqual(0);
expect(scale(1)).toEqual(1);
expect(scale(2)).toEqual(2);
// Align is 0.5 by default
expect(scale(-1)).toEqual(-0.5);
expect(scale(0)).toEqual(0.5);
expect(scale(1)).toEqual(1.5);
expect(scale(2)).toEqual(2.5);
});

test("Scale scales correctly with custom domain and range", () => {
const scale = scaleIndex().domain([0, 10]).range([100, 200]);
const scale = scaleIndex().domain([0, 10]).range([100, 200]).align(0.0);

expect(scale(0)).toEqual(100);
expect(scale(10)).toEqual(200);
});

test("Invert works as expected", () => {
const scale = scaleIndex().domain([0, 10]).range([100, 200]);
const scale = scaleIndex().domain([0, 10]).range([100, 200]).align(0.0);

expect(scale.invert(scale(0))).toEqual(0);
expect(scale.invert(scale(5))).toEqual(5);
expect(scale.invert(scale(10))).toEqual(10);
});

test("Scale scales correctly with custom domain, range, and align", () => {
const scale = scaleIndex().domain([0, 10]).range([100, 200]).align(0.5);

expect(scale(0)).toEqual(105);
expect(scale(10)).toEqual(205);
});

test("Invert works as expected with align", () => {
const scale = scaleIndex().domain([0, 10]).range([100, 200]).align(0.5);

expect(scale.invert(scale(0))).toEqual(0);
expect(scale.invert(scale(5))).toEqual(5);
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/view/scaleResolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,16 @@ export default class ScaleResolution {
let newDomain = [...oldDomain];

// @ts-expect-error
const anchor = scale.invert(scaleAnchor);
let anchor = scale.invert(scaleAnchor);

if (this.getScaleProps().reverse) {
pan = -pan;
}

if ("align" in scale) {
anchor += scale.align();
}

// TODO: symlog
switch (scale.type) {
case "linear":
Expand Down

0 comments on commit a0341b0

Please sign in to comment.