Skip to content

Commit

Permalink
handle misaligned intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Aug 4, 2023
1 parent ffb9637 commit 132ab4f
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/marks/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
11 changes: 6 additions & 5 deletions src/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
123 changes: 123 additions & 0 deletions test/output/timeAxisOrdinalIrregular.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions test/plots/time-axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export async function timeAxisOrdinal() {
});
}

export async function timeAxisOrdinalIrregular() {
const aapl = await d3.csv<any>("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<any>("data/aapl.csv", d3.autoType);
return Plot.plot({
Expand Down

0 comments on commit 132ab4f

Please sign in to comment.