diff --git a/src/marks/axis.js b/src/marks/axis.js
index 8e966bd09cc..39b287520fd 100644
--- a/src/marks/axis.js
+++ b/src/marks/axis.js
@@ -547,6 +547,7 @@ function axisMark(mark, k, ariaLabel, data, options, initialize) {
const [start, stop] = extent(data);
if (interval) {
data = maybeRangeInterval(interval, type).range(start, stop);
+ data = data.map(scale.interval.floor, scale.interval);
} else {
if (ticks === undefined) {
const [min, max] = extent(scale.range());
diff --git a/src/time.js b/src/time.js
index 8870e4d4de9..4cf8fe84c24 100644
--- a/src/time.js
+++ b/src/time.js
@@ -145,11 +145,12 @@ export function formatTimeTicks(scale, data = scale.domain(), ticks, anchor) {
// the ticks show the field that is changing. If the ticks are not available,
// fallback to an approximation based on the desired number of ticks.
function getTimeTicksInterval(scale, data, ticks) {
- const medianStep = median(pairs(data, (a, b) => Math.abs(b - a) || NaN));
- if (medianStep > 0) return formats[bisector(([, step]) => step).right(formats, medianStep, 1, formats.length) - 1][0];
- const [start, stop] = extent(scale.domain());
- const count = typeof ticks === "number" ? ticks : 10;
- const step = Math.abs(stop - start) / count;
+ let step = median(pairs(data, (a, b) => Math.abs(b - a) || NaN));
+ if (!(step > 0)) {
+ const [start, stop] = extent(scale.domain());
+ const count = typeof ticks === "number" ? ticks : 10;
+ step = Math.abs(stop - start) / count;
+ }
return formats[bisector(([, step]) => Math.log(step)).center(formats, Math.log(step))][0];
}
diff --git a/test/output/timeAxisOrdinalIrregular.svg b/test/output/timeAxisOrdinalIrregular.svg
new file mode 100644
index 00000000000..07a68339c61
--- /dev/null
+++ b/test/output/timeAxisOrdinalIrregular.svg
@@ -0,0 +1,123 @@
+
\ No newline at end of file
diff --git a/test/plots/time-axis.ts b/test/plots/time-axis.ts
index f06a58bcd1a..6ef22686612 100644
--- a/test/plots/time-axis.ts
+++ b/test/plots/time-axis.ts
@@ -91,6 +91,14 @@ export async function timeAxisOrdinal() {
});
}
+export async function timeAxisOrdinalIrregular() {
+ const aapl = await d3.csv("data/aapl.csv", d3.autoType);
+ return Plot.plot({
+ x: {interval: "4 weeks", ticks: "year"},
+ marks: [Plot.barY(aapl, Plot.groupX({y: "median", title: "min"}, {title: "Date", x: "Date", y: "Close"}))]
+ });
+}
+
export async function timeAxisOrdinalTicks() {
const aapl = await d3.csv("data/aapl.csv", d3.autoType);
return Plot.plot({