@@ -94,17 +94,22 @@ export const MainGraph = ({
9494 return
9595 }
9696 console . log ( 'effect running' )
97+ const yBottomEdge = height - marginBottom
98+ const yTopEdge = marginTop
99+ const xLeftEdge = marginLeft
100+ const xRightEdge = width - marginRight
101+ const chartAreaWidth = width - marginLeft - marginRight
97102
98103 const yMin = 0
99104 const yDomain = [ yMin , yMax ]
100105 // Declare the y (vertical position) scale.
101- const y = d3 . scaleLinear ( yDomain , [ height - marginBottom , marginTop ] ) . nice ( )
106+ const y = d3 . scaleLinear ( yDomain , [ yBottomEdge , yTopEdge ] ) . nice ( )
102107
103108 // Declare the x (horizontal position) scale.
104109 // It's a simple linear axis, one unit for every time bucket
105110 // because the BE returns equal length buckets
106111 const xDomain = [ 0 , remappedData . length - 1 ]
107- const x = d3 . scaleLinear ( xDomain , [ marginLeft , width - marginRight ] )
112+ const x = d3 . scaleLinear ( xDomain , [ xLeftEdge , xRightEdge ] )
108113
109114 const points : Point [ ] = remappedData . map ( ( d , index ) => [
110115 x ( index ) ,
@@ -123,7 +128,7 @@ export const MainGraph = ({
123128 // Add the x-axis.
124129 svg
125130 . append ( 'g' )
126- . attr ( 'transform' , `translate(0,${ height - marginBottom } )` )
131+ . attr ( 'transform' , `translate(0,${ yBottomEdge } )` )
127132 . call (
128133 d3
129134 . axisBottom ( x )
@@ -151,14 +156,13 @@ export const MainGraph = ({
151156 . selectAll ( '.tick text' )
152157 . attr ( 'class' , classNames ( tickClass , 'translate-y-2' ) )
153158 )
154-
155159 // Add the y-axis, remove the domain line, add grid lines and a label.
156160 // TODO: make dynamic
157161 // const maxYTicks = 8
158162 const yTickCount = 8
159163 svg
160164 . append ( 'g' )
161- . attr ( 'transform' , `translate(${ marginLeft } , 0)` )
165+ . attr ( 'transform' , `translate(${ xLeftEdge } , 0)` )
162166 . call (
163167 d3
164168 . axisLeft ( y )
@@ -173,7 +177,7 @@ export const MainGraph = ({
173177 g
174178 . selectAll ( '.tick line' )
175179 . clone ( )
176- . attr ( 'x2' , width - marginLeft - marginRight )
180+ . attr ( 'x2' , chartAreaWidth )
177181 . attr ( 'class' , tickLineClass )
178182 )
179183
@@ -190,8 +194,6 @@ export const MainGraph = ({
190194 stopBottom : secondaryGradient . stopBottom
191195 } )
192196
193- const yBottomEdge = height - marginBottom
194-
195197 drawAreaUnderLine ( {
196198 svg,
197199 gradientId : mainGradientId ,
@@ -236,29 +238,45 @@ export const MainGraph = ({
236238 svg
237239 . on ( 'pointermove' , ( event ) => {
238240 const [ xPointer , yPointer ] = d3 . pointer ( event )
239- const closestIndexToPointer = d3
240- . bisector ( ( dataPoint : Point ) => dataPoint [ 0 ] )
241- . center ( points , xPointer )
242- const [ x , yValues ] = points [ closestIndexToPointer ]
243- if ( yValues . yMain !== null ) {
244- dot
245- . attr ( 'transform' , `translate(${ x } ,${ yValues . yMain } )` )
246- . attr ( 'display' , null )
247- } else {
241+ const buffer = 4 // can stray this many px off the chart and still see tooltip
242+ const inHoverableArea =
243+ xPointer >= xLeftEdge - buffer &&
244+ xPointer <= xRightEdge + buffer &&
245+ yPointer >= yTopEdge - buffer &&
246+ yPointer <= yBottomEdge + buffer
247+ if ( ! inHoverableArea ) {
248248 dot . attr ( 'display' , 'none' )
249- }
250- if ( yValues . yComparison !== null ) {
251- comparisonDot
252- . attr ( 'transform' , `translate(${ x } ,${ yValues . yComparison } )` )
253- . attr ( 'display' , null )
254- } else {
255249 comparisonDot . attr ( 'display' , 'none' )
250+ setTooltip ( {
251+ selectedIndex : null ,
252+ x : 0 ,
253+ y : 0
254+ } )
255+ } else {
256+ const closestIndexToPointer = d3
257+ . bisector ( ( dataPoint : Point ) => dataPoint [ 0 ] )
258+ . center ( points , xPointer )
259+ const [ x , yValues ] = points [ closestIndexToPointer ]
260+ if ( yValues . yMain !== null ) {
261+ dot
262+ . attr ( 'transform' , `translate(${ x } ,${ yValues . yMain } )` )
263+ . attr ( 'display' , null )
264+ } else {
265+ dot . attr ( 'display' , 'none' )
266+ }
267+ if ( yValues . yComparison !== null ) {
268+ comparisonDot
269+ . attr ( 'transform' , `translate(${ x } ,${ yValues . yComparison } )` )
270+ . attr ( 'display' , null )
271+ } else {
272+ comparisonDot . attr ( 'display' , 'none' )
273+ }
274+ setTooltip ( {
275+ selectedIndex : closestIndexToPointer ,
276+ x : xPointer ,
277+ y : yPointer
278+ } )
256279 }
257- setTooltip ( {
258- selectedIndex : closestIndexToPointer ,
259- x : xPointer ,
260- y : yPointer
261- } )
262280 } )
263281 . on ( 'pointerleave' , ( ) => {
264282 dot . attr ( 'display' , 'none' )
0 commit comments