Skip to content
This repository was archived by the owner on Aug 4, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/components/page/Index/index.hook.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Options, SeriesLineOptions } from "highcharts";
// eslint-disable-next-line import/no-named-as-default
import HighchartsReact from "highcharts-react-official";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";

import {
Expand All @@ -15,6 +17,7 @@ export const useIndex = () => {
const [targetDataIndex, setTargetDataIndex] = useState<number>(0);

const isInitialized = useRef(false);
const chartComponentRef = useRef<HighchartsReact.RefObject>(null);

const [prefecturesData] = trpc.getPrefectures.useSuspenseQuery();

Expand All @@ -29,6 +32,8 @@ export const useIndex = () => {

const populationsData = useGetPopulationsQueries(prefectures);

const isFetching = useMemo(() => populationsData.some(({ isFetching }) => isFetching), [populationsData]);

const checkedPopulations = useMemo(() => {
const checkedPopulations: Exclude<GetPopulationQueryResultType["data"], undefined>[] = [];
populationsData.forEach(({ data }) => {
Expand Down Expand Up @@ -74,7 +79,14 @@ export const useIndex = () => {
yAxis: { title: { text: "人口" } },
series: highchartsSeries,
credits: { enabled: false },
lang: { noData: "都道府県を選択してください" },
lang: {
noData: "都道府県を選択してください",
loading: "データ取得中...",
},
loading: {
hideDuration: 1000,
showDuration: 1000,
},
};
return options;
}, [highchartsSeries, targetLabel]);
Expand Down Expand Up @@ -126,11 +138,22 @@ export const useIndex = () => {
isInitialized.current = true;
}, [checkedPrefCodes.size, handleSetCheckedPrefCodes, prefecturesData.result]);

// データ取得中にローディングを表示する
useEffect(() => {
const chart = chartComponentRef.current?.chart;
if (isFetching) {
chart?.showLoading();
} else {
chart?.hideLoading();
}
}, [isFetching]);

return {
prefectures,
labels,
targetDataIndex,
highchartsOptions,
chartComponentRef,
handleChangeCheckedCode,
handleChangeTargetDataIndex,
};
Expand Down
5 changes: 3 additions & 2 deletions src/components/page/Index/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { css } from "@linaria/core";
import Highcharts from "highcharts";
import * as Highcharts from "highcharts";
import { HighchartsReact } from "highcharts-react-official";

import { useIndex } from "./index.hook";
Expand All @@ -13,6 +13,7 @@ export const Index = () => {
labels,
targetDataIndex,
highchartsOptions,
chartComponentRef,
handleChangeCheckedCode,
handleChangeTargetDataIndex,
} = useIndex();
Expand Down Expand Up @@ -109,7 +110,7 @@ export const Index = () => {
margin-top: 32px;
`}
>
<HighchartsReact highcharts={Highcharts} options={highchartsOptions} />
<HighchartsReact highcharts={Highcharts} options={highchartsOptions} ref={chartComponentRef} />
</div>
</div>
);
Expand Down