Repository navigation
fix: keep millisecond resolution when x is a Date on a datetime axis - #5277
junedchhipa merged 2 commits into
Conversation
Data.js ran a Date x through x.toString() before parsing it back, and Date.prototype.toString() has second resolution, so the milliseconds were dropped. Two points 500ms apart landed on the same x and drew as a vertical line. A numeric x never took that branch, which is why the same data worked when passed as raw epoch values. A Date already carries the exact epoch, so read it directly.
The datetime axis now keeps millisecond resolution for a Date x, but ApexAxisChartSeries still declared `x: string | number`, so the exact code the fix enables did not compile. Widen it to include Date, and cover the three x forms in the types consumer test. Also corrects the comment above the branch, which claimed a JS date object is not supported, and adds the trailing comma prettier wants on the new call.
junedchhipa
left a comment
There was a problem hiding this comment.
Thanks, this is a well diagnosed fix and the write-up made it easy to verify.
Confirmed the root cause: Date.parse coerces its argument through Date.prototype.toString(), so the round trip really does truncate.
Date.parse(new Date(1748924002500)) -> 1748924002000
A few things I checked before merging:
getTime()matches the existing helper. Since the new branch skipsgetTimeStamp, I wanted to be sure it does not sidestepxaxis.labels.datetimeUTC. That option's branch (new Date(new Date(x).toISOString().substr(0, 25)).getTime()) turns out to be epoch-identity for in-range dates, verified under a non-UTCTZfor both UTC and offset-bearing inputs. So aDateand its equivalent string still land on the same value, which is the property the issue was really about.- This was the only lossy path.
handleFormat2DArrayalready doesnew Date(x).getTime(), and the siblingconvertedCatToNumericbranch already passes theDatestraight intodt.parseDate. Line 334 was the one place an x value was routed through a string. - The test is a real guard. Reverting only
Data.jsfails exactly the milliseconds and parity cases (expected [1748924002000, 1748924002000, …] to deeply equal [1748924002000, 1748924002500, …]) and leaves the date-string case green, matching your description. Full unit suite passes with the patch.
I pushed one follow-up commit rather than send it back to you, since it is small:
ApexAxisChartSeriesdeclaredx: string | number, so the exact code this fix enables still failed to compile for TypeScript users. Widened tostring | number | Dateand covered the three x forms intests/types/consumer-patterns.ts.- The comment above the branch claimed a JS date object is not supported, which your change makes untrue.
- Added the trailing comma prettier wants on the new call. (The two other prettier deviations in
Data.jsare pre-existing and left alone.)
One adjacent thing I deliberately did not fold in, for anyone who finds this later: on a non-datetime axis a Date x still goes through parseFloat(x) and becomes NaN, and dt.isValidDate(new Date('garbage')) throws because parseDate's fallback assumes a string. Both predate this PR and belong in their own change.
|
Thanks for the thorough merge review, and for pushing the type widening yourself rather than sending it back — I took the two adjacent items you flagged: #5279. Both still reproduced on
I reverted each of the two fixes on its own to confirm neither test is vacuous (2 failed / 3 passed each way, 5 passed with both), kept the pre-existing prettier deviations in those files untouched as you did, and checked that |
Two problems the review of #5277 named and left for their own change. A Date x on a numeric or category axis went through parseFloat(), which reads the Date through toString(), so every point became NaN even though the surrounding lines had just set dataFormatXNumeric and isXNumeric. A Date's numeric value is its epoch, the same one the datetime branch directly above already reads. isValidDate(new Date('garbage')) threw instead of answering false: parseDate()'s salvage pass calls .replace() on its argument, and an invalid Date has no such method. A single bad x took the whole render down with a TypeError. Only a string has an alternate spelling to try, so anything else is simply not a date. Co-Authored-By: Claude Opus 5 <[email protected]>
Closes #5054.
It's the
toString()inData.js:Date.prototype.toString()only has second resolution, so parsing its output back loses the milliseconds:Which is why two points 500ms apart end up on the same x and draw as a vertical line. A numeric x never reaches that branch, and that's what the reporter noticed when switching to raw numbers fixed it.
A Date already holds the exact epoch, so this reads it directly instead of round-tripping through a string. Note
parseDate(x)on the line above (theisXStringbranch) already preserves it, sinceDate.parsecoerces internally andgetTimeStampthen doesnew Date(x).getTime()— only the.toString()call is lossy.Added
tests/unit/datetime-date-object-x.spec.jscovering the milliseconds, Date-vs-number parity, and that date strings still parse. Against the unpatchedData.jsthe first two fail and the string one passes, which is the right shape.