Skip to content

Commit

Permalink
[Feat] 종목에 따른 차트 y축 눈금 변경 로직 구현
Browse files Browse the repository at this point in the history
- 차트 종목에 따른 차트 눈금 변경 로직 구현 (종목마다 y축 간격 및 최소값이 다르므로, 이를 반영하여 차트를 그리는 로직)

Issues #14
  • Loading branch information
novice1993 committed Sep 6, 2023
1 parent ff755ca commit b45d674
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 136 deletions.
13 changes: 8 additions & 5 deletions client/src/components/CentralChart/KospiChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ const KospiChart = () => {
setCompanyLists(companyList);
}, [companyList]);

useEffect(() => {
console.log(companyLists);
}, [companyLists]);

const handleChangeSearchWord = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchWord(e.target.value);
};
Expand All @@ -54,6 +50,13 @@ const KospiChart = () => {
}
};

const handlePressEmnterToSearch = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.code === "Enter") {
handleSearchCompany();
setSearchWord("");
}
};

// 🔴 2) 클릭 이벤트
const handleKospi = () => {
dispatch(changeCompanyId(0));
Expand Down Expand Up @@ -81,7 +84,7 @@ const KospiChart = () => {
{/* 🔴 차트 변경 이벤트 테스트 */}
<label>
종목 검색
<input onChange={handleChangeSearchWord} />
<input onChange={handleChangeSearchWord} onKeyDown={handlePressEmnterToSearch} />
<button onClick={handleSearchCompany}>검색</button>
</label>
<button onClick={handleKospi}>코스피 버튼</button>
Expand Down
11 changes: 7 additions & 4 deletions client/src/components/CentralChart/StockChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ const StockChart = () => {
}
};

useEffect(() => {
console.log(companyId);
}, [companyId]);
const handlePressEmnterToSearch = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.code === "Enter") {
handleSearchCompany();
setSearchWord("");
}
};

