-
Notifications
You must be signed in to change notification settings - Fork 6
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
a8b4682
commit 6be91bd
Showing
9 changed files
with
281 additions
and
15 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
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
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,64 @@ | ||
import { GradeStatistic } from "../lib/model" | ||
import ReactEcharts from "echarts-for-react"; | ||
|
||
export default function GradeStatisticChart({ statistic }: { | ||
statistic: GradeStatistic | ||
}) { | ||
const computeGradeDistribution = (grades: number[]) => { | ||
const gradeMap = new Map<number, number>(); | ||
if (grades.length === 0) { | ||
return gradeMap; | ||
} | ||
const step = 0.5; | ||
grades.map(grade => { | ||
let section = grade / step - (grade % step ? 0 : 1); | ||
let count = gradeMap.get(section) ?? 0; | ||
gradeMap.set(section, count + 1); | ||
}); | ||
|
||
return gradeMap; | ||
} | ||
|
||
const getGradeDistributionOptions = (grades: number[]) => { | ||
let distribution = computeGradeDistribution(grades); | ||
let maxSection = 0; | ||
for (let section of distribution.keys()) { | ||
if (section > maxSection) { | ||
maxSection = section; | ||
} | ||
} | ||
let labels = []; | ||
let values = []; | ||
const step = 0.5; | ||
|
||
for (let i = 0; i <= maxSection; i++) { | ||
labels.push(`${step * i}-${step * (i + 1)}`); | ||
values.push(distribution.get(i) ?? 0); | ||
} | ||
|
||
return { | ||
xAxis: { | ||
data: labels | ||
}, | ||
yAxis: {}, | ||
legend: { | ||
data: ['成绩分布'] | ||
}, | ||
series: [ | ||
{ | ||
name: '成绩分布', | ||
type: 'bar', | ||
data: values | ||
} | ||
] | ||
}; | ||
} | ||
|
||
const graded = statistic.grades.length; | ||
const average = graded > 0 ? statistic.grades.reduce((g1, g2) => g1 + g2) / graded : 0; | ||
const options = getGradeDistributionOptions(statistic.grades); | ||
|
||
return <>{statistic.total > 0 && <span><b>{graded}/{statistic.total}</b>个作业已记分(平均<b>{average.toFixed(2)}</b>分)</span>} | ||
{graded > 0 && <ReactEcharts option={options} />} | ||
</> | ||
} |
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.