Skip to content

Commit 26b33ff

Browse files
committed
fix(x_domain): fix x value asc sorting using numbers
1 parent e34f0ae commit 26b33ff

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/lib/series/domains/x_domain.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { identity } from '../../utils/commons';
1+
import { compareByValueAsc, identity } from '../../utils/commons';
22
import { computeContinuousDataDomain, computeOrdinalDataDomain } from '../../utils/domain';
33
import { ScaleType } from '../../utils/scales/scales';
44
import { BasicSeriesSpec } from '../specs';
@@ -49,14 +49,17 @@ export function mergeXDomain(
4949
* to display a bar chart in a linear scale.
5050
*/
5151
export function findMinInterval(xValues: number[]): number | null {
52-
const sortedValues = xValues.slice().sort();
52+
if (xValues.length === 1) {
53+
return 1;
54+
}
55+
const sortedValues = xValues.slice().sort(compareByValueAsc);
5356
const sortedValuesLength = sortedValues.length - 1;
5457
let i;
5558
let minInterval = null;
5659
for (i = 0; i < sortedValuesLength; i++) {
5760
const current = sortedValues[i];
5861
const next = sortedValues[i + 1];
59-
const interval = next - current;
62+
const interval = Math.abs(next - current);
6063
if (minInterval === null) {
6164
minInterval = interval;
6265
} else {

src/lib/utils/commons.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export function identity<T>(value: T): T {
22
return value;
33
}
4+
5+
export function compareByValueAsc(firstEl: number, secondEl: number): number {
6+
return firstEl - secondEl;
7+
}

0 commit comments

Comments
 (0)