Skip to content

Commit 45f602d

Browse files
committed
fix: 차트 버그 수정
- 차트에 0s 가 중복되어 적히는 것을 수정 - 1s 부터 보이게 변경
1 parent 988ac5c commit 45f602d

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

src/components/chart/IndividualChart.tsx

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
import React, { useEffect, useState } from "react";
2-
import { useRecoilValue } from "recoil";
3-
import { gameState } from "../../recoil/atoms/gameState";
4-
import { userState } from "../../recoil/atoms/userState";
5-
import { Line } from "react-chartjs-2";
6-
import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend } from "chart.js";
7-
import { RoomDataProps } from "../../types/RoomData.type.ts";
8-
import { RoomClientProps } from "../../types/RoomClient.type.ts";
1+
import React, { useEffect, useState } from 'react';
2+
import { useRecoilValue } from 'recoil';
3+
import { gameState } from '../../recoil/atoms/gameState';
4+
import { userState } from '../../recoil/atoms/userState';
5+
import { Line } from 'react-chartjs-2';
6+
import {
7+
Chart as ChartJS,
8+
CategoryScale,
9+
LinearScale,
10+
PointElement,
11+
LineElement,
12+
Title,
13+
Tooltip,
14+
Legend,
15+
} from 'chart.js';
16+
import { RoomDataProps } from '../../types/RoomData.type.ts';
17+
import { RoomClientProps } from '../../types/RoomClient.type.ts';
918

1019
// Chart.js 모듈 등록
1120
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend);
@@ -19,7 +28,7 @@ const IndividualChart: React.FC = () => {
1928
const nickname = user.nickname;
2029

2130
useEffect(() => {
22-
if (game?.currentTime !== undefined) {
31+
if (game?.currentTime !== undefined && game?.currentTime !== 0) {
2332
// 사용자가 속한 팀과 사용자 데이터 검색
2433
const user = game.teams
2534
.flatMap((team) => team.users)
@@ -42,8 +51,8 @@ const IndividualChart: React.FC = () => {
4251
{
4352
label: `My Click Count`,
4453
data: userScores,
45-
borderColor: "rgba(54, 162, 235, 1)",
46-
backgroundColor: "rgba(54, 162, 235, 0.2)",
54+
borderColor: 'rgba(54, 162, 235, 1)',
55+
backgroundColor: 'rgba(54, 162, 235, 0.2)',
4756
tension: 0.4,
4857
},
4958
],
@@ -55,7 +64,7 @@ const IndividualChart: React.FC = () => {
5564
maintainAspectRatio: false,
5665
plugins: {
5766
legend: {
58-
position: "top" as const,
67+
position: 'top' as const,
5968
},
6069
title: {
6170
display: true,
@@ -66,14 +75,14 @@ const IndividualChart: React.FC = () => {
6675
x: {
6776
title: {
6877
display: true,
69-
text: "Time (s)",
78+
text: 'Time (s)',
7079
},
7180
},
7281
y: {
7382
beginAtZero: true,
7483
title: {
7584
display: true,
76-
text: "Click Count",
85+
text: 'Click Count',
7786
},
7887
},
7988
},

src/components/chart/TeamChart.tsx

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useEffect, useState } from "react";
2-
import { useRecoilValue } from "recoil";
3-
import { gameState } from "../../recoil/atoms/gameState";
4-
import { Line } from "react-chartjs-2";
1+
import React, { useEffect, useState } from 'react';
2+
import { useRecoilValue } from 'recoil';
3+
import { gameState } from '../../recoil/atoms/gameState';
4+
import { Line } from 'react-chartjs-2';
55
import {
66
Chart as ChartJS,
77
CategoryScale,
@@ -11,8 +11,8 @@ import {
1111
Title,
1212
Tooltip,
1313
Legend,
14-
} from "chart.js";
15-
import { RoomDataProps } from "../../types/RoomData.type.ts";
14+
} from 'chart.js';
15+
import { RoomDataProps } from '../../types/RoomData.type.ts';
1616

1717
// Chart.js 모듈 등록
1818
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend);
@@ -23,33 +23,36 @@ const TeamChart: React.FC = () => {
2323
const [teamScores, setTeamScores] = useState<{ [key: string]: number[] }>({}); // 팀 점수 저장
2424

2525
useEffect(() => {
26-
if (game?.currentTime !== undefined && game?.teams) {
26+
if (game?.currentTime !== undefined && game?.currentTime !== 0 && game?.teams) {
2727
// X축 레이블에 현재 시간을 추가
2828
setTimeLabels((prev) => [...prev, `${game.currentTime}s`]);
2929

30-
// 게임 타입별로 다르게 처리
30+
// 게임 타입별로 점수 계산 및 업데이트
3131
if (game?.gameType === 'FREE_FOR_ALL') {
32-
const updatedScores = { ...teamScores };
33-
// team.users에서 각 사용자의 클릭 수 합산
34-
game.teams[0].users.forEach((user) => {
35-
if (!updatedScores[user.nickname]) {
36-
updatedScores[user.nickname] = [];
37-
}
38-
updatedScores[user.nickname].push(user.clickCount);
32+
setTeamScores((prevScores) => {
33+
const updatedScores = { ...prevScores };
34+
game.teams[0].users.forEach((user) => {
35+
if (!updatedScores[user.nickname]) {
36+
updatedScores[user.nickname] = []; // 새로운 유저 초기화
37+
}
38+
// 이전 값 유지하며 새로운 값 추가
39+
updatedScores[user.nickname] = [...updatedScores[user.nickname], user.clickCount];
40+
});
41+
return updatedScores;
3942
});
40-
setTeamScores(updatedScores);
41-
} else{
42-
// 팀별 클릭 수 합산
43-
const updatedScores = { ...teamScores };
44-
game.teams.forEach((team) => {
45-
const totalClicks = team.teamScore
46-
if (!updatedScores[team.teamName]) {
47-
updatedScores[team.teamName] = [];
48-
}
49-
updatedScores[team.teamName].push(totalClicks);
43+
} else {
44+
setTeamScores((prevScores) => {
45+
const updatedScores = { ...prevScores };
46+
game.teams.forEach((team) => {
47+
const totalClicks = team.teamScore;
48+
if (!updatedScores[team.teamName]) {
49+
updatedScores[team.teamName] = []; // 새로운 팀 초기화
50+
}
51+
// 이전 값 유지하며 새로운 값 추가
52+
updatedScores[team.teamName] = [...updatedScores[team.teamName], totalClicks];
53+
});
54+
return updatedScores;
5055
});
51-
52-
setTeamScores(updatedScores);
5356
}
5457
}
5558
}, [game?.currentTime]); // currentTime
@@ -72,25 +75,25 @@ const TeamChart: React.FC = () => {
7275
maintainAspectRatio: false,
7376
plugins: {
7477
legend: {
75-
position: "top" as const,
78+
position: 'top' as const,
7679
},
7780
title: {
7881
display: true,
79-
text: "Click Score Chart",
82+
text: 'Click Score Chart',
8083
},
8184
},
8285
scales: {
8386
x: {
8487
title: {
8588
display: true,
86-
text: "Time (s)",
89+
text: 'Time (s)',
8790
},
8891
},
8992
y: {
9093
beginAtZero: true,
9194
title: {
9295
display: true,
93-
text: "Team Click Score",
96+
text: 'Team Click Score',
9497
},
9598
},
9699
},

0 commit comments

Comments
 (0)