// 🔴 2) 클릭 이벤트
const handleKospi = () => {
Expand Down Expand Up @@ -85,7 +88,7 @@ const StockChart = () => {
{/* 🔴 차트 변경 이벤트 테스트 */}
<label>
종목 검색
<input onChange={handleChangeSearchWord} />
<input onChange={handleChangeSearchWord} onKeyDown={handlePressEmnterToSearch} />
<button onClick={handleSearchCompany}>검색</button>
</label>
<button onClick={handleKospi}>코스피 버튼</button>
Expand Down
6 changes: 1 addition & 5 deletions client/src/hooks/useGetKospiChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import axios from "axios";
const useGetKospiChart = () => {
const [kospiData, setKospiData] = useState([]);

const { data, isLoading, error } = useQuery("kospi", getKospiData, {
// onSuccess: () => {
// console.log(data);
// },
});
const { data, isLoading, error } = useQuery("kospi", getKospiData, {});

useEffect(() => {
if (data) {
Expand Down
29 changes: 25 additions & 4 deletions client/src/hooks/useGetStockChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ import useGetStockData from "./useGetStockData";

const useGetStockChart = (companyId: number) => {
const { data } = useGetStockData(companyId);
const [chartData, setChartData] = useState([]);
const [chartData, setChartData] = useState<StockProps[]>([]);
const [yAxisInterval, setYAxisInterval] = useState(0);
const [yAxisMinPrice, setYAxisMinPrice] = useState(0);

// 서버에서 차트 데이터 fetching -> 클라이언트 화면에 활용할 차트 데이터 + 차트 y축 수치 상태 변화
useEffect(() => {
if (data) {
setChartData(data);

const { interval, min } = calculateYAxisOptions(data);
setYAxisInterval(interval);
setYAxisMinPrice(min);
}
}, [data, companyId]);
}, [data]);

const options = {
xAxis: {
Expand All @@ -24,8 +31,8 @@ const useGetStockChart = (companyId: number) => {
{
type: "value",
position: "right",
interval: 100,
min: 69700,
interval: yAxisInterval,
min: yAxisMinPrice,
},
],
dataZoom: [
Expand Down Expand Up @@ -61,6 +68,20 @@ const useGetStockChart = (companyId: number) => {

export default useGetStockChart;

// y축 눈금 옵션 설정하는 함수
const calculateYAxisOptions = (data: StockProps[]) => {
const sampleData = data.filter((_, index) => index % 10 === 0);
const samplePrice = sampleData.map((stock) => parseFloat(stock.stck_prpr));

const maxPrice = Math.max(...samplePrice);
const minPrice = Math.min(...samplePrice);

const interval = Math.ceil((maxPrice - minPrice) / 10);
const min = Math.floor(minPrice - interval * 5);

return { interval, min };
};

interface StockProps {
stockMinId: number;
companyId: number;
Expand Down
7 changes: 0 additions & 7 deletions client/src/hooks/useGetStockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const useGetStockData = (companyId: number) => {
const currentTime = new Date();
const minute = currentTime.getMinutes();

console.log(currentTime);
console.log("checkTime 테스트");

if (minute === 0 || minute === 30) {
refetch();
setAutoRefetch(true);
Expand All @@ -39,10 +36,6 @@ const useGetStockData = (companyId: number) => {
enabled: true,
refetchInterval: autoRefetch && 60000 * 10, // 정각 혹은 30분에 맞춰서 10분 마다 데이터 리패칭
refetchOnMount: true,
// onSuccess: () => {
// console.log(new Date());
// console.log(data);
// },
});

return { data, isLoading, error };
Expand Down
57 changes: 9 additions & 48 deletions client/src/page/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ const MainPage = () => {
setEmailSignupModalOpen(false);
}, []);

const [isEmailVerificationModalOpen, setEmailVerificationModalOpen] =
useState(false);
const [isEmailVerificationModalOpen, setEmailVerificationModalOpen] = useState(false);

const openEmailVerificationModal = useCallback(() => {
setEmailSignupModalOpen(false); // 이메일 회원가입 모달 닫기
Expand All @@ -63,8 +62,7 @@ const MainPage = () => {
setEmailVerificationModalOpen(false);
}, []);

const [isPasswordSettingModalOpen, setPasswordSettingModalOpen] =
useState(false);
const [isPasswordSettingModalOpen, setPasswordSettingModalOpen] = useState(false);

const openPasswordSettingModal = useCallback(() => {
setEmailVerificationModalOpen(false); // 이메일 인증 모달 닫기
Expand All @@ -81,9 +79,7 @@ const MainPage = () => {
setIsLoggedIn(true);
};

const [selectedMenu, setSelectedMenu] = useState<"관심목록" | "투자목록">(
"투자목록"
); // Default menu is 관심목록
const [selectedMenu, setSelectedMenu] = useState<"관심목록" | "투자목록">("투자목록"); // Default menu is 관심목록

const handleMenuChange = (menu: "관심목록" | "투자목록") => {
setSelectedMenu(menu);
Expand All @@ -95,57 +91,22 @@ const MainPage = () => {

return (
<Container>
{isLoggedIn ? (
<LoginHeader onLogoutClick={handleLogout} />
) : (
<LogoutHeader onLoginClick={openOAuthModal} />
)}
{isLoggedIn ? <LoginHeader onLogoutClick={handleLogout} /> : <LogoutHeader onLoginClick={openOAuthModal} />}
<Main>
<CompareChartSection />
{!expandScreen.left && (
<LeftSection>
{selectedMenu === "관심목록" ? (
<WatchList
key="watchlist"
currentListType={selectedMenu}
onChangeListType={handleMenuChange}
/>
) : (
<Holdings
currentListType={selectedMenu}
onChangeListType={handleMenuChange}
/>
)}
</LeftSection>
<LeftSection>{selectedMenu === "관심목록" ? <WatchList key="watchlist" currentListType={selectedMenu} onChangeListType={handleMenuChange} /> : <Holdings currentListType={selectedMenu} onChangeListType={handleMenuChange} />}</LeftSection>
)}
<CentralChart />
<StockOrderSection />
{!expandScreen.right && <TabContainerPage></TabContainerPage>}
</Main>
{isOAuthModalOpen && (
<OAuthLoginModal
onClose={closeOAuthModal}
onEmailLoginClick={openEmailLoginModal}
onEmailSignupClick={openEmailSignupModal}
onWatchListClick={() => handleMenuChange("관심목록")}
onHoldingsClick={() => handleMenuChange("투자목록")}
/>
)}
{isEmailLoginModalOpen && (
<EmailLoginModal onClose={closeEmailLoginModal} onLogin={handleLogin} />
)}
{isEmailSignupModalOpen && (
<EmailSignupModal
onClose={closeEmailSignupModal}
onRequestVerification={openEmailVerificationModal}
/>
)}
{isEmailVerificationModalOpen && (
<EmailVerificationModal
onClose={closeEmailVerificationModal}
onNextStep={openPasswordSettingModal}
/>
<OAuthLoginModal onClose={closeOAuthModal} onEmailLoginClick={openEmailLoginModal} onEmailSignupClick={openEmailSignupModal} onWatchListClick={() => handleMenuChange("관심목록")} onHoldingsClick={() => handleMenuChange("투자목록")} />
)}
{isEmailLoginModalOpen && <EmailLoginModal onClose={closeEmailLoginModal} onLogin={handleLogin} />}
{isEmailSignupModalOpen && <EmailSignupModal onClose={closeEmailSignupModal} onRequestVerification={openEmailVerificationModal} />}
{isEmailVerificationModalOpen && <EmailVerificationModal onClose={closeEmailVerificationModal} onNextStep={openPasswordSettingModal} />}
{isPasswordSettingModalOpen && (
<PasswordSettingModal
onClose={() => {
Expand Down
26 changes: 6 additions & 20 deletions client/src/page/TabPages/MarketInfoPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from "styled-components";
import React, { useState } from "react";
import MarketSummary from "../../components/MarketComponents/MarketSummary";
import MarketSummary from "../../components/MarketComponents/index";
import MarketStockList from "../../components/MarketComponents/MarketStockList";
import MarketIssue from "../../components/MarketComponents/MarketIssue";
interface Props {}
Expand All @@ -15,30 +15,16 @@ const MarketInfo: React.FC<Props> = () => {
};
return (
<div>
<style>
@import
url('https://fonts.googleapis.com/css2?family=Jua&family=Noto+Sans+KR:wght@500&display=swap');
</style>
<style>@import url('https://fonts.googleapis.com/css2?family=Jua&family=Noto+Sans+KR:wght@500&display=swap');</style>

<MarketInfoStyle>
<TabStyle
onClick={() => handleTabStyle(1)}
className={`tab ${tabStyle === 1 ? "active-tab" : "inactive-tab"}`}
>
<TabStyle onClick={() => handleTabStyle(1)} className={`tab ${tabStyle === 1 ? "active-tab" : "inactive-tab"}`}>
<Market onClick={() => handleTabClick("market")}>시장요약</Market>
</TabStyle>
<TabStyle
onClick={() => handleTabStyle(2)}
className={`tab ${tabStyle === 2 ? "active-tab" : "inactive-tab"}`}
>
<StockList onClick={() => handleTabClick("stockList")}>
전체종목
</StockList>
<TabStyle onClick={() => handleTabStyle(2)} className={`tab ${tabStyle === 2 ? "active-tab" : "inactive-tab"}`}>
<StockList onClick={() => handleTabClick("stockList")}>전체종목</StockList>
</TabStyle>
<TabStyle
onClick={() => handleTabStyle(3)}
className={`tab ${tabStyle === 3 ? "active-tab" : "inactive-tab"}`}
>
<TabStyle onClick={() => handleTabStyle(3)} className={`tab ${tabStyle === 3 ? "active-tab" : "inactive-tab"}`}>
<Issue onClick={() => handleTabClick("issues")}>시장이슈</Issue>
</TabStyle>
</MarketInfoStyle>
Expand Down
31 changes: 6 additions & 25 deletions client/src/page/TabPages/TabContainerPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Routes, Route, Link } from "react-router-dom";
import styled from "styled-components";
import { StockItems } from "../../components/stockListComponents/StockItems";
import { Community } from "./communityPage";
import { Status } from "../../components/statusComponents/Status";
import { Status } from "../../components/statusComponents/index";
import { useState } from "react";
export const TabContainerPage = () => {
const [activeTab, setActiveTab] = useState(1);
Expand All @@ -13,39 +13,20 @@ export const TabContainerPage = () => {

return (
<TabContainerStyle>
<style>
@import
url('https://fonts.googleapis.com/css2?family=Jua&family=Noto+Sans+KR:wght@500&display=swap');
</style>
<style>@import url('https://fonts.googleapis.com/css2?family=Jua&family=Noto+Sans+KR:wght@500&display=swap');</style>

<div>
<TabNavArea>
<Nav
to="/"
onClick={() => handleClickActiveTab(1)}
className={`tab ${activeTab === 1 ? "active-tab" : "inactive-tab"}`}
>
<Nav to="/" onClick={() => handleClickActiveTab(1)} className={`tab ${activeTab === 1 ? "active-tab" : "inactive-tab"}`}>
시장정보
</Nav>
<Nav
to="/stockitems"
onClick={() => handleClickActiveTab(2)}
className={`tab ${activeTab === 2 ? "active-tab" : "inactive-tab"}`}
>
<Nav to="/stockitems" onClick={() => handleClickActiveTab(2)} className={`tab ${activeTab === 2 ? "active-tab" : "inactive-tab"}`}>
종목정보
</Nav>
<Nav
to="/community"
onClick={() => handleClickActiveTab(3)}
className={`tab ${activeTab === 3 ? "active-tab" : "inactive-tab"}`}
>
<Nav to="/community" onClick={() => handleClickActiveTab(3)} className={`tab ${activeTab === 3 ? "active-tab" : "inactive-tab"}`}>
커뮤니티
</Nav>
<Nav
to="/status"
onClick={() => handleClickActiveTab(4)}
className={`tab ${activeTab === 4 ? "active-tab" : "inactive-tab"}`}
>
<Nav to="/status" onClick={() => handleClickActiveTab(4)} className={`tab ${activeTab === 4 ? "active-tab" : "inactive-tab"}`}>
투자현황
</Nav>
</TabNavArea>
Expand Down
22 changes: 4 additions & 18 deletions client/src/page/TabPages/communityPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//import { TabNavArea } from "./TabNavArea";
import { useState } from "react";
import styled from "styled-components";
import TimeLineComponent from "../../components/communityComponents/TimeLineComponent";
import TimeLineComponent from "../../components/communityComponents/index";
import { StockDisscussion } from "../../components/communityComponents/StockDisscussion";

export const Community = () => {
Expand All @@ -20,25 +20,11 @@ export const Community = () => {
<div>
<CommunityNav>
{/*커뮤니티 내부 탭 전환*/}
{selectCommunity === "timeline" ? (
<NavButtonFocus onClick={() => setSelectCommunity("timeline")}>
{communityText.timeline}
</NavButtonFocus>
) : (
<NavButton onClick={() => setSelectCommunity("timeline")}>
{communityText.timeline}
</NavButton>
)}
{selectCommunity === "timeline" ? <NavButtonFocus onClick={() => setSelectCommunity("timeline")}>{communityText.timeline}</NavButtonFocus> : <NavButton onClick={() => setSelectCommunity("timeline")}>{communityText.timeline}</NavButton>}
{selectCommunity === "stockDisscussion" ? (
<NavButtonFocus
onClick={() => setSelectCommunity("stockDisscussion")}
>
{communityText.stockDisscussion}
</NavButtonFocus>
<NavButtonFocus onClick={() => setSelectCommunity("stockDisscussion")}>{communityText.stockDisscussion}</NavButtonFocus>
) : (
<NavButton onClick={() => setSelectCommunity("stockDisscussion")}>
{communityText.stockDisscussion}
</NavButton>
<NavButton onClick={() => setSelectCommunity("stockDisscussion")}>{communityText.stockDisscussion}</NavButton>
)}
</CommunityNav>

Expand Down

0 comments on commit b45d674

Please sign in to comment.