Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Chart for displaying grades #44

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"typescript": "4.9.5"
},
"dependencies": {
"chart.js": "3.7.1",
"classnames": "2.3.2",
"cozy-bar": "^12.2.4",
"cozy-client": "^48.1.1",
Expand All @@ -67,6 +68,7 @@
"date-fns": "2.29.3",
"lodash": "4.17.21",
"react": "18.3.1",
"react-chartjs-2": "4.1.0",
"react-dom": "18.3.1",
"react-inspector": "5.1.1",
"react-router-dom": "6.23.1"
Expand Down
118 changes: 118 additions & 0 deletions src/components/Atoms/Grades/GradesChart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend
} from 'chart.js'
import React from 'react'
import { Bar } from 'react-chartjs-2'
import { getSubjectName } from 'src/format/subjectName'

import useBreakpoints from 'cozy-ui/transpiled/react/providers/Breakpoints'
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend)

const truncateLabel = (label, maxLength) => {
if (label.length > maxLength) {
return label.substring(0, maxLength) + '...'
}
return label
}

const GradesChart = ({ subjects }) => {
const { t } = useI18n()
const { isMobile } = useBreakpoints()

const primaryColor = getComputedStyle(
document.documentElement
).getPropertyValue('--primaryColor')
const primaryColorLightest = getComputedStyle(
document.documentElement
).getPropertyValue('--primaryColorLightest')
const maxColor = getComputedStyle(document.documentElement).getPropertyValue(
'--successColorLight'
)
const minColor = getComputedStyle(document.documentElement).getPropertyValue(
'--errorColorLight'
)

const data = {
labels: subjects.map(subject =>
truncateLabel(getSubjectName(subject.subject).pretty, 10)
),
datasets: [
{
label: t('Grades.values.class.title'),
data: subjects.map(subject => subject.aggregation?.avgClass ?? 0),
backgroundColor: primaryColorLightest,
borderRadius: 5
},
{
label: t('Grades.values.student.title'),
data: subjects.map(subject => subject.aggregation?.avgGrades ?? 0),
backgroundColor: primaryColor,
borderRadius: 5
},
{
label: t('Grades.values.max.title'),
data: subjects.map(subject => subject.aggregation?.maxClass ?? 0),
backgroundColor: maxColor + '60',
borderRadius: 5,
hidden: true
},
{
label: t('Grades.values.min.title'),
data: subjects.map(subject => subject.aggregation?.minClass ?? 0),
backgroundColor: minColor + '60',
borderRadius: 5,
hidden: true
}
]
}

const options = {
responsive: true,
plugins: {
legend: {
labels: {
font: {
size: 14,
family: 'Lato',
weight: '500'
}
}
},
title: {
display: false
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
grid: {
drawBorder: false // Masquer la bordure verticale gauche
}
}
}
}

return (
<div
style={{
padding: '16px'
}}
>
<Bar height={isMobile ? 150 : 70} data={data} options={options} />
</div>
)
}

export default GradesChart
11 changes: 11 additions & 0 deletions src/components/Views/GradesView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n'

import { GradeItem } from '../Atoms/Grades/GradeItem'
import GradesChart from '../Atoms/Grades/GradesChart'
import { GradesSubjectSubheader } from '../Atoms/Grades/GradesSubjectSubheader'
import { PeriodSelector } from '../Atoms/Grades/PeriodSelector'
import { TabTitle } from '../Atoms/TabTitle'
Expand Down Expand Up @@ -75,7 +76,7 @@
if (selectedPeriod && selectedYear) {
updateYear(selectedPeriod, selectedYear)
}
}, [selectedPeriod, selectedYear])

Check warning on line 79 in src/components/Views/GradesView.jsx

View workflow job for this annotation

GitHub Actions / Build and publish

React Hook useEffect has a missing dependency: 'updateYear'. Either include it or remove the dependency array

if (selectedPeriod === '' && periods.length > 0) {
setSelectedPeriod(periods[0])
Expand All @@ -96,7 +97,7 @@
if (!periods.includes(selectedPeriod)) {
setSelectedPeriod(periods[0])
}
}, [subjects])

Check warning on line 100 in src/components/Views/GradesView.jsx

View workflow job for this annotation

GitHub Actions / Build and publish

React Hook useEffect has missing dependencies: 'periods' and 'selectedPeriod'. Either include them or remove the dependency array

const periodSelectorProps = {
periodDropdownRef,
Expand All @@ -123,6 +124,16 @@
<PeriodSelector {...periodSelectorProps} />
</TabTitle>

{(subjects ?? []).length > 0 && !isLoading && (
<GradesChart
subjects={
subjects
? subjects.filter(subject => subject.title === selectedPeriod)
: []
}
/>
)}

{(subjects ?? []).length === 0 && !isLoading && (
<Empty
icon={CozyIcon}
Expand Down
Loading