Closed gabeb03 closed 3 months ago
scaleBand
really should just be treating things as string
s, so if you verified that the domain is correct my guess would be the date formatter is off - e.g., when it gets 06-01-2024
, it looks like it's creating a new Date
and maybe in that process it's throwing the timezone off and going back to May? you could possibly verify by returning the string directly in the formatDate
function?
That was it!
Apparently, it's an issue with the way the Date object in JavaScript interprets the date string. When you pass a date string in the format "YYYY-MM-DD" to the Date constructor, it is treated as a UTC date by default, not as a local date.
Here's how I fixed formatDate
so that it converted the date to the local time zone:
const formatDate = (value: Date | NumberLike | string): string => {
if (value instanceof Date) {
return format(value);
} else if (typeof value === "string") {
// When Date is given the year, month & day as separate parameters, it returns the Date in the local timezone... weird
const [year, month, day] = value.split("-").map(Number);
return format(new Date(year, month - 1, day));
}
return String(value.valueOf());
};
Thanks so William, I really appreciate how you tirelessly help devs like me use the tool :) ! I absolutely love visx; it's such a joy to use.
I'm making a bar chart using visx, and I'm running into this issue where the dates on the x-axis are one day behind the actual data:
The domain that was passed to the bar chart component was from May 31st, but you can see that the leftmost tick is May 30. This may be related to this question, but I'm looking to use
band
scales instead ofscaleUtc
, which was the solution for that question.Here's how I am setting up the xScale:
dateDomain
is returning the correct array (ifstart
is June 1, the first element of the returned array is June 1). Here's what I wrote to render the bar chart:Does anything jump out at you?