Skip to content
Snippets Groups Projects
Unverified Commit ed5190c2 authored by Paul Rosenzweig's avatar Paul Rosenzweig Committed by GitHub
Browse files

Fix bug with timeseries spacing of a single month (#11646)

parent 46c01293
No related branches found
No related tags found
No related merge requests found
......@@ -41,10 +41,12 @@ const timeseriesScale = (
};
function domainForEvenlySpacedMonths(domain, { timezone, interval }) {
return wrapValues(
ticksForRange(domain, { count: 1, timezone, interval }),
domain,
);
const ticks = ticksForRange(domain, { count: 1, timezone, interval });
// if the domain only contains one month, return the domain untouched
if (ticks.length < 2) {
return domain;
}
return wrapValues(ticks, domain);
}
function rangeForEvenlySpacedMonths(range, domain, { timezone, interval }) {
......@@ -53,6 +55,10 @@ function rangeForEvenlySpacedMonths(range, domain, { timezone, interval }) {
.domain(domain.map(toInt))
.range(range);
const ticks = ticksForRange(domain, { count: 1, timezone, interval });
// if the domain only contains one month, return the range untouched
if (ticks.length < 2) {
return range;
}
const [start, end] = firstAndLast(ticks).map(t => plainScale(toInt(t)));
const step = (end - start) / (ticks.length - 1);
const monthPoints = d3.range(ticks.length).map(i => start + i * step);
......
......@@ -257,6 +257,25 @@ describe("timeseriesScale", () => {
).toEqual([0, 10, 20, 30]);
});
it("should work for one 'evenly spaced' month", () => {
const scale = timeseriesScale({
interval: "month",
count: 1,
timezone: "Etc/UTC",
})
.domain([
moment("2018-11-15T00:00:00.000Z"),
moment("2018-12-15T00:00:00.000Z"),
])
.range([0, 30]);
expect(
["2018-11-15", "2018-12-15"].map(d =>
scale(moment(`${d}T00:00:00.000Z`)),
),
).toEqual([0, 30]);
});
it("should not evenly space years", () => {
// 2020 is a leap year and 2019 is not. With the total width set to the
// total number of days, each year should have one pixel per day.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment