-
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.
- Loading branch information
1 parent
4917ff7
commit 1d729b8
Showing
6 changed files
with
328 additions
and
91 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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { Doughnut } from 'react-chartjs-2'; | ||
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'; | ||
import * as S from './EventsGraphChart.style'; | ||
import { axiosInstance } from '@/axios'; | ||
|
||
ChartJS.register(ArcElement, Tooltip, Legend); | ||
|
||
const EventsGraphChart = () => { | ||
const [rankedEvents, setRankedEvents] = useState([]); | ||
|
||
useEffect(() => { | ||
const fetchStatistics = async () => { | ||
try { | ||
const eventsResponse = await axiosInstance.get( | ||
`/api/v1/events/statistic/event`, | ||
); | ||
|
||
const top3Events = eventsResponse.data.slice(0, 3); | ||
|
||
const coloredEvents = top3Events.map((event, index) => { | ||
let color = ''; | ||
switch (index) { | ||
case 0: | ||
color = '#333'; | ||
break; | ||
case 1: | ||
color = '#1E88E5'; | ||
break; | ||
case 2: | ||
color = '#4CAF50'; | ||
break; | ||
default: | ||
color = '#E4E4E4'; | ||
} | ||
return { ...event, color }; | ||
}); | ||
|
||
setRankedEvents(coloredEvents); | ||
} catch (error) { | ||
console.error('Error fetching statistics:', error); | ||
} | ||
}; | ||
|
||
fetchStatistics(); | ||
}, []); | ||
|
||
const createEventDoughnutData = (rating, color) => ({ | ||
labels: ['참석', '미참석'], | ||
datasets: [ | ||
{ | ||
data: [Math.floor(rating), Math.floor(100 - rating)], | ||
backgroundColor: [color, '#E0E0E0'], | ||
hoverBackgroundColor: [color, '#E0E0E0'], | ||
borderWidth: 0, | ||
}, | ||
], | ||
}); | ||
|
||
const doughnutOptions = { | ||
responsive: true, | ||
maintainAspectRatio: false, | ||
cutout: '30%', | ||
plugins: { | ||
legend: { | ||
display: false, | ||
position: 'right', | ||
}, | ||
tooltip: { | ||
callbacks: { | ||
label: (tooltipItem) => { | ||
const label = tooltipItem.label || ''; | ||
const value = tooltipItem.raw || 0; | ||
return `${label}: ${value}%`; | ||
}, | ||
}, | ||
}, | ||
datalabels: { | ||
color: '#000', | ||
anchor: 'center', | ||
align: 'center', | ||
textAlign: 'center', | ||
formatter: (value, context) => { | ||
if (typeof value === 'number') { | ||
const percentage = value.toFixed(0); | ||
const label = context.chart.data.labels[context.dataIndex]; | ||
return `${label} \n ${percentage}%`; | ||
} | ||
return ''; | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
return ( | ||
<S.GraphContainer> | ||
{rankedEvents.map((event, index) => ( | ||
<S.GraphBoxWrapper key={event.eventTitle}> | ||
<S.LeftSide> | ||
<S.TopBadge>TOP {index + 1}</S.TopBadge> | ||
<S.EventTitle>{event.eventTitle}</S.EventTitle> | ||
<S.AttendanceInfo> | ||
<S.AttendancePercentage> | ||
{Math.floor(event.eventRating)}% | ||
</S.AttendancePercentage> | ||
<S.AttendanceLabel>참석</S.AttendanceLabel> | ||
</S.AttendanceInfo> | ||
</S.LeftSide> | ||
<S.ChartWrapper> | ||
<Doughnut | ||
data={createEventDoughnutData(event.eventRating, event.color)} | ||
options={doughnutOptions} | ||
/> | ||
</S.ChartWrapper> | ||
</S.GraphBoxWrapper> | ||
))} | ||
</S.GraphContainer> | ||
); | ||
}; | ||
|
||
export default EventsGraphChart; |
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,86 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const GraphContainer = styled.div` | ||
display: flex; | ||
justify-content: space-around; | ||
padding: 30px 0; | ||
gap: 30px; | ||
@media (max-width: 768px) { | ||
flex-direction: column; | ||
gap: 20px; | ||
} | ||
`; | ||
|
||
export const GraphBoxWrapper = styled.div` | ||
flex: 1; | ||
display: flex; | ||
border: 1px solid #aecfff; | ||
border-radius: 20px; | ||
padding: 30px; | ||
position: relative; | ||
max-width: 400px; | ||
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); | ||
@media (max-width: 768px) { | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
`; | ||
|
||
export const LeftSide = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-start; | ||
justify-content: center; | ||
flex: 1; | ||
`; | ||
|
||
export const TopBadge = styled.div` | ||
position: absolute; | ||
top: -20px; | ||
right: 10%; | ||
background-color: #e3f2fd; | ||
color: #1e88e5; | ||
padding: 10px 30px; | ||
border-radius: 18px; | ||
font-size: 20px; | ||
font-weight: bold; | ||
border: 1px solid #90caf9; | ||
transform: translateX(20%); | ||
`; | ||
|
||
export const EventTitle = styled.h3` | ||
font-size: 18px; | ||
font-weight: 600; | ||
color: #333; | ||
margin-bottom: 10px; | ||
text-align: left; | ||
`; | ||
|
||
export const AttendanceInfo = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-start; | ||
gap: 5px; | ||
`; | ||
|
||
export const AttendancePercentage = styled.div` | ||
font-size: 36px; | ||
font-weight: bold; | ||
color: #333; | ||
`; | ||
|
||
export const AttendanceLabel = styled.div` | ||
font-size: 20px; | ||
font-weight: 600; | ||
color: #323232; | ||
`; | ||
|
||
export const ChartWrapper = styled.div` | ||
width: 150px; | ||
height: 150px; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
`; |
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
Oops, something went wrong.