Skip to content

fix: keep millisecond resolution when x is a Date on a datetime axis - #5277

Merged
junedchhipa merged 2 commits into
apexcharts:mainfrom
aron-intframe:fix/datetime-date-object-milliseconds
Aug 17, 2026
Merged

junedchhipa merged 2 commits into
apexcharts:mainfrom
aron-intframe:fix/datetime-date-object-milliseconds

Conversation

@aron-intframe

Copy link
Copy Markdown
Contributor

Closes #5054.

It's the toString() in Data.js:

this.twoDSeriesX.push(dt.parseDate(x.toString()))

Date.prototype.toString() only has second resolution, so parsing its output back loses the milliseconds:

new Date(1748924002500).toString()
// 'Tue Jun 03 2025 11:13:22 GMT+0700 (Indochina Time)'   <- .500 gone

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 (the isXString branch) already preserves it, since Date.parse coerces internally and getTimeStamp then does new Date(x).getTime() — only the .toString() call is lossy.

Added tests/unit/datetime-date-object-x.spec.js covering the milliseconds, Date-vs-number parity, and that date strings still parse. Against the unpatched Data.js the first two fail and the string one passes, which is the right shape.

aron-intframe and others added 2 commits August 15, 2026 19:01
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 junedchhipa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 skips getTimeStamp, I wanted to be sure it does not sidestep xaxis.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-UTC TZ for both UTC and offset-bearing inputs. So a Date and its equivalent string still land on the same value, which is the property the issue was really about.
  • This was the only lossy path. handleFormat2DArray already does new Date(x).getTime(), and the sibling convertedCatToNumeric branch already passes the Date straight into dt.parseDate. Line 334 was the one place an x value was routed through a string.
  • The test is a real guard. Reverting only Data.js fails 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:

  • ApexAxisChartSeries declared x: string | number, so the exact code this fix enables still failed to compile for TypeScript users. Widened to string | number | Date and covered the three x forms in tests/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.js are 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.

@junedchhipa
junedchhipa merged commit 9c11c62 into apexcharts:main Aug 17, 2026
5 checks passed
@aron-intframe

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough merge review, and for pushing the type widening yourself rather than sending it back — x: string | number would indeed have made the fix unusable from TypeScript.

I took the two adjacent items you flagged: #5279.

Both still reproduced on main (b0dc6af):

× reads the Date as its epoch on a numeric axis
    AssertionError: expected [ NaN, NaN ] to deeply equal [ 1748924002000, 1748924003000 ]
× makes isValidDate answer false rather than throw
    AssertionError: expected [Function] to not throw an error but 'TypeError: dateStr.replace is not a f…' was thrown

parseFloat reads a Date through toString(), which starts with a weekday, so the branch that had just set dataFormatXNumeric and isXNumeric got NaN. And parseDate's salvage pass calls .replace() on whatever it was handed, so an invalid Date threw out of isValidDate and took the render down instead of falling through to the category path.

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 typecheck reports the same 23 pre-existing Drilldown.js errors with and without the change. Full unit suite is 3181 passing.

junedchhipa pushed a commit that referenced this pull request Aug 27, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line chart with datetime axis loses millisond resolution when x values are dates

2 participants