import { PropType, defineComponent, nextTick, onMounted, ref, toRefs, watch } from 'vue'; import styles from '../index.module.less'; import icons from '../icons.json'; import { Badge, Image, Skeleton, SkeletonImage, SkeletonParagraph } from 'vant'; import * as echarts from 'echarts'; import { IGradeDistribution } from '../type'; import icon_1 from '../image/icon_1.png'; type EChartsOption = echarts.EChartsOption; const colors = [ ['#94C2FD', '#5B8FF9'], ['#FBE031', '#F6BD16'], ['#93EED2', '#5AD8A6'], ['#F5A181', '#E8684A'], ['#96A9C4', '#5D7092'], ['#A6E6F7', '#6DC8EC'], ['#FFCA7D', '#FF9530'], ['#DBC6FF', '#B87BDD'], ['#D2FFC4', '#92DE97'], ['#94C2FD', '#5B8FF9'], ['#FBE031', '#F6BD16'], ['#93EED2', '#5AD8A6'], ['#F5A181', '#E8684A'], ['#96A9C4', '#5D7092'], ['#A6E6F7', '#6DC8EC'], ['#FFCA7D', '#FF9530'], ['#DBC6FF', '#B87BDD'], ['#D2FFC4', '#92DE97'], ['#94C2FD', '#5B8FF9'], ['#FBE031', '#F6BD16'], ['#93EED2', '#5AD8A6'], ['#F5A181', '#E8684A'], ['#96A9C4', '#5D7092'], ['#A6E6F7', '#6DC8EC'], ['#FFCA7D', '#FF9530'], ['#DBC6FF', '#B87BDD'], ['#D2FFC4', '#92DE97'] ]; export default defineComponent({ name: 'CurrentStudent', props: { list: { type: Array as PropType, default: () => [] } }, setup(props) { const firstInit = ref(false); const echratsRef = ref(); const { list } = toRefs(props); watch( () => list.value, () => { firstInit.value = true; nextTick(() => { handleInit(); }); } ); let myChart: echarts.ECharts; const handleInit = () => { if (!list.value.length) return; if (myChart) { myChart.dispose(); } myChart = echarts.init(echratsRef.value); const option: EChartsOption = { title: { text: list.value .reduce((total, item: IGradeDistribution) => { total += item.studentNum; return total; }, 0) .toString(), subtext: '在读人数', textAlign: 'center', left: '48%', top: '36%', textStyle: { fontSize: '22px', fontWeight: 'bold', color: '#333', fontFamily: 'DINAlternate-Bold, DINAlternate' }, subtextStyle: { fontSize: '12px', color: '#777' } }, tooltip: { trigger: 'item', confine: true, borderWidth: 0 }, series: [ { type: 'pie', radius: ['40%', '62%'], itemStyle: { borderRadius: 2, borderColor: '#fff', borderWidth: 1 }, label: { show: false }, labelLine: { show: false }, avoidLabelOverlap: false, data: list.value.map((item: IGradeDistribution, index: number) => { return { value: item.studentNum, name: item.grade, itemStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: colors[index][0] }, { offset: 1, color: colors[index][1] } ]) } }; }) } ] }; option && myChart.setOption(option); }; return () => (
在读学员
年级分布
(单位:人)
{!firstInit.value && ( {{ template: () => ( <>
) }}
)}
{list.value.map((item: IGradeDistribution, i: number) => (
{item.grade}
{item.studentNum}
))}
{firstInit.value && !list.value.length && (
)}
); } });