-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from CheckMate-sookmyung/feat/stat
#247 feat: 그래프 차트 컴포넌트 구현
- Loading branch information
Showing
6 changed files
with
276 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
import styled, { css } from 'styled-components'; | ||
import { BREAKPOINTS } from '@/styles'; | ||
import styled from 'styled-components'; | ||
|
||
export const Wrapper = styled.div` | ||
export const ChartWrapper = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
margin: 20px 0; | ||
gap: 10px; | ||
width: calc(50% - 16px); | ||
@media (max-width: ${BREAKPOINTS[1]}px) { | ||
width: 100%; | ||
} | ||
`; | ||
|
||
export const Label = styled.div` | ||
position: relative; | ||
font-size: 28px; | ||
export const ChartTitle = styled.h2` | ||
font-size: 18px; | ||
font-weight: 600; | ||
color: #0d0d0d; | ||
text-align: left; | ||
`; | ||
|
||
export const Box = styled.div` | ||
max-width: 100%; | ||
position: relative; | ||
export const Chart = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
border-radius: 20px; | ||
border: 1px solid #aecfff; | ||
box-sizing: border-box; | ||
overflow: auto; | ||
padding: 20px; | ||
margin: 28px 0; | ||
height: 332px; | ||
* { | ||
max-width: 100%; | ||
height: auto; | ||
display: block; | ||
} | ||
background: #fff; | ||
padding: 0 30px; | ||
width: 100%; | ||
height: 100%; | ||
max-height: 300px; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
import React, { useContext } from 'react'; | ||
import { Bar } from 'react-chartjs-2'; | ||
import { SortedStudent } from './TotalStatisticsPage'; | ||
import { | ||
Chart as ChartJS, | ||
BarElement, | ||
Title, | ||
Tooltip, | ||
Legend as ChartLegend, | ||
CategoryScale, | ||
LinearScale, | ||
} from 'chart.js'; | ||
import { GraphBox } from '@/components'; | ||
import styled from 'styled-components'; | ||
import { BsThreeDots } from 'react-icons/bs'; | ||
|
||
ChartJS.register( | ||
BarElement, | ||
Title, | ||
Tooltip, | ||
ChartLegend, | ||
CategoryScale, | ||
LinearScale, | ||
); | ||
|
||
const ChartContent = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
padding: 30px 0; | ||
`; | ||
|
||
const ChartArea = styled.div` | ||
display: flex; | ||
flex: 1; | ||
min-width: 300px; | ||
max-width: 600px; | ||
padding-right: 40px; | ||
`; | ||
|
||
const LegendContainer = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-around; | ||
gap: 10px; | ||
`; | ||
|
||
const LegendItem = styled.div` | ||
display: flex; | ||
align-items: center; | ||
font-size: 16px; | ||
color: #323232; | ||
gap: 10px; | ||
`; | ||
|
||
const LegendCircle = styled.div` | ||
width: 16px; | ||
height: 16px; | ||
background-color: ${(props) => props.color}; | ||
border-radius: 50%; | ||
margin-right: 10px; | ||
`; | ||
|
||
const LegendText = styled.span` | ||
color: #323232; | ||
flex-grow: 1; | ||
`; | ||
|
||
const LegendDots = styled(BsThreeDots)` | ||
font-size: 16px; | ||
color: #5495f6; | ||
display: flex; | ||
align-items: center; | ||
`; | ||
|
||
const LegendPercentage = styled.span` | ||
font-size: 16px; | ||
color: #2f7cef; | ||
display: flex; | ||
align-items: center; | ||
`; | ||
|
||
const HiddenLegend = styled.div` | ||
display: block; | ||
@media (max-width: 1024px) { | ||
display: none; | ||
} | ||
`; | ||
|
||
const Legend = ({ topStudents, ratedColors }) => ( | ||
<LegendContainer> | ||
{topStudents.map((student, index) => ( | ||
<LegendItem key={student.studentName}> | ||
<LegendCircle color={ratedColors[index]} /> | ||
<LegendText>{student.studentName}</LegendText> | ||
<LegendDots /> | ||
<LegendPercentage>{`(${student.attendanceRate}%)`}</LegendPercentage> | ||
</LegendItem> | ||
))} | ||
</LegendContainer> | ||
); | ||
|
||
const GraphChart = () => { | ||
const studentData = useContext(SortedStudent); | ||
|
||
if (!studentData || studentData.length === 0) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
// 데이터를 직접 계산 | ||
const sortedStudents = [...studentData].sort( | ||
(a, b) => b.attendanceRate - a.attendanceRate, | ||
); | ||
const topStudents = sortedStudents.slice(0, 5); | ||
|
||
const labels = topStudents.map((student) => student.studentName); | ||
const attendanceRates = topStudents.map((student) => student.attendanceRate); | ||
|
||
const ratedColors = ['#1E88E5', '#42A5F5', '#90CAF9', '#BBDEFB', '#E3F2FD']; | ||
|
||
const graphData = { | ||
labels, | ||
datasets: [ | ||
{ | ||
label: '출석률', | ||
data: attendanceRates, | ||
backgroundColor: ratedColors, | ||
borderWidth: 1, | ||
datalabels: { | ||
color: '#fff', | ||
align: 'end', | ||
anchor: 'end', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const options = { | ||
responsive: true, | ||
maintainAspectRatio: false, | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
tooltip: { | ||
backgroundColor: '#fff', | ||
titleColor: '#000', | ||
bodyColor: '#000', | ||
borderColor: '#ccc', | ||
borderWidth: 1, | ||
callbacks: { | ||
label: (context) => { | ||
const label = context.label || ''; | ||
const value = context.raw || 0; | ||
return `${label}: ${value}%`; | ||
}, | ||
}, | ||
}, | ||
datalabels: { | ||
display: true, | ||
color: 'white', | ||
font: { | ||
weight: 'bold', | ||
size: 14, | ||
}, | ||
}, | ||
}, | ||
scales: { | ||
y: { | ||
beginAtZero: true, | ||
max: 100, | ||
ticks: { | ||
stepSize: 20, | ||
font: { | ||
size: 12, | ||
}, | ||
}, | ||
title: { | ||
display: false, | ||
}, | ||
}, | ||
x: { | ||
grid: { | ||
display: true, | ||
}, | ||
ticks: { | ||
font: { | ||
size: 12, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
return ( | ||
<GraphBox title="출석률 좋은 학생 TOP 5"> | ||
<ChartContent> | ||
<ChartArea> | ||
<Bar data={graphData} options={options} /> | ||
</ChartArea> | ||
<HiddenLegend> | ||
<Legend topStudents={topStudents} ratedColors={ratedColors} /> | ||
</HiddenLegend> | ||
</ChartContent> | ||
</GraphBox> | ||
); | ||
}; | ||
|
||
export default GraphChart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { AttendeeTable } from '@/components'; | ||
import React from 'react'; | ||
|
||
const TableChart = () => { | ||
return ( | ||
<div> | ||
<AttendeeTable /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TableChart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters