Skip to content

Commit 9391e1b

Browse files
authored
Merge pull request #630 from rakseong/main
fix(view) : 상단 라인 차트 전체 기간에서 특정 기간으로 Brushing 후 Reset 기능 버튼 추가
2 parents 6ee915d + 2b4c07f commit 9391e1b

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

packages/view/src/components/TemporalFilter/LineChartBrush.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import type { Margin } from "./LineChart";
55

66
export type BrushXSelection = [number, number] | null;
77

8-
export const drawBrush = (
9-
refTarget: SVGSVGElement,
8+
export const createBrush = (
109
margin: Margin,
1110
chartWidth: number,
1211
chartHeight: number,
@@ -19,7 +18,6 @@ export const drawBrush = (
1918
brushHandler(event.selection as BrushXSelection);
2019
};
2120

22-
const svg = d3.select(refTarget);
2321
const brush = d3
2422
.brushX()
2523
.extent([
@@ -29,8 +27,20 @@ export const drawBrush = (
2927
// .handleSize(5)
3028
.on("end", brushed);
3129

32-
svg
30+
return brush;
31+
};
32+
33+
export const drawBrush = (refTarget: SVGSVGElement, margin: Margin, brush: d3.BrushBehavior<unknown>) => {
34+
const svg = d3.select(refTarget);
35+
36+
const brushGroup = svg
3337
.append("g")
3438
.call(brush)
3539
.attr("transform", `translate(${margin.left / 2}, 0)`);
40+
41+
return brushGroup;
42+
};
43+
44+
export const resetBrush = (brushGroup: SVGGElement, brush: d3.BrushBehavior<unknown>) => {
45+
d3.select(brushGroup).call(brush.move, null);
3646
};

packages/view/src/components/TemporalFilter/TemporalFilter.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ import drawLineChart from "./LineChart";
1212
import type { LineChartDatum } from "./LineChart";
1313
import { useWindowResize } from "./TemporalFilter.hook";
1414
import type { BrushXSelection } from "./LineChartBrush";
15-
import { drawBrush } from "./LineChartBrush";
15+
import { createBrush, drawBrush, resetBrush } from "./LineChartBrush";
1616
import { BRUSH_MARGIN, TEMPORAL_FILTER_LINE_CHART_STYLES } from "./LineChart.const";
1717

1818
const TemporalFilter = () => {
1919
const { data, filteredData, setFilteredData, filteredRange, setFilteredRange, setSelectedData, loading } =
2020
useGlobalData();
2121

22+
const brushGroupRef = useRef<SVGGElement | null>(null);
23+
const brushRef = useRef<d3.BrushBehavior<unknown>>();
24+
2225
const loaderStyle: CSSProperties = {
2326
position: "fixed",
2427
left: "50%",
@@ -113,7 +116,8 @@ const TemporalFilter = () => {
113116
setSelectedData([]);
114117
};
115118

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();
117121

118122
return () => {
119123
d3.select(svgElement).selectAll("g").remove();
@@ -130,6 +134,12 @@ const TemporalFilter = () => {
130134
setSelectedData,
131135
]);
132136

137+
const resetBrushHandler = () => {
138+
if (brushGroupRef.current && brushRef.current) {
139+
resetBrush(brushGroupRef.current, brushRef.current);
140+
}
141+
};
142+
133143
return (
134144
<article className="temporal-filter">
135145
<BounceLoader
@@ -141,6 +151,14 @@ const TemporalFilter = () => {
141151
className="line-charts"
142152
ref={wrapperRef}
143153
>
154+
{filteredRange && (
155+
<button
156+
type="button"
157+
onClick={resetBrushHandler}
158+
>
159+
reset
160+
</button>
161+
)}
144162
<svg
145163
className="line-charts-svg"
146164
ref={ref}

0 commit comments

Comments
 (0)