Skip to content

Commit c1ca65f

Browse files
committed
fix(tickStep&logticks): prevent division by zero and handle invalid count (#8194)
1 parent 263ad97 commit c1ca65f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/plugins/plot/tickUtils.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable prettier/prettier */
12
import { antisymlog, symlog } from './mathUtils.js';
23

34
const e10 = Math.sqrt(50);
@@ -8,6 +9,10 @@ const e2 = Math.sqrt(2);
89
* Nicely formatted tick steps from d3-array.
910
*/
1011
function tickStep(start, stop, count) {
12+
// fix-1
13+
if (count <= 0 || !Number.isFinite(count)) {
14+
throw new Error('Count must be a positive finite number');
15+
}
1116
const step0 = Math.abs(stop - start) / Math.max(0, count);
1217
let step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10));
1318
const error = step0 / step1;
@@ -63,10 +68,12 @@ export function getLogTicks(start, stop, mainTickCount = 8, secondaryTickCount =
6368
const nextTick = mainTicks[i + 1];
6469
const rangeBetweenMainTicks = nextTick - tick;
6570

71+
// fix-2
72+
const adjustedTickCount = Math.max(1, secondaryTickCount - 2);
6673
const secondaryLogTicks = ticks(
6774
tick + rangeBetweenMainTicks / (secondaryTickCount + 1),
6875
nextTick - rangeBetweenMainTicks / (secondaryTickCount + 1),
69-
secondaryTickCount - 2
76+
adjustedTickCount
7077
).map((n) => symlog(n, 10));
7178

7279
result.push(...secondaryLogTicks);
@@ -76,7 +83,7 @@ export function getLogTicks(start, stop, mainTickCount = 8, secondaryTickCount =
7683

7784
return result;
7885
}
79-
86+
// getLogTicks(0, 100, 4, 2);
8087
/**
8188
* Linear tick generation from d3-array.
8289
*/
@@ -154,3 +161,4 @@ export function getFormattedTicks(newTicks, format) {
154161

155162
return newTicks;
156163
}
164+

0 commit comments

Comments
 (0)