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!

chart zooming

Blue Mouseb69c6c8a00fb
diff --git a/web/app/pages/Project/View/ViewProject.helpers.tsx b/web/app/pages/Project/View/ViewProject.helpers.tsx
index 82b3a4b56..f2a9f0459 100644
--- a/web/app/pages/Project/View/ViewProject.helpers.tsx
+++ b/web/app/pages/Project/View/ViewProject.helpers.tsx
@@ -1,6 +1,6 @@
import { LanguageIcon, ArrowRightCircleIcon } from '@heroicons/react/24/outline'
import type { ChartOptions, GridLineOptions } from 'billboard.js'
-import { area, areaSpline, spline, bar, line } from 'billboard.js'
+import { area, areaSpline, spline, bar, line, zoom } from 'billboard.js'
import * as d3 from 'd3'
import dayjs from 'dayjs'
import filesaver from 'file-saver'
@@ -756,6 +756,7 @@ const getSettingsSession = (
timeFormat: string,
rotateXAxis: boolean,
chartType: string,
+ onZoom?: (domain: [Date, Date]) => void,
): ChartOptions => {
const xAxisSize = _size(chart.x)
@@ -777,6 +778,12 @@ const getSettingsSession = (
sessionDuration: 'y2',
},
},
+ zoom: {
+ enabled: zoom(),
+ type: 'drag',
+ onzoom: onZoom,
+ resetButton: false, // We render a custom button that also resets pageflow
+ },
transition: {
duration: 500,
},
diff --git a/web/app/pages/Project/View/ViewProject.tsx b/web/app/pages/Project/View/ViewProject.tsx
index f27c33052..f34a6f208 100644
--- a/web/app/pages/Project/View/ViewProject.tsx
+++ b/web/app/pages/Project/View/ViewProject.tsx
@@ -439,6 +439,8 @@ const ViewProject = () => {
const activePSID = useMemo(() => {
return searchParams.get('psid')
}, [searchParams])
+ const [zoomedTimeRange, setZoomedTimeRange] = useState<[Date, Date] | null>(null)
+ const [sessionChartInstance, setSessionChartInstance] = useState<Chart | null>(null)
// errors
const [errorsSkip, setErrorsSkip] = useState(0)
@@ -2461,6 +2463,21 @@ const ViewProject = () => {
</div>
)
+ const handleChartZoom = useCallback((domain: [Date, Date] | null) => {
+ setZoomedTimeRange(domain)
+ }, [])
+
+ const handleSessionChartReady = useCallback((chartInst: Chart | null) => {
+ setSessionChartInstance(chartInst)
+ }, [])
+
+ const resetSessionChartZoom = () => {
+ if (sessionChartInstance) {
+ sessionChartInstance.unzoom()
+ }
+ setZoomedTimeRange(null)
+ }
+
if (authLoading || !project) {
return (
<>
@@ -3288,16 +3305,28 @@ const ViewProject = () => {
</div>
{activeSession?.details ? <SessionDetails details={activeSession?.details} /> : null}
{!_isEmpty(activeSession?.chart) ? (
- <SessionChart
- chart={activeSession?.chart}
- timeBucket={activeSession?.timeBucket}
- timeFormat={timeFormat}
- rotateXAxis={rotateXAxis}
- chartType={chartType}
- dataNames={dataNames}
- />
+ <div className='relative'>
+ <SessionChart
+ chart={activeSession?.chart}
+ timeBucket={activeSession?.timeBucket}
+ timeFormat={timeFormat}
+ rotateXAxis={rotateXAxis}
+ chartType={chartType}
+ dataNames={dataNames}
+ onZoom={handleChartZoom}
+ onChartReady={handleSessionChartReady}
+ />
+ {zoomedTimeRange ? (
+ <button
+ onClick={resetSessionChartZoom}
+ className='absolute top-2 right-0 z-10 rounded border bg-white px-2 py-1 text-xs text-gray-800 hover:bg-gray-100 dark:border-slate-700 dark:bg-slate-800 dark:text-gray-200 hover:dark:bg-slate-700'
+ >
+ {t('project.resetZoom')}
+ </button>
+ ) : null}
+ </div>
) : null}
- <Pageflow pages={activeSession?.pages} timeFormat={timeFormat} />
+ <Pageflow pages={activeSession?.pages} timeFormat={timeFormat} zoomDomain={zoomedTimeRange} />
{_isEmpty(activeSession) && sessionLoading ? <Loader /> : null}
{activeSession !== null &&
_isEmpty(activeSession?.chart) &&
diff --git a/web/app/pages/Project/View/components/Pageflow.tsx b/web/app/pages/Project/View/components/Pageflow.tsx
index d35e1add2..84b08503f 100644
--- a/web/app/pages/Project/View/components/Pageflow.tsx
+++ b/web/app/pages/Project/View/components/Pageflow.tsx
@@ -1,4 +1,5 @@
import clsx from 'clsx'
+import _isEmpty from 'lodash/isEmpty'
import _map from 'lodash/map'
import _toUpper from 'lodash/toUpper'
import { BugIcon, FileTextIcon, MousePointerClickIcon } from 'lucide-react'
@@ -18,6 +19,7 @@ interface PageflowProps {
metadata?: Metadata[]
}[]
timeFormat: '12-hour' | '24-hour'
+ zoomDomain?: [Date, Date] | null
}
const KeyValue = ({ evKey, evValue }: { evKey: string; evValue: string }) => (
@@ -39,16 +41,31 @@ const TransValue = ({ metadata, children }: { metadata?: Metadata[]; children: R
</div>
)
-export const Pageflow = ({ pages, timeFormat }: PageflowProps) => {
+export const Pageflow = ({ pages, timeFormat, zoomDomain }: PageflowProps) => {
const {
t,
i18n: { language },
} = useTranslation('common')
+ const filteredPages = zoomDomain
+ ? pages.filter((page) => {
+ const pageTime = new Date(page.created).getTime()
+ return pageTime >= zoomDomain[0].getTime() && pageTime <= zoomDomain[1].getTime()
+ })
+ : pages
+
+ if (zoomDomain && _isEmpty(filteredPages)) {
+ return (
+ <div className='my-4 py-8 text-center font-mono text-gray-800 dark:text-gray-300'>
+ {t('project.noEventsForSelectedPeriod')}
+ </div>
+ )
+ }
+
return (
<div className='flow-root font-mono'>
<ul className='-mb-8'>
- {_map(pages, ({ value, created, type, metadata }, index) => {
+ {_map(filteredPages, ({ value, created, type, metadata }, index) => {
const displayCreated = new Date(created).toLocaleDateString(language, {
day: 'numeric',
month: 'short',
@@ -61,7 +78,7 @@ export const Pageflow = ({ pages, timeFormat }: PageflowProps) => {
return (
<li key={`${value}${created}${index}`}>
<div className='relative pb-8'>
- {index !== pages.length - 1 ? (
+ {index !== filteredPages.length - 1 ? (
<span
className='absolute top-4 left-4 -ml-px h-full w-0.5 bg-slate-200 dark:bg-slate-700'
aria-hidden='true'
diff --git a/web/app/pages/Project/View/components/SessionChart.tsx b/web/app/pages/Project/View/components/SessionChart.tsx
index 4cb674078..c4b58cf2d 100644
--- a/web/app/pages/Project/View/components/SessionChart.tsx
+++ b/web/app/pages/Project/View/components/SessionChart.tsx
@@ -1,5 +1,5 @@
-import billboard, { type ChartOptions } from 'billboard.js'
-import { useEffect } from 'react'
+import billboard, { type ChartOptions, type Chart } from 'billboard.js'
+import { useEffect, useRef } from 'react'
import { getSettingsSession } from '../ViewProject.helpers'
@@ -13,6 +13,8 @@ interface SessionChartProps {
rotateXAxis: boolean
chartType: string
dataNames: any
+ onZoom?: (domain: [Date, Date] | null) => void
+ onChartReady?: (chart: Chart) => void
}
export const SessionChart = ({
@@ -22,13 +24,43 @@ export const SessionChart = ({
rotateXAxis,
chartType,
dataNames,
+ onZoom,
+ onChartReady,
}: SessionChartProps) => {
+ const chartRef = useRef<Chart | null>(null)
+
useEffect(() => {
- const bbSettings: ChartOptions = getSettingsSession(chart, timeBucket as string, timeFormat, rotateXAxis, chartType)
+ const bbSettings: ChartOptions = getSettingsSession(
+ chart,
+ timeBucket as string,
+ timeFormat,
+ rotateXAxis,
+ chartType,
+ onZoom,
+ )
+
+ if (chartRef.current) {
+ chartRef.current.destroy()
+ }
const generate = billboard.generate(bbSettings)
+ chartRef.current = generate
generate.data.names(dataNames)
- }, [chart, timeBucket, timeFormat, rotateXAxis, chartType, dataNames])
+
+ if (onChartReady) {
+ onChartReady(generate)
+ }
+
+ return () => {
+ if (generate) {
+ generate.destroy()
+ chartRef.current = null
+ if (onChartReady) {
+ onChartReady(null as any)
+ }
+ }
+ }
+ }, [chart, timeBucket, timeFormat, rotateXAxis, chartType, dataNames, onChartReady, onZoom])
return <div className='mt-5 h-80 md:mt-0 [&_svg]:!overflow-visible' id='sessionChart' />
}
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index 071930c27..9c5dfddbd 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -1002,6 +1002,8 @@
"markAsActive": "Mark as active",
"refreshStats": "Refresh stats",
"refresh": "Refresh",
+ "resetZoom": "Reset zoom",
+ "noEventsForSelectedPeriod": "There are no events for the selected time period.",
"barChart": "Bar chart",
"lineChart": "Line chart",
"settings": {