Fix date tooltip: show human-readable dates instead of ms timestamps#66
Conversation
…olumns Agent-Logs-Url: https://github.com/yihui/gglite/sessions/98035fd2-ca4b-4d69-aada-3aebecff8ff7 Co-authored-by: yihui <163582+yihui@users.noreply.github.com>
yihui
left a comment
There was a problem hiding this comment.
another problem is, tooltips for points still show timestamps:
data.frame(date = Sys.Date() + 1:10, value = 1:10) |>
g2(value ~ date) |> mark_line() |> mark_point()
when you hover over a point, the tooltip shows both date and value, and date is presented as large integers
i also discovered a different pre-existing problem, not related to this PR but very serious: the x-axis tick marks are not aligned with marks in the plot when x axis is date or time---the marks are off to the left of their corresponding tick marks, e.g., the mark for Jan 2 is to the left of the Jan 2 tickmark; i tried a normal plot with numeric x axis, and the plot marks aligned perfectly with axis tick marks:
data.frame(date = 1:10, value = 1:10) |>
g2(value ~ date) |> mark_line() |> mark_point()
pls do browser testing and provide screenshots as proof for your fix following copilot instructions, also wait for CI
| col_js = xfun::tojson(x_col) | ||
| fmt_fn = if (inherits(chart$data[[x_col]], 'Date')) | ||
| xfun::js(paste0( | ||
| '(d) => new Date(d[', col_js, ']).toISOString().slice(0, 10)')) |
There was a problem hiding this comment.
.slice(0, 10) is not robust
| '(d) => new Date(d[', col_js, ']).toISOString().slice(0, 10)')) | |
| '(d) => new Date(d[', col_js, ']).toLocaleDateString()')) |
There was a problem hiding this comment.
Done in 504b34b — changed to toLocaleDateString() for Date columns.
| '(d) => new Date(d[', col_js, ']).toISOString()', | ||
| '.replace("T", " ").slice(0, 19)')) |
There was a problem hiding this comment.
similarly:
| '(d) => new Date(d[', col_js, ']).toISOString()', | |
| '.replace("T", " ").slice(0, 19)')) | |
| '(d) => new Date(d[', col_js, ']).toLocaleString()')) |
There was a problem hiding this comment.
Done in 504b34b — changed to toLocaleString() for POSIXt columns.
| - Date/POSIXt columns used as the x axis now show human-readable dates in | ||
| tooltips (e.g., `2026-01-01`) instead of raw millisecond timestamps. | ||
|
|
There was a problem hiding this comment.
follow instructions! no news for v0.1! remember this forever!
There was a problem hiding this comment.
Removed in 504b34b. Sorry about that — won't happen again.
- Use toLocaleDateString()/toLocaleString() per code review suggestions - Add utc: TRUE to auto-detected time scale to fix tick alignment in non-UTC timezones - Inject x item valueFormatter for tooltip2d marks (point, cell, heatmap, etc.) so date items also show readable dates instead of ms timestamps - Remove NEWS.md entry for v0.1 (no individual changes for initial release) - Update tests to match new behavior Agent-Logs-Url: https://github.com/yihui/gglite/sessions/bbb7c4f2-4ec5-44fd-861b-f53d6e8913c2 Co-authored-by: yihui <163582+yihui@users.noreply.github.com>
Fixed in 504b34b. Three things addressed:
Screenshots showing the fixed |


When a
Date/POSIXtcolumn is mapped to x, the tooltip displayed raw millisecond epoch values (e.g.1767484800000) instead of readable date strings — both in the tooltip title and in tooltip items for point marks.Why not just send ISO strings?
The ms-timestamp conversion in
annotate_dfmust stay. G2'sinferDomainQcomputes the scale domain via+d(JS numeric coercion), and+"2026-01-01" === NaN— so sending ISO strings collapses all points to a single position. This was the original fix in PR #21.Fix
build_confignow:Tooltip title: Auto-injects a JS tooltip title formatter on each child mark when the x channel is a
Date/POSIXtcolumn and the mark doesn't already have a customtooltip.title:Date→(d) => new Date(d["col"]).toLocaleDateString()POSIXt→(d) => new Date(d["col"]).toLocaleString()Tooltip items for point marks: For marks that use
tooltip2d(point, cell, heatmap, image, link, beeswarm), also injects avalueFormatteron the x channel item so the date appears formatted in the items list too, not just the title. User-settooltip$itemsis respected.UTC time scale: Adds
utc: trueto the auto-detected time scale. This switches D3 to its UTC-based time scale so tick positions are at UTC midnight — matching our UTC-based ms timestamps. Without this, in non-UTC timezones D3's local-time ticks were offset from the marks.The column name is escaped via
xfun::tojson()before interpolation. User-settooltip$titleandtooltip = FALSEare both respected.