Skip to content

Commit 3ad90cc

Browse files
committed
Force ascending axes in scatter vis
1 parent 7cceba9 commit 3ad90cc

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

apps/storybook/src/Utilities.mdx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,23 @@ getAxisValues([0, 1], 3); // Throws error as 2 != 3
170170

171171
#### getAxisDomain
172172

173-
Find the ascending or descending domain of an axis given its values and scale. Optionally extend the domain by a given factor.
173+
Find the ascending or descending domain of an axis given its values and scale. Optionally extend the domain by a given factor or force ascending domain.
174174

175175
```ts
176176
getAxisDomain(
177177
axisValues: number[],
178-
scaleType: ScaleType = ScaleType.Linear,
179-
extensionFactor = 0
178+
opts: {
179+
scaleType?: AxisScaleType, // default: ScaleType.Linear
180+
extensionFactor?: number, // default: 0
181+
forceAscending?: boolean, // default: false
182+
}
180183
): Domain | undefined
181184

182185
getAxisDomain([-1, 0, 1]); // [-1, 1]
183-
getAxisDomain([4, 2, 0, -2], ScaleType.Log); // [4, 2]);
184-
getAxisDomain([4, 2, 0], ScaleType.Linear, 0.5); // [6, -2])
186+
getAxisDomain([4, 2, 0]); // [4, 0]
187+
getAxisDomain([4, 2, 0], { scaleType: ScaleType.Log }); // [4, 2]);
188+
getAxisDomain([4, 2, 0], { extensionFactor: 0.5 }); // [6, -2])
189+
getAxisDomain([4, 2, 0], { forceAscending: true }); // [0, 4])
185190
```
186191

187192
#### toTypedNdArray

packages/lib/src/vis/line/LineVis.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ function LineVis(props: PropsWithChildren<Props>) {
8989
const abscissas = useAxisValues(abscissaValue, dataArray.size);
9090
const abscissaToIndex = useValueToIndexScale(abscissas, true);
9191

92-
const abscissaDomain = useAxisDomain(abscissas, abscissaScaleType, 0.01);
92+
const abscissaDomain = useAxisDomain(abscissas, {
93+
scaleType: abscissaScaleType,
94+
extensionFactor: 0.01,
95+
});
9396
assertDefined(abscissaDomain, 'Abscissas have undefined domain');
9497

9598
const dataDomain = useMemo(() => {

packages/lib/src/vis/scatter/ScatterVis.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,20 @@ function ScatterVis(props: PropsWithChildren<Props>) {
6969
label: ordinateLabel,
7070
scaleType: ordinateScaleType,
7171
} = ordinateParams;
72-
7372
assertLength(abscissas, dataArray.size, 'abscissa');
7473
assertLength(ordinates, dataArray.size, 'ordinates');
7574

76-
const abscissaDomain = useAxisDomain(abscissas, abscissaScaleType, 0.01);
75+
const abscissaDomain = useAxisDomain(abscissas, {
76+
scaleType: abscissaScaleType,
77+
extensionFactor: 0.01,
78+
forceAscending: true,
79+
});
80+
const ordinateDomain = useAxisDomain(ordinates, {
81+
scaleType: ordinateScaleType,
82+
extensionFactor: 0.01,
83+
forceAscending: true,
84+
});
7785
assertDefined(abscissaDomain, 'Abscissas have undefined domain');
78-
const ordinateDomain = useAxisDomain(ordinates, ordinateScaleType, 0.01);
7986
assertDefined(ordinateDomain, 'Ordinates have undefined domain');
8087

8188
const {

packages/lib/src/vis/utils.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,32 @@ describe('getAxisDomain', () => {
405405
});
406406

407407
it('should respect given scale', () => {
408-
expect(getAxisDomain([-1, 0, 1], ScaleType.Log)).toEqual([1, 1]);
409-
expect(getAxisDomain([4, 2, 0, -2], ScaleType.Log)).toEqual([4, 2]);
408+
expect(getAxisDomain([-1, 0, 1], { scaleType: ScaleType.Log })).toEqual([
409+
1, 1,
410+
]);
411+
expect(getAxisDomain([4, 2, 0, -2], { scaleType: ScaleType.Log })).toEqual([
412+
4, 2,
413+
]);
410414
});
411415

412416
it('should allow extending domain', () => {
413-
expect(getAxisDomain([4, 2, 0], ScaleType.Linear, 0.5)).toEqual([6, -2]);
414-
expect(getAxisDomain([-1, 0, 1], ScaleType.Log, 1)).toEqual([0.1, 10]);
417+
expect(
418+
getAxisDomain([4, 2, 0], {
419+
scaleType: ScaleType.Linear,
420+
extensionFactor: 0.5,
421+
}),
422+
).toEqual([6, -2]);
423+
424+
expect(
425+
getAxisDomain([-1, 0, 1], {
426+
scaleType: ScaleType.Log,
427+
extensionFactor: 1,
428+
}),
429+
).toEqual([0.1, 10]);
430+
});
431+
432+
it('should allow forcing ascending domain', () => {
433+
expect(getAxisDomain([4, 2, 0], { forceAscending: true })).toEqual([0, 4]);
415434
});
416435

417436
it('should return undefined when values array is empty', () => {

packages/lib/src/vis/utils.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,25 @@ function isDescending(array: NumArray): boolean {
357357

358358
export function getAxisDomain(
359359
axisValues: NumArray,
360-
scaleType: AxisScaleType = ScaleType.Linear,
361-
extensionFactor = 0,
360+
opts: {
361+
scaleType?: AxisScaleType;
362+
extensionFactor?: number;
363+
forceAscending?: boolean;
364+
} = {},
362365
): Domain | undefined {
366+
const {
367+
scaleType = ScaleType.Linear,
368+
extensionFactor = 0,
369+
forceAscending = false,
370+
} = opts;
371+
363372
const rawDomain = getDomain(axisValues, { scaleType });
364373
if (!rawDomain) {
365374
return undefined;
366375
}
367376

368377
const extendedDomain = extendDomain(rawDomain, extensionFactor, scaleType);
369-
return isDescending(axisValues)
378+
return isDescending(axisValues) && !forceAscending
370379
? [extendedDomain[1], extendedDomain[0]]
371380
: extendedDomain;
372381
}

0 commit comments

Comments
 (0)