Skip to content

Commit 148c773

Browse files
committed
Implement click to zoom
1 parent 8921c95 commit 148c773

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

assets/js/dashboard/stats/graph/main-graph.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import dateFormatter from './date-formatter'
1010
import classNames from 'classnames'
1111
import { ChangeArrow } from '../reports/change-arrow'
1212
import { Metric } from '../../../types/query-api'
13+
import { useAppNavigate } from '../../navigation/use-app-navigate'
1314

1415
const height = 368
1516
const marginTop = 16
@@ -69,14 +70,15 @@ export const MainGraph = ({
6970
data: MainGraphData
7071
}) => {
7172
const { mode } = useTheme()
73+
const navigate = useAppNavigate()
7274
const { primaryGradient, secondaryGradient } = paletteByTheme[mode]
7375
const svgRef = useRef<SVGSVGElement | null>(null)
7476
const [tooltip, setTooltip] = useState<{
7577
x: number
7678
y: number
7779
selectedIndex: number | null
7880
}>({ x: 0, y: 0, selectedIndex: null })
79-
81+
const { selectedIndex } = tooltip
8082
const interval = data.query.dimensions[0].split('time:')[1]
8183
const metric = data.query.metrics[0] as FormattableMetric
8284
const period = data.period
@@ -271,6 +273,7 @@ export const MainGraph = ({
271273

272274
return () => {
273275
svg.selectAll('*').remove()
276+
setTooltip({ x: 0, y: 0, selectedIndex: null })
274277
}
275278
}, [
276279
primaryGradient,
@@ -290,11 +293,30 @@ export const MainGraph = ({
290293
style={{ height: height, maxWidth: width }}
291294
>
292295
<svg
296+
onClick={
297+
selectedIndex !== null && showZoomToPeriod
298+
? () =>
299+
navigate({
300+
search: (currentSearch) => ({
301+
...currentSearch,
302+
date:
303+
remappedData[selectedIndex].timeLabel ??
304+
remappedData[selectedIndex].comparisonTimeLabel,
305+
period: {
306+
month: DashboardPeriod.month,
307+
day: DashboardPeriod.day
308+
}[interval]
309+
})
310+
})
311+
: undefined
312+
}
293313
ref={svgRef}
294314
viewBox={`0 0 ${width} ${height}`}
295-
className="w-full h-auto cursor-pointer"
315+
className={classNames('w-full h-auto', {
316+
['cursor-pointer']: showZoomToPeriod
317+
})}
296318
/>
297-
{tooltip.selectedIndex !== null && (
319+
{selectedIndex !== null && remappedData[selectedIndex] && (
298320
<GraphTooltip
299321
width={width}
300322
showZoomToPeriod={showZoomToPeriod}
@@ -304,7 +326,7 @@ export const MainGraph = ({
304326
metric={metric}
305327
x={tooltip.x}
306328
y={tooltip.y}
307-
datum={remappedData[tooltip.selectedIndex]}
329+
datum={remappedData[selectedIndex]}
308330
/>
309331
)}
310332
</div>

0 commit comments

Comments
 (0)