Skip to content

Commit b47a24d

Browse files
committed
Colors gradient for student stats
1 parent 3d0f316 commit b47a24d

6 files changed

Lines changed: 31 additions & 12 deletions

File tree

hwproj.front/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hwproj.front/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"axios": "^0.21.2",
2525
"bootstrap": "^4.3.1",
2626
"classnames": "^2.3.1",
27+
"color-util": "^2.2.3",
2728
"date-fns": "^2.23.0",
2829
"dayjs": "^1.10.6",
2930
"dotenv": "^10.0.0",

hwproj.front/src/components/Courses/StudentStats.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class StudentStats extends React.Component<IStudentStatsProps, IStudentStatsStat
102102
forMentor={this.props.isMentor}
103103
studentId={String(cm.id)}
104104
taskId={task.id!}
105-
/>
105+
taskMaxRating={task.maxRating!}/>
106106
))
107107
)}
108108
</TableRow>

hwproj.front/src/components/Solutions/TaskSolutionsPage.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ const TaskSolutionsPage: FC<RouteComponentProps<ITaskSolutionsProps>> = (props)
9898
forMentor={false}
9999
task={taskSolution.task}
100100
studentId={userId as string}
101-
maxRating={taskSolution.task!.maxRating!}
102101
/>
103102
</Grid>
104103
)}
@@ -128,7 +127,6 @@ const TaskSolutionsPage: FC<RouteComponentProps<ITaskSolutionsProps>> = (props)
128127
forMentor={false}
129128
task={taskSolution.task}
130129
studentId={userId as string}
131-
maxRating={taskSolution.task!.maxRating!}
132130
/>
133131
</div>
134132
<div style={{marginTop: "10px"}}>

hwproj.front/src/components/Tasks/StudentStatsCell.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import TableCell from "@material-ui/core/TableCell";
33
import {Redirect} from "react-router-dom";
44
import {Solution, StatisticsCourseSolutionsModel} from "api";
55
import {Chip, Stack} from "@mui/material";
6+
import {colorBetween} from "../../services/JsUtils";
67

78
interface ITaskStudentCellProps {
89
studentId: string;
910
taskId: number;
1011
forMentor: boolean;
1112
userId: string;
13+
taskMaxRating: number;
1214
solutions?: StatisticsCourseSolutionsModel[];
1315
}
1416

@@ -89,18 +91,19 @@ export default class StudentStatsCell extends React.Component<ITaskStudentCellPr
8991
this.setState({redirectForStudent: true});
9092
}
9193

92-
getCellBackgroundColor = (state: Solution.StateEnum | undefined, isFirstTry: boolean): string => {
94+
95+
getCellBackgroundColor = (state: Solution.StateEnum | undefined,
96+
rating: number | undefined, maxRating: number,
97+
isFirstTry: boolean): string => {
9398
if (state == Solution.StateEnum.NUMBER_0)
94-
return isFirstTry ? "#d0fcc7" : "#ffeb99"
95-
if (state == Solution.StateEnum.NUMBER_1)
96-
return "#ffc346"
97-
if (state == Solution.StateEnum.NUMBER_2)
98-
return "#7ad67a"
99-
return "#ffffff"
99+
return isFirstTry ? "#d0fcc7" : "#afeeee"
100+
return rating !== undefined
101+
? colorBetween(0xff0000, 0x2cba00, rating / maxRating * 100)
102+
: "#ffffff"
100103
}
101104

102105
async componentDidMount() {
103-
const solutions = this.props.solutions
106+
const {solutions, taskMaxRating} = this.props
104107
const ratedSolutions = solutions!.filter(x => x.state != Solution.StateEnum.NUMBER_0)
105108
const ratedSolutionsCount = ratedSolutions.length
106109
const isFirstUnratedTry = ratedSolutionsCount === 0
@@ -116,7 +119,7 @@ export default class StudentStatsCell extends React.Component<ITaskStudentCellPr
116119
return
117120
}
118121
this.setState({
119-
color: this.getCellBackgroundColor(lastSolution.state, isFirstUnratedTry),
122+
color: this.getCellBackgroundColor(lastSolution.state, lastSolution.rating, taskMaxRating, isFirstUnratedTry),
120123
isLoaded: true,
121124
lastRatedSolution: lastRatedSolution,
122125
ratedSolutionsCount: ratedSolutions.length
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import colorutil from "color-util";
2+
3+
export const colorBetween = (color1, color2, position) => {
4+
const ratingGradient = colorutil.hsv.gradient({
5+
type: "linear",
6+
colors: [
7+
colorutil.color(color1).hsv,
8+
colorutil.color(color2).hsv
9+
]
10+
})
11+
return colorutil.rgb.to.hex(colorutil.hsv.to.rgb(ratingGradient(position, 0)))
12+
}

0 commit comments

Comments
 (0)