Skip to content

Commit

Permalink
Merge pull request #167 from VisActor/fix/continuous-clamp
Browse files Browse the repository at this point in the history
fix: fix clamp of comtinuous scale
  • Loading branch information
xile611 authored Feb 21, 2024
2 parents d86c752 + f51deb7 commit 3302839
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/vscale/__tests__/clamp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { LinearScale } from '../src/linear-scale';

test('linear(x) will not ignores extra range values if the domain is smaller than the range', function () {
const scale = new LinearScale();

scale.clamp(true).domain([-10, 10]).range([100, 200]);
expect(scale.scale(0)).toBe(150);
expect(scale.scale(-50)).toBe(100);
expect(scale.scale(50)).toBe(200);

scale.domain([-100, 100]);
expect(scale.scale(0)).toBe(150);
expect(scale.scale(-50)).toBe(125);
expect(scale.scale(50)).toBe(175);
});
5 changes: 4 additions & 1 deletion packages/vscale/src/continuous-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class ContinuousScale extends BaseScale implements IContinuousScale {
protected _domainValidator?: (val: number) => boolean;

_clamp?: (x: number) => number;
_autoClamp?: boolean;

constructor(transformer: TransformType = identity, untransformer: TransformType = identity) {
super();
Expand Down Expand Up @@ -154,7 +155,7 @@ export class ContinuousScale extends BaseScale implements IContinuousScale {
n = rangeLength;
}

if (this._clamp === undefined) {
if (this._autoClamp) {
this._clamp = clamper(domain[0], domain[n - 1]);
}
this._piecewise = n > 2 ? polymap : bimap;
Expand All @@ -172,8 +173,10 @@ export class ContinuousScale extends BaseScale implements IContinuousScale {
return this._clamp !== identity;
}
if (f) {
this._autoClamp = false;
this._clamp = f;
} else {
this._autoClamp = !!_;
this._clamp = _ ? undefined : identity;
}

Expand Down

0 comments on commit 3302839

Please sign in to comment.