@@ -12,13 +12,16 @@ import drawLineChart from "./LineChart";
12
12
import type { LineChartDatum } from "./LineChart" ;
13
13
import { useWindowResize } from "./TemporalFilter.hook" ;
14
14
import type { BrushXSelection } from "./LineChartBrush" ;
15
- import { drawBrush } from "./LineChartBrush" ;
15
+ import { createBrush , drawBrush , resetBrush } from "./LineChartBrush" ;
16
16
import { BRUSH_MARGIN , TEMPORAL_FILTER_LINE_CHART_STYLES } from "./LineChart.const" ;
17
17
18
18
const TemporalFilter = ( ) => {
19
19
const { data, filteredData, setFilteredData, filteredRange, setFilteredRange, setSelectedData, loading } =
20
20
useGlobalData ( ) ;
21
21
22
+ const brushGroupRef = useRef < SVGGElement | null > ( null ) ;
23
+ const brushRef = useRef < d3 . BrushBehavior < unknown > > ( ) ;
24
+
22
25
const loaderStyle : CSSProperties = {
23
26
position : "fixed" ,
24
27
left : "50%" ,
@@ -113,7 +116,8 @@ const TemporalFilter = () => {
113
116
setSelectedData ( [ ] ) ;
114
117
} ;
115
118
116
- drawBrush ( svgElement , BRUSH_MARGIN , windowSize . width , chartHeight * 2 , dateChangeHandler ) ;
119
+ brushRef . current = createBrush ( BRUSH_MARGIN , windowSize . width , chartHeight * 2 , dateChangeHandler ) ;
120
+ brushGroupRef . current = drawBrush ( svgElement , BRUSH_MARGIN , brushRef . current ) . node ( ) ;
117
121
118
122
return ( ) => {
119
123
d3 . select ( svgElement ) . selectAll ( "g" ) . remove ( ) ;
@@ -130,6 +134,12 @@ const TemporalFilter = () => {
130
134
setSelectedData ,
131
135
] ) ;
132
136
137
+ const resetBrushHandler = ( ) => {
138
+ if ( brushGroupRef . current && brushRef . current ) {
139
+ resetBrush ( brushGroupRef . current , brushRef . current ) ;
140
+ }
141
+ } ;
142
+
133
143
return (
134
144
< article className = "temporal-filter" >
135
145
< BounceLoader
@@ -141,6 +151,14 @@ const TemporalFilter = () => {
141
151
className = "line-charts"
142
152
ref = { wrapperRef }
143
153
>
154
+ { filteredRange && (
155
+ < button
156
+ type = "button"
157
+ onClick = { resetBrushHandler }
158
+ >
159
+ reset
160
+ </ button >
161
+ ) }
144
162
< svg
145
163
className = "line-charts-svg"
146
164
ref = { ref }
0 commit comments