:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(charts): infer date axis interval from the smallest gap (#110667)
posthog[bot]4c47f2518eac
diff --git a/packages/quill/packages/charts/src/utils/dates.test.ts b/packages/quill/packages/charts/src/utils/dates.test.tsindex 56f5e342f4c5..de473d3d5bc7 100644--- a/packages/quill/packages/charts/src/utils/dates.test.ts+++ b/packages/quill/packages/charts/src/utils/dates.test.ts@@ -47,6 +47,12 @@ describe('createXAxisTickCallback', () => {allDays: ['2025-04-07', '2025-04-14', '2025-04-21'],expected: ['Apr 7', 'Apr 14', 'Apr 21'],},+ {+ scenario: 'inferred day interval when an early outlier precedes daily points',+ interval: undefined,+ allDays: ['2026-08-07', '2026-09-25', '2026-09-26', '2026-09-27'],+ expected: ['Aug 7', 'Sep 25', 'Sep 26', 'Sep 27'],+ },{scenario: 'inferred hour interval from 1 hour gaps',interval: undefined,diff --git a/packages/quill/packages/charts/src/utils/dates.ts b/packages/quill/packages/charts/src/utils/dates.tsindex 84b88df616d7..fdd2df89054a 100644--- a/packages/quill/packages/charts/src/utils/dates.ts+++ b/packages/quill/packages/charts/src/utils/dates.ts@@ -201,18 +201,24 @@ function isDateOnlyLabel(label: string | number): boolean {return typeof label === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(label)}+const HOUR_MS = 60 * 60 * 1000+const DAY_MS = 24 * HOUR_MS+function inferInterval(parsedDates: Dayjs[], dateOnlyLabels: boolean): TimeInterval {- if (parsedDates.length < 2) {+ // Use the smallest gap, not the first one, so one early outlier in a sparse daily series+ // does not make the whole axis look monthly.+ const gapMs = smallestWallClockGapMs(parsedDates)+ if (gapMs === null) {return 'day'}- const diffHours = Math.abs(parsedDates[1].diff(parsedDates[0], 'hour'))+ const diffHours = gapMs / HOUR_MSif (diffHours < 1) {return 'minute'}if (!dateOnlyLabels && diffHours < 24) {return 'hour'}- const diffDays = Math.abs(parsedDates[1].diff(parsedDates[0], 'day'))+ const diffDays = Math.floor(gapMs / DAY_MS)if (diffDays >= 300) {return 'year'}@@ -228,6 +234,23 @@ function inferInterval(parsedDates: Dayjs[], dateOnlyLabels: boolean): TimeInterreturn 'day'}+// Wall-clock gaps ignore UTC offset changes, so a daily gap across a DST switch stays 24 hours.+function smallestWallClockGapMs(parsedDates: Dayjs[]): number | null {+ let smallest: number | null = null+ for (let i = 1; i < parsedDates.length; i++) {+ const prev = parsedDates[i - 1]+ const curr = parsedDates[i]+ if (!prev.isValid() || !curr.isValid()) {+ continue+ }+ const gap = Math.abs(curr.valueOf() - prev.valueOf() + (curr.utcOffset() - prev.utcOffset()) * 60 * 1000)+ if (gap > 0 && (smallest === null || gap < smallest)) {+ smallest = gap+ }+ }+ return smallest+}+function buildDayStartIndices(parsedDates: Dayjs[]): Set<number> {const indices = new Set<number>()let prevDateStr = ''