Files
Academic-Affairs-System/web/src/views/CourseGradeStatisticsView.vue
T

301 lines
14 KiB
Vue

<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { ArrowLeft, Refresh } from '@element-plus/icons-vue'
import * as echarts from 'echarts/core'
import { BarChart, LineChart } from 'echarts/charts'
import { AriaComponent, GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
import { CanvasRenderer } from 'echarts/renderers'
import { useRoute, useRouter } from 'vue-router'
import http, { apiErrorMessage } from '../api/http'
import CourseDiscussionPanel from '../components/CourseDiscussionPanel.vue'
echarts.use([
AriaComponent,
BarChart,
CanvasRenderer,
GridComponent,
LegendComponent,
LineChart,
TooltipComponent,
])
const route = useRoute()
const router = useRouter()
const { t } = useI18n()
const loading = ref(false)
const report = ref<any | null>(null)
const activeScope = ref('class')
const chartElement = ref<HTMLElement>()
const comparisonChartElement = ref<HTMLElement>()
let chart: echarts.ECharts | undefined
let comparisonChart: echarts.ECharts | undefined
const rows = computed(() => {
const classRows = report.value?.classes?.length
? report.value.classes.map((item: any) => ({
key: `class-${item.classId}`,
label: item.className,
caption: t('courseStats.classStudents'),
value: item.statistics,
}))
: [{ key: 'class', label: t('courseStats.myClass'), caption: t('courseStats.classmates'), value: report.value?.class }]
return [
...classRows,
{ key: 'major', label: t('courseStats.myMajor'), caption: t('courseStats.majorStudents'), value: report.value?.major },
{ key: 'college', label: t('courseStats.myCollege'), caption: t('courseStats.collegeStudents'), value: report.value?.college },
{ key: 'university', label: t('courseStats.university'), caption: t('courseStats.universityStudents'), value: report.value?.university },
].filter(item => item.value)
})
const selectedRow = computed(() => rows.value.find(item => item.key === activeScope.value) ?? rows.value[0])
const selectedDistribution = computed(() => selectedRow.value?.value.distribution ?? [])
const recommendation = computed(() => {
const value = selectedRow.value?.value
if (!value) return ''
if (Number(value.passRate) < 70) return t('courseStats.insights.lowPass', { rate: score(value.passRate) })
if (Number(value.averageScore) < 70) return t('courseStats.insights.lowAverage', { average: score(value.averageScore) })
if (Number(value.standardDeviation) > 20) return t('courseStats.insights.highDeviation')
return t('courseStats.insights.stable', { average: score(value.averageScore), rate: score(value.passRate) })
})
function score(value: unknown) {
return Number(value).toFixed(1)
}
async function load() {
loading.value = true
try {
report.value = (await http.get(`/grades/sheets/${route.params.sheetId}/statistics`)).data
if (!rows.value.some(item => item.key === activeScope.value)) activeScope.value = rows.value[0]?.key ?? 'class'
await nextTick()
drawChart()
drawComparisonChart()
} catch (error) {
ElMessage.error(apiErrorMessage(error))
} finally {
loading.value = false
}
}
function drawChart() {
if (!chartElement.value || !selectedRow.value) return
chart?.dispose()
chart = echarts.init(chartElement.value)
const distribution = selectedDistribution.value
const total = selectedRow.value.value.studentCount || 1
const palette = ['#a7b4c6', '#8198b6', '#5f7fa8', '#3d6797', '#173f78']
chart.setOption({
animationDuration: 520,
aria: { enabled: true, decal: { show: true } },
tooltip: {
trigger: 'axis',
formatter: (params: any) => {
const item = distribution[params[0]?.dataIndex ?? 0]
const rate = item ? Number(item.count) / total * 100 : 0
return t('courseStats.chart.distributionTooltip', { range: item?.range ?? '', count: item?.count ?? 0, rate: rate.toFixed(1) })
},
},
grid: { left: 42, right: 22, top: 44, bottom: 38 },
xAxis: {
type: 'category',
name: t('courseStats.scoreBand'),
data: distribution.map((item: any) => item.range),
axisTick: { show: false },
axisLine: { lineStyle: { color: '#cbd4e1' } },
axisLabel: { color: '#475467', fontWeight: 600 },
},
yAxis: {
type: 'value', min: 0, minInterval: 1, name: t('courseStats.people'),
splitLine: { lineStyle: { color: '#edf1f6' } },
},
series: [
{
name: t('courseStats.scopePeople', { scope: selectedRow.value.label }), type: 'bar', barMaxWidth: 62,
data: distribution.map((item: any, index: number) => ({ value: item.count, itemStyle: { color: palette[index] } })),
itemStyle: { borderRadius: [5, 5, 0, 0] },
label: {
show: true, position: 'top', color: '#344054', fontWeight: 700,
formatter: (params: any) => {
const count = Number(params.value)
return t('courseStats.chart.barLabel', { count, rate: (count / total * 100).toFixed(1) })
},
},
},
],
})
}
function drawComparisonChart() {
if (!comparisonChartElement.value || !rows.value.length) return
comparisonChart?.dispose()
comparisonChart = echarts.init(comparisonChartElement.value)
const selectedIndex = rows.value.findIndex(item => item.key === activeScope.value)
comparisonChart.setOption({
animationDuration: 520,
aria: { enabled: true, decal: { show: true } },
tooltip: {
trigger: 'axis',
formatter: (params: any) => {
const item = rows.value[params[0]?.dataIndex ?? 0]
return t('courseStats.chart.comparisonTooltip', { scope: item.label, highest: score(item.value.highestScore), average: score(item.value.averageScore), lowest: score(item.value.lowestScore), rate: score(item.value.passRate) })
},
},
legend: { top: 2, data: [t('courseStats.scoreRange'), t('courseStats.average'), t('courseStats.passRate')] },
grid: { left: 42, right: 52, top: 56, bottom: 36 },
xAxis: {
type: 'category',
data: rows.value.map(item => item.label),
axisTick: { show: false },
axisLine: { lineStyle: { color: '#cbd4e1' } },
axisLabel: {
color: (value: string) => value === selectedRow.value?.label ? '#173f78' : '#667085',
fontWeight: 600,
},
},
yAxis: [
{ type: 'value', min: 0, max: 100, name: t('courseStats.score'), splitLine: { lineStyle: { color: '#edf1f6' } } },
{ type: 'value', min: 0, max: 100, name: t('courseStats.passRate'), axisLabel: { formatter: '{value}%' }, splitLine: { show: false } },
],
series: [
{
name: t('courseStats.rangeStart'), type: 'bar', stack: 'score-range', silent: true,
itemStyle: { color: 'transparent' }, emphasis: { disabled: true },
data: rows.value.map(item => item.value.lowestScore),
},
{
name: t('courseStats.scoreRange'), type: 'bar', stack: 'score-range', barWidth: 22,
itemStyle: { borderRadius: 8 },
data: rows.value.map((item, index) => ({
value: item.value.highestScore - item.value.lowestScore,
itemStyle: { color: index === selectedIndex ? '#315a9b' : '#b8c7dc' },
})),
},
{
name: t('courseStats.average'), type: 'line', smooth: true, symbol: 'circle', symbolSize: 10,
lineStyle: { width: 3, color: '#d58b32' },
itemStyle: { color: '#d58b32', borderColor: '#fff', borderWidth: 2 },
data: rows.value.map(item => item.value.averageScore),
},
{
name: t('courseStats.passRate'), type: 'line', yAxisIndex: 1, smooth: true, symbol: 'diamond', symbolSize: 9,
lineStyle: { width: 2, type: 'dashed', color: '#25847a' },
itemStyle: { color: '#25847a' },
data: rows.value.map(item => item.value.passRate),
},
],
})
}
watch(activeScope, async () => {
await nextTick()
drawChart()
drawComparisonChart()
})
onMounted(load)
onBeforeUnmount(() => {
chart?.dispose()
comparisonChart?.dispose()
})
</script>
<template>
<div class="page-stack course-statistics" v-loading="loading">
<section class="page-intro">
<div>
<span class="section-kicker">COURSE INSIGHT</span>
<h2>{{ report?.courseName ?? t('courseStats.title') }}</h2><p>{{ report?.courseCode }} · {{ report?.termName }} · {{ t('courseStats.publishedOnly') }}</p>
</div>
<div class="actions">
<el-button :icon="ArrowLeft" @click="router.back()">{{ t('courseStats.back') }}</el-button><el-button :icon="Refresh" @click="load">{{ t('courseStats.refresh') }}</el-button>
</div>
</section>
<el-alert v-if="report?.isRefreshing" type="info" :closable="false"
:title="t('courseStats.generating')" />
<section v-if="rows.length && selectedRow" class="scope-panel">
<nav class="scope-tabs" :aria-label="t('courseStats.scope')">
<button
v-for="item in rows"
:key="item.key"
type="button"
:class="{ active: activeScope === item.key }"
@click="activeScope = item.key"
>
<span>{{ item.label }}</span>
<small>{{ t('courseStats.peopleCount', { count: item.value.studentCount }) }}</small>
</button>
</nav>
<article class="scope-summary">
<div class="average-score">
<span>{{ t('courseStats.scopeAverage', { scope: selectedRow.label }) }}</span>
<strong>{{ score(selectedRow.value.averageScore) }}</strong>
<small>{{ t('courseStats.population', { caption: selectedRow.caption, count: selectedRow.value.studentCount }) }}</small>
</div>
<dl>
<div><dt>{{ t('courseStats.highest') }}</dt><dd>{{ score(selectedRow.value.highestScore) }}</dd></div><div><dt>{{ t('courseStats.lowest') }}</dt><dd>{{ score(selectedRow.value.lowestScore) }}</dd></div><div><dt>{{ t('courseStats.passed') }}</dt><dd>{{ selectedRow.value.passedCount }} / {{ selectedRow.value.studentCount }}</dd></div><div><dt>{{ t('courseStats.passRate') }}</dt><dd>{{ score(selectedRow.value.passRate) }}%</dd></div>
</dl>
</article>
</section>
<section v-if="recommendation" class="action-advice"><span>ACTION ADVICE</span><p>{{ recommendation }}</p></section>
<el-empty v-else-if="!loading" :description="t('courseStats.empty')" />
<section v-if="rows.length" class="chart-panel">
<div class="chart-heading">
<div><span class="section-kicker">SCORE DISTRIBUTION</span><h3>{{ t('courseStats.distributionTitle', { scope: selectedRow?.label }) }}</h3></div><p>{{ t('courseStats.distributionDescription', { count: selectedRow?.value.studentCount }) }}</p>
</div>
<div ref="chartElement" class="score-chart" />
</section>
<section v-if="rows.length" class="chart-panel comparison-panel">
<div class="chart-heading">
<div><span class="section-kicker">LEVEL COMPARISON</span><h3>{{ t('courseStats.comparisonTitle') }}</h3></div><p>{{ t('courseStats.comparisonDescription') }}</p>
</div>
<div ref="comparisonChartElement" class="score-chart" />
</section>
<CourseDiscussionPanel v-if="report && rows.length" :sheet-id="String(route.params.sheetId)" :course-name="report.courseName" />
</div>
</template>
<style scoped>
.actions { display: flex; gap: 10px; }
.scope-panel, .chart-panel { background: var(--el-bg-color); border: 1px solid var(--el-border-color-lighter); box-shadow: 0 10px 30px rgb(21 47 82 / 5%); }
.scope-tabs { display: grid; grid-template-columns: repeat(4, 1fr); border-bottom: 1px solid #dfe5ee; background: #f6f8fb; }
.scope-tabs button { min-height: 58px; padding: 10px 18px; border: 0; border-right: 1px solid #e2e7ef; color: #667085; background: transparent; cursor: pointer; text-align: left; transition: color .2s ease, background .2s ease, box-shadow .2s ease; }
.scope-tabs button:last-child { border-right: 0; }
.scope-tabs button span, .scope-tabs button small { display: block; }
.scope-tabs button span { font-weight: 700; font-size: 14px; }
.scope-tabs button small { margin-top: 3px; color: #98a2b3; font: 11px Consolas, monospace; }
.scope-tabs button.active { color: #173f78; background: #fff; box-shadow: inset 0 3px #315a9b; }
.scope-tabs button:focus-visible { outline: 2px solid #315a9b; outline-offset: -3px; }
.scope-summary { display: grid; grid-template-columns: minmax(220px, .8fr) 1.6fr; gap: 36px; padding: 28px 30px; }
.average-score { padding-right: 30px; border-right: 1px solid #e3e8ef; }
.average-score span, .average-score small { display: block; color: #667085; }
.average-score span { font-size: 12px; font-weight: 700; }
.average-score strong { display: block; margin: 9px 0 7px; color: #173f78; font: 700 48px/1 Consolas, monospace; }
.average-score small { font-size: 11px; }
.scope-summary dl { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin: 0; align-items: stretch; }
.scope-summary dl div { padding: 14px 16px; background: #f7f9fc; border-left: 2px solid #d5deeb; }
.scope-summary dt { color: #667085; font-size: 11px; }
.scope-summary dd { margin: 8px 0 0; color: #243b5a; font: 700 20px/1.1 Consolas, monospace; }
.chart-panel { margin-top: 18px; padding: 22px 26px; }
.chart-heading { display: flex; align-items: end; justify-content: space-between; gap: 20px; }
.chart-heading h3 { margin: 4px 0 0; }
.chart-heading p { max-width: 420px; margin: 0; color: #667085; font-size: 12px; text-align: right; }
.score-chart { height: 350px; margin-top: 10px; }
@media (max-width: 760px) {
.page-intro { align-items: flex-start; }
.actions { flex-wrap: wrap; }
.scope-tabs { grid-template-columns: repeat(2, 1fr); }
.scope-tabs button:nth-child(2) { border-right: 0; }
.scope-tabs button:nth-child(-n + 2) { border-bottom: 1px solid #e2e7ef; }
.scope-summary { grid-template-columns: 1fr; gap: 20px; padding: 22px; }
.average-score { padding: 0 0 20px; border: 0; border-bottom: 1px solid #e3e8ef; }
.scope-summary dl { grid-template-columns: repeat(2, 1fr); }
.chart-heading { align-items: flex-start; flex-direction: column; }
.chart-heading p { text-align: left; }
}
</style>