Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Display relative date on charts on hover

Blue Mousef5c968739246
diff --git a/web/app/pages/Project/View/components/MainChart.tsx b/web/app/pages/Project/View/components/MainChart.tsx
index c0d1a2a38..941ce2d38 100644
--- a/web/app/pages/Project/View/components/MainChart.tsx
+++ b/web/app/pages/Project/View/components/MainChart.tsx
@@ -3,6 +3,8 @@ import React, { useEffect, useMemo } from 'react'
import BillboardChart from '~/ui/BillboardChart'
+import { useViewProjectContext } from '../ViewProject'
+
import { useChartManager } from './ChartManager'
interface MainChartProps {
@@ -23,6 +25,7 @@ export const MainChart = ({
xGridLines,
}: MainChartProps) => {
const { registerChart, unregisterChart } = useChartManager()
+ const { timezone } = useViewProjectContext()
const mergedDeps = useMemo(
() => deps || [options, dataNames],
@@ -47,6 +50,7 @@ export const MainChart = ({
onReady={handleChartReady}
deps={mergedDeps}
xGridLines={xGridLines}
+ relativeTimeTimezone={timezone}
/>
)
}
diff --git a/web/app/ui/BillboardChart.tsx b/web/app/ui/BillboardChart.tsx
index 46d3ac2ae..7e94bbcdd 100644
--- a/web/app/ui/BillboardChart.tsx
+++ b/web/app/ui/BillboardChart.tsx
@@ -7,9 +7,12 @@ import billboard, {
} from 'billboard.js'
import cx from 'clsx'
import React, { useEffect, useMemo, useRef } from 'react'
+import { useTranslation } from 'react-i18next'
import { trackError } from '~/utils/analytics'
+import { attachRelativeTimeBadge } from './chartRelativeTime'
+
interface BillboardChartProps {
options: ChartOptions
dataNames?: Record<string, string>
@@ -17,6 +20,7 @@ interface BillboardChartProps {
onReady?: (chart: Chart | null) => void
deps?: any[]
xGridLines?: GridLineOptions[]
+ relativeTimeTimezone?: string
}
const BillboardChart = ({
@@ -26,7 +30,9 @@ const BillboardChart = ({
onReady,
deps,
xGridLines,
+ relativeTimeTimezone,
}: BillboardChartProps) => {
+ const { i18n } = useTranslation()
const containerRef = useRef<HTMLDivElement | null>(null)
const chartRef = useRef<Chart | null>(null)
const isDestroyingRef = useRef(false)
@@ -136,6 +142,18 @@ const BillboardChart = ({
}
}
+ const removeRelativeTimeBadge =
+ relativeTimeTimezone &&
+ wrappedOptions.axis?.x?.type === 'timeseries' &&
+ !wrappedOptions.axis.rotated
+ ? attachRelativeTimeBadge(
+ containerRef.current,
+ wrappedOptions,
+ relativeTimeTimezone,
+ i18n.language,
+ )
+ : undefined
+
// Translation tools (Google Translate, Immersive Translate etc.) can
// rewrite the SVG internals mid-session, which makes billboard.js throw
// during re-render. Keep the page alive instead of crashing to the
@@ -167,6 +185,7 @@ const BillboardChart = ({
if (onReady) onReady(chart)
return () => {
+ removeRelativeTimeBadge?.()
const chartToDestroy = chartRef.current
if (!chartToDestroy) return
@@ -190,7 +209,7 @@ const BillboardChart = ({
if (onReady) onReady(null)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, mergedDeps)
+ }, [...mergedDeps, relativeTimeTimezone, i18n.language])
useEffect(() => {
const chart = chartRef.current
diff --git a/web/app/ui/chartRelativeTime.ts b/web/app/ui/chartRelativeTime.ts
new file mode 100644
index 000000000..678b79be3
--- /dev/null
+++ b/web/app/ui/chartRelativeTime.ts
@@ -0,0 +1,77 @@
+import type { ChartOptions } from 'billboard.js'
+import dayjs from 'dayjs'
+import timezonePlugin from 'dayjs/plugin/timezone'
+import utc from 'dayjs/plugin/utc'
+
+import { getRelativeDateIfPossible } from '~/utils/date'
+
+dayjs.extend(utc)
+dayjs.extend(timezonePlugin)
+
+export const attachRelativeTimeBadge = (
+ container: HTMLElement,
+ options: ChartOptions,
+ timezone: string,
+ language: string,
+) => {
+ const badge = document.createElement('span')
+ badge.className =
+ 'pointer-events-none absolute z-10 max-w-full truncate rounded-full border border-gray-200 bg-white px-2 py-0.5 text-xs font-medium leading-4 text-gray-900 shadow-sm dark:border-slate-700 dark:bg-slate-900 dark:text-gray-100'
+ badge.hidden = true
+
+ let frame = 0
+ const hide = () => {
+ cancelAnimationFrame(frame)
+ badge.hidden = true
+ }
+
+ const tooltip = options.tooltip
+ options.tooltip = {
+ ...tooltip,
+ onshown(data) {
+ tooltip?.onshown?.call(this, data)
+ hide()
+
+ const x: unknown = data.find((point) => point?.x != null)?.x
+ if (!(x instanceof Date) || !Number.isFinite(x.getTime())) return
+
+ const date = dayjs.tz(dayjs(x).format('YYYY-MM-DD HH:mm:ss'), timezone)
+ badge.textContent =
+ getRelativeDateIfPossible(date.toISOString(), language) || ''
+
+ frame = requestAnimationFrame(() => {
+ const focus = container.querySelector<SVGLineElement>(
+ 'line.bb-xgrid-focus',
+ )
+ const axis = container.querySelector<SVGGElement>('.bb-axis-x')
+ if (!focus || !axis) return
+
+ if (!badge.isConnected) container.appendChild(badge)
+ badge.hidden = false
+
+ const bounds = container.getBoundingClientRect()
+ const focusBounds = focus.getBoundingClientRect()
+ const axisBounds = axis.getBoundingClientRect()
+ const left = focusBounds.left - bounds.left - badge.offsetWidth / 2
+
+ badge.style.left = `${Math.max(0, Math.min(left, container.clientWidth - badge.offsetWidth))}px`
+ badge.style.top = `${axisBounds.top - bounds.top + 6}px`
+ })
+ },
+ onhide(data) {
+ hide()
+ tooltip?.onhide?.call(this, data)
+ },
+ }
+
+ const onresize = options.onresize
+ options.onresize = function () {
+ hide()
+ onresize?.call(this)
+ }
+
+ return () => {
+ hide()
+ badge.remove()
+ }
+}