Skip to content

Commit ee3fea1

Browse files
committed
better ticks
1 parent c8e41ad commit ee3fea1

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

docs/summary-table.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function table(data, options = {}) {
7373

7474
function summary({name, type, values}) {
7575
const {width: w, height} = this.getBoundingClientRect();
76-
const width = Math.min(200, (w ?? 80) - 10);
76+
const width = Math.min(200, (w ?? 80));
7777
let chart;
7878

7979
// Count values, NaN, nulls, distinct
@@ -145,18 +145,21 @@ function summary({name, type, values}) {
145145
}
146146
// temporal, quantitative
147147
else {
148+
const niceK = 5;
149+
const isDate = type.startsWith("Date");
148150
const thresholds = Math.max(10, Math.min(50, d3.thresholdScott(values, min, max))); // TODO optimize thresholdScott
151+
const domain = d3.nice(min, max, niceK);
152+
if (domain.length > 2) domain.splice(1, domain.length - 2);
153+
const ticks = isDate ? d3.utcTicks(...domain, niceK) : d3.ticks(...domain, niceK);
154+
if (ticks.length > 2) ticks.splice(1, ticks.length - 2);
149155
chart = Plot.plot({
150156
width,
151157
height,
152158
style: "overflow: visible;",
153-
x: {
154-
round: true,
155-
nice: true
156-
},
159+
x: {round: true, nice: niceK},
157160
y: {axis: null},
158-
marginLeft: 9,
159-
marginRight: 9,
161+
marginLeft: 4,
162+
marginRight: 12,
160163
marginTop: 0,
161164
marginBottom: 13,
162165
marks: [
@@ -171,9 +174,9 @@ function summary({name, type, values}) {
171174
inset: 0,
172175
insetRight: 1,
173176
})),
174-
min * max <= 0 ? Plot.ruleX([0]) : [],
177+
domain[0] * domain[1] <= 0 ? Plot.ruleX([0]) : [],
175178
Plot.ruleY([0]),
176-
Plot.axisX({tickSpacing: 41, tickSize: 3, tickPadding: 2, fontSize: 8, ...(!type.startsWith("Date") && Math.max(Math.abs(min), Math.abs(max)) >= 1e5 && {tickFormat: "s"})}),
179+
Plot.axisX(ticks, {tickSize: 3, tickPadding: 2, fontSize: 8, ...(!isDate && {tickFormat: "s"})}),
177180
]
178181
});
179182
}

0 commit comments

Comments
 (0)