Skip to content

Commit 5fd2f92

Browse files
y axis tick label fixes + distribution labels
1 parent b354e09 commit 5fd2f92

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/infertypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ export function infertypes(
387387
}
388388
}
389389
if (info.type == ParamType.NUMERICLOG) {
390-
info.ticks_format = d3.format(".1e");
390+
info.ticks_format = ".1e";
391391
}
392392
return info;
393393
}

src/plotxy.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,21 +229,37 @@ export class PlotXY extends React.Component<PlotXYProps, PlotXYState> {
229229
if (
230230
!paramDef ||
231231
paramDef.type !== ParamType.NUMERICLOG ||
232-
typeof scale.ticks !== "function"
232+
typeof scale.domain !== "function"
233233
) {
234234
return null;
235235
}
236-
const ticks = scale.ticks(approxTickCount);
237-
if (!Array.isArray(ticks)) {
236+
const domain = scale.domain();
237+
if (!Array.isArray(domain) || domain.length < 2) {
238+
return null;
239+
}
240+
const dMin = Number(domain[0]);
241+
const dMax = Number(domain[domain.length - 1]);
242+
if (!Number.isFinite(dMin) || !Number.isFinite(dMax) || dMin <= 0 || dMax <= 0) {
238243
return null;
239244
}
240-
const majorTicks = ticks.filter((tick: number) => {
241-
if (!Number.isFinite(tick) || tick <= 0) {
242-
return false;
245+
// Generate powers of 10 spanning the domain, including boundary powers
246+
// so that e.g. domain [1.05, 99.3] produces ticks [1, 10, 100]
247+
const minExp = Math.floor(Math.log10(dMin));
248+
const maxExp = Math.ceil(Math.log10(dMax));
249+
const majorTicks: number[] = [];
250+
for (let exp = minExp; exp <= maxExp; exp++) {
251+
majorTicks.push(Math.pow(10, exp));
252+
}
253+
// Thin out if there are too many powers of 10 for the available space
254+
if (majorTicks.length > approxTickCount && majorTicks.length > 2) {
255+
const step = Math.ceil(majorTicks.length / approxTickCount);
256+
const thinned: number[] = [majorTicks[0]];
257+
for (let i = step; i < majorTicks.length - 1; i += step) {
258+
thinned.push(majorTicks[i]);
243259
}
244-
const lg = Math.log10(tick);
245-
return Math.abs(lg - Math.round(lg)) < 1e-10;
246-
});
260+
thinned.push(majorTicks[majorTicks.length - 1]);
261+
return thinned;
262+
}
247263
return majorTicks.length > 1 ? majorTicks : null;
248264
}
249265
function redraw_axis() {

0 commit comments

Comments
 (0)