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!

feat: date selection by zooming the main chart

Blue Mouseee2506d67d40
diff --git a/web/app/pages/Project/View/ViewProject.helpers.tsx b/web/app/pages/Project/View/ViewProject.helpers.tsx
index 00a9d0104..19c63f854 100644
--- a/web/app/pages/Project/View/ViewProject.helpers.tsx
+++ b/web/app/pages/Project/View/ViewProject.helpers.tsx
@@ -498,6 +498,8 @@ const getSettings = (
chartType: string,
customEvents?: Record<string, string[]>,
compareChart?: TrafficLogResponse['chart'] & { [key: string]: number[] },
+ onZoom?: (domain: [Date, Date] | null) => void,
+ enableZoom?: boolean,
): ChartOptions => {
const xAxisSize = _size(chart.x)
const lines: GridLineOptions[] = []
@@ -821,6 +823,15 @@ const getSettings = (
bar: {
linearGradient: true,
},
+ zoom:
+ onZoom && enableZoom !== false
+ ? {
+ enabled: zoom(),
+ type: 'drag',
+ onzoom: onZoom,
+ resetButton: false,
+ }
+ : undefined,
bindto: '#dataChart',
}
}
@@ -1290,6 +1301,8 @@ const getSettingsPerf = (
chartType: string,
timeFormat: string,
compareChart?: Record<string, string[]>,
+ onZoom?: (domain: [Date, Date] | null) => void,
+ enableZoom?: boolean,
): ChartOptions => {
const xAxisSize = _size(chart.x)
@@ -1501,6 +1514,15 @@ const getSettingsPerf = (
bar: {
linearGradient: true,
},
+ zoom:
+ onZoom && enableZoom !== false
+ ? {
+ enabled: zoom(),
+ type: 'drag',
+ onzoom: onZoom,
+ resetButton: false,
+ }
+ : undefined,
bindto: '#dataChart',
}
}
diff --git a/web/app/pages/Project/View/ViewProject.tsx b/web/app/pages/Project/View/ViewProject.tsx
index 6d543d785..f0d66d1c1 100644
--- a/web/app/pages/Project/View/ViewProject.tsx
+++ b/web/app/pages/Project/View/ViewProject.tsx
@@ -1250,6 +1250,9 @@ const ViewProject = () => {
rotateXAxis,
chartType,
events,
+ undefined,
+ onMainChartZoom,
+ shouldEnableZoom,
)
setMainChart(() => {
const generate = billboard.generate(bbSettings)
@@ -1482,6 +1485,8 @@ const ViewProject = () => {
customEventsChart,
// @ts-expect-error
dataCompare?.chart,
+ onMainChartZoom,
+ shouldEnableZoom,
)
setChartData(chart as any)
@@ -1882,6 +1887,8 @@ const ViewProject = () => {
chartType,
timeFormat,
dataCompare?.chart,
+ onMainChartZoom,
+ shouldEnableZoom,
)
setChartDataPerf(chartPerf)
@@ -2136,6 +2143,8 @@ const ViewProject = () => {
chartType,
customEventsChartData,
dataChartCompare,
+ onMainChartZoom,
+ shouldEnableZoom,
)
setMainChart(() => {
@@ -2181,6 +2190,8 @@ const ViewProject = () => {
chartType,
timeFormat,
dataChartPerfCompare,
+ onMainChartZoom,
+ shouldEnableZoom,
)
setMainChart(() => {
@@ -2388,6 +2399,34 @@ const ViewProject = () => {
setZoomedTimeRange(null)
}
+ const onMainChartZoom = (domain: [Date, Date] | null) => {
+ if (!domain) {
+ return
+ }
+
+ const [from, to] = domain
+ const newSearchParams = new URLSearchParams(searchParams.toString())
+
+ // Format dates based on time bucket precision
+ let fromFormatted = from.toISOString().split('T')[0] + 'T00:00:00.000Z'
+ let toFormatted = to.toISOString().split('T')[0] + 'T23:59:59.999Z'
+
+ newSearchParams.set('from', fromFormatted)
+ newSearchParams.set('to', toFormatted)
+ newSearchParams.set('period', 'custom')
+ setSearchParams(newSearchParams)
+ }
+
+ const shouldEnableZoom = useMemo(() => {
+ if (period !== 'custom' || !dateRange) {
+ return true
+ }
+
+ // Enable zoom only if the range is more than 1 day
+ const daysDiff = Math.ceil((dateRange[1].getTime() - dateRange[0].getTime()) / (1000 * 3600 * 24))
+ return daysDiff > 1
+ }, [period, dateRange])
+
const getFilterLink = (column: string, value: string): LinkProps['to'] => {
const isFilterActive = filters.findIndex((filter) => filter.column === column && filter.filter === value) >= 0
@@ -2434,6 +2473,8 @@ const ViewProject = () => {
type,
customEventsChartData,
dataChartCompare,
+ onMainChartZoom,
+ shouldEnableZoom,
)
setMainChart(() => {
@@ -2452,6 +2493,8 @@ const ViewProject = () => {
type,
timeFormat,
dataChartPerfCompare,
+ onMainChartZoom,
+ shouldEnableZoom,
)
setMainChart(() => {