Skip to content

feat: add utc scale type#507

Merged
gka merged 7 commits intomainfrom
feat/utc-scale-type
Mar 8, 2026
Merged

feat: add utc scale type#507
gka merged 7 commits intomainfrom
feat/utc-scale-type

Conversation

@ljodea
Copy link
Copy Markdown
Collaborator

@ljodea ljodea commented Feb 28, 2026

Summary

  • Adds type: 'utc' support for x/y scales, fixing timezone-shifted tick rendering when dates are stored as UTC (common for APIs, CSVs with autoType)
  • Uses d3's scaleUtc and UTC-aware intervals/formatting (timeZone: 'UTC' in Intl.DateTimeFormat)
  • Introduces isTemporalScale() helper to eliminate === 'time' checks across 8 call sites, covering both 'time' and 'utc'

Usage

<Plot x={{ type: 'utc', domain: [new Date('2025-06-15'), new Date('2025-09-15')] }}>
    <AxisX />
    <Line {data} x="date" y="value" />
</Plot>

What changed

File Change
src/lib/types/scale.ts Add 'utc' to ScaleType union
src/lib/helpers/typeChecks.ts Add isTemporalScale() helper
src/lib/constants.ts Add 'utc' to VALID_SCALE_TYPES.x and .y
src/lib/helpers/autoScales.ts Import scaleUtc, add to map, update nice condition
src/lib/helpers/autoTicks.ts Route string intervals to UTC variants when scaleType === 'utc'
src/lib/helpers/autoTimeFormat.ts Pass timeZone: 'UTC' for UTC scales
src/lib/marks/AxisX.svelte Handle 'utc' type in tick formatting
src/lib/marks/AxisY.svelte Handle 'utc' type in tick formatting
src/lib/helpers/scales.ts Suppress auto-title for UTC scales (same as time)
src/lib/marks/helpers/Regression.svelte Convert epoch→Date for UTC scales

Not changed (and why)

  • inferScaleType() — Keeps returning 'time' for dates. UTC must be explicit (type: 'utc'). No breaking change.
  • shift.ts, group.ts, bin.ts — Use numeric intervals or maybeTimeInterval for relative shifts. Separate concern.

Test plan

  • VALID_SCALE_TYPES.x/y include 'utc'
  • isTemporalScale() returns correct values for 'time', 'utc', 'linear', 'band'
  • inferScaleType returns 'time' (not 'utc') for date values — regression check
  • maybeInterval('month', 'utc') returns UTC interval (checked via intervalType symbol)
  • autoTimeFormat with UTC scale formats dates in UTC timezone
  • Rendering <Plot x={{ type: 'utc' }}> with <AxisX> produces visible tick labels
  • UTC ticks with time interval ('3 months') render correctly
  • All 697 existing tests pass, svelte-check clean, prettier clean

Closes #120, closes #214

🤖 Generated with Claude Code

When dates are stored as UTC (common for APIs and CSVs with autoType),
the existing `time` scale renders them in the local timezone, causing
visible shifts — e.g. midnight UTC appears as 01:00 BST. This adds
explicit `type: 'utc'` support using d3's scaleUtc and UTC-aware
intervals/formatting, fixing tick alignment for UTC data.

Closes #120, closes #214

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@netlify
Copy link
Copy Markdown

netlify bot commented Feb 28, 2026

Deploy Preview for svelteplot ready!

Name Link
🔨 Latest commit 3175d0d
🔍 Latest deploy log https://app.netlify.com/projects/svelteplot/deploys/69ad86fa35a7df0008a0ad84
😎 Deploy Preview https://deploy-preview-507--svelteplot.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@gka
Copy link
Copy Markdown
Contributor

gka commented Mar 2, 2026

Thanks for adding new features! One important thing missing here is documentation. The scales page should ideally mention the utc scale and include a practical example showing the difference between time and utc.

@gka gka added the enhancement New feature or request label Mar 2, 2026
@gka gka added this to the 1.0 milestone Mar 2, 2026
@ljodea
Copy link
Copy Markdown
Collaborator Author

ljodea commented Mar 2, 2026

Thanks for adding new features! One important thing missing here is documentation. The scales page should ideally mention the utc scale and include a practical example showing the difference between time and utc.

Fair point. I will add that as soon as I am able.

gka and others added 6 commits March 2, 2026 21:58
Fix intro text that incorrectly stated SveltePlot defaults to UTC
(it defaults to `time`), and add a new UTC scale subsection with a
side-by-side comparison example showing the difference between `time`
and `utc` scale types.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Resolve conflict in autoTimeFormat.ts: keep utc parameter support
from feature branch and formatter variable naming from main.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gka
Copy link
Copy Markdown
Contributor

gka commented Mar 8, 2026

@ljodea I tweaked your example a bit so that it also shows a difference for me (assuming it looked different for you). I'm on UTC+1 so an intra-day axis range is a safer choice

image

@gka gka merged commit 2a632b8 into main Mar 8, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] axis ticks don't align with data points Support for Observable Plot's utc scale.

2 participants