Skip to content

Commit a23e7e6

Browse files
committed
Add separate hook for numeric data
- Remove and replace useChartData with appropriate hooks
1 parent 692aa0e commit a23e7e6

File tree

24 files changed

+830
-834
lines changed

24 files changed

+830
-834
lines changed

app/src/components/domain/HistoricalDataChart/index.tsx

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ import {
2626
unique,
2727
} from '@togglecorp/fujs';
2828

29-
import useChartData from '#hooks/useChartData';
3029
import useInputState from '#hooks/useInputState';
30+
import useTemporalChartData from '#hooks/useTemporalChartData';
3131
import {
3232
COLOR_HAZARD_CYCLONE,
3333
COLOR_HAZARD_DROUGHT,
3434
COLOR_HAZARD_FLOOD,
3535
COLOR_HAZARD_FOOD_INSECURITY,
36-
defaultChartMargin,
37-
defaultChartPadding,
36+
DEFAULT_Y_AXIS_WIDTH_WITH_LABEL,
3837
} from '#utils/constants';
3938
import type { GoApiResponse } from '#utils/restRequest';
4039
import { useRequest } from '#utils/restRequest';
@@ -66,20 +65,6 @@ const validDisastersForChart: Record<number, boolean> = {
6665
[DISASTER_DROUGHT]: true,
6766
};
6867

69-
const X_AXIS_HEIGHT = 32;
70-
const Y_AXIS_WIDTH = 64;
71-
72-
const chartOffset = {
73-
left: Y_AXIS_WIDTH,
74-
top: 0,
75-
right: 0,
76-
bottom: X_AXIS_HEIGHT,
77-
};
78-
79-
const currentYear = new Date().getFullYear();
80-
const firstDayOfYear = new Date(currentYear, 0, 1);
81-
const lastDayOfYear = new Date(currentYear, 11, 31);
82-
8368
function isValidDisaster(
8469
disaster: PartialDisasterType | null | undefined,
8570
): disaster is DisasterType {
@@ -193,36 +178,15 @@ function HistoricalDataChart(props: Props) {
193178
},
194179
).filter(isDefined);
195180

196-
const {
197-
dataPoints,
198-
xAxisTicks,
199-
yAxisTicks,
200-
chartSize,
201-
} = useChartData(
181+
const chartData = useTemporalChartData(
202182
filteredEvents,
203183
{
204184
containerRef: chartContainerRef,
205-
chartOffset,
206-
chartMargin: defaultChartMargin,
207-
chartPadding: defaultChartPadding,
208185
keySelector: (datum) => datum.id,
209-
xValueSelector: (datum) => {
210-
const date = new Date(datum.disaster_start_date);
211-
date.setFullYear(currentYear);
212-
return date.getTime();
213-
},
214-
type: 'temporal',
215-
xAxisLabelSelector: (timestamp) => (
216-
new Date(timestamp).toLocaleString(
217-
navigator.language,
218-
{ month: 'short' },
219-
)
220-
),
186+
xValueSelector: (datum) => datum.disaster_start_date,
221187
yValueSelector: (datum) => datum.num_affected,
222-
xDomain: {
223-
min: firstDayOfYear.getTime(),
224-
max: lastDayOfYear.getTime(),
225-
},
188+
yearlyChart: true,
189+
yAxisWidth: DEFAULT_Y_AXIS_WIDTH_WITH_LABEL,
226190
},
227191
);
228192

@@ -250,16 +214,11 @@ function HistoricalDataChart(props: Props) {
250214
>
251215
<svg className={styles.svg}>
252216
<ChartAxes
253-
xAxisPoints={xAxisTicks}
254-
yAxisPoints={yAxisTicks}
255-
chartSize={chartSize}
256-
chartMargin={defaultChartMargin}
257-
xAxisHeight={X_AXIS_HEIGHT}
258-
yAxisWidth={Y_AXIS_WIDTH}
217+
chartData={chartData}
259218
yAxisLabel={strings.peopleExposed}
260219
/>
261220
</svg>
262-
{dataPoints?.map(
221+
{chartData.chartPoints?.map(
263222
(point) => {
264223
const funded = sumSafe(
265224
point.originalData.appeals.map(

app/src/hooks/useChartData.ts

Lines changed: 0 additions & 223 deletions
This file was deleted.

0 commit comments

Comments
 (0)