practiceData.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import { Ref, defineComponent, onMounted, reactive, ref } from 'vue';
  2. import styles from '../index.module.less';
  3. import {
  4. NButton,
  5. NDataTable,
  6. NForm,
  7. NFormItem,
  8. NNumberAnimation,
  9. NSpace
  10. } from 'naive-ui';
  11. import numeral from 'numeral';
  12. import { useECharts } from '@/hooks/web/useECharts';
  13. import Pagination from '/src/components/pagination';
  14. import { getTrainingStat } from '../api';
  15. import { getTrainingStatList } from '@/views/classList/api'
  16. import {
  17. getNowDateAndMonday,
  18. getNowDateAndSunday,
  19. getTimes,
  20. getMinutes,
  21. getSecend
  22. } from '/src/utils/dateFormat';
  23. import CDatePicker from '/src/components/CDatePicker';
  24. export default defineComponent({
  25. name: 'student-practiceData',
  26. props: {
  27. studentId: {
  28. type: String,
  29. default: ''
  30. }
  31. },
  32. setup(props) {
  33. const chartRef = ref<HTMLDivElement | null>(null);
  34. const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  35. const practiceFlag = ref(true);
  36. const payForm = reactive({
  37. height: '360px',
  38. width: '100%',
  39. practiceDurationAvg: 0,
  40. practiceDays: 0,
  41. practiceDurationTotal: 0,
  42. dateList: [],
  43. timeList: []
  44. });
  45. const state = reactive({
  46. loading: false,
  47. pagination: {
  48. page: 1,
  49. rows: 10,
  50. pageTotal: 4
  51. },
  52. tableList: [] as any,
  53. goCourseVisiable: false
  54. });
  55. const timer = ref<[number, number]>([
  56. getNowDateAndMonday(new Date().getTime()),
  57. getNowDateAndSunday(new Date().getTime())
  58. ]);
  59. const columns = () => {
  60. return [
  61. {
  62. title: '日期',
  63. key: 'date'
  64. },
  65. {
  66. title: '练习时长(分钟)',
  67. key: 'practiceDuration',
  68. render(row: any) {
  69. return (
  70. <>
  71. {' '}
  72. <>
  73. {row.practiceDuration
  74. ? getMinutes(row.practiceDuration) > 0
  75. ? getMinutes(row.practiceDuration) +
  76. '分' +
  77. getSecend(row.practiceDuration) +
  78. '秒'
  79. : getSecend(row.practiceDuration) + '秒'
  80. : 0 + '分钟'}
  81. </>
  82. </>
  83. );
  84. }
  85. }
  86. ];
  87. };
  88. const getList = async () => {
  89. try{
  90. const res = await getTrainingStatList({
  91. page:1,
  92. rows:999,
  93. studentId: props.studentId,
  94. ...getTimes(timer.value, ['startTime', 'endTime'], 'YYYY-MM-DD')
  95. })
  96. state.tableList = res.data.rows;
  97. }catch(e){
  98. console.log(e)
  99. }
  100. };
  101. const setChart = () => {
  102. setOptions({
  103. tooltip: {
  104. trigger: 'axis',
  105. axisPointer: {
  106. type: 'shadow'
  107. }
  108. },
  109. legend: {
  110. show: false,
  111. selected: {
  112. //在这里设置默认展示就ok了
  113. '练习时长(分钟)': practiceFlag.value
  114. }
  115. },
  116. xAxis: {
  117. type: 'category',
  118. boundaryGap: true,
  119. axisLabel: {
  120. show: true,
  121. interval: 0
  122. },
  123. data: payForm.dateList
  124. },
  125. yAxis: [
  126. {
  127. type: 'value',
  128. axisLabel: {
  129. formatter: (value: any) => {
  130. return getMinutes(value) + 'min';
  131. }
  132. },
  133. axisTick: {
  134. show: false
  135. },
  136. splitArea: {
  137. show: false,
  138. areaStyle: {
  139. color: ['rgba(255,255,255,0.2)']
  140. }
  141. }
  142. }
  143. ],
  144. grid: {
  145. left: '1%',
  146. right: '1%',
  147. top: '2%',
  148. bottom: 0,
  149. containLabel: true
  150. },
  151. series: [
  152. {
  153. // smooth: true,
  154. data: payForm.timeList,
  155. type: 'bar',
  156. barWidth: '48px',
  157. stack: 'total',
  158. // label: {
  159. // // 柱图头部显示值
  160. // formatter: (value: any) => {
  161. // console.log(value);
  162. // return getMinutes(value.value);
  163. // },
  164. // show: true,
  165. // position: 'top',
  166. // color: '#333',
  167. // fontSize: '12px',
  168. // fontWeight: 600
  169. // },
  170. itemStyle: {
  171. normal: {
  172. //这里设置柱形图圆角 [左上角,右上角,右下角,左下角]
  173. barBorderRadius: [8, 8, 0, 0],
  174. color: '#D5E9FF'
  175. },
  176. emphasis: {
  177. focus: 'series',
  178. color: '#3583FA' //hover时改变柱子颜色
  179. // borderWidth: 4,
  180. // borderColor: 'rgba(213, 233, 255,.4)',
  181. // borderType: 'solid'
  182. }
  183. } as any
  184. }
  185. ],
  186. formatter: (item: any) => {
  187. if (Array.isArray(item)) {
  188. return [
  189. item[0].axisValueLabel,
  190. ...item.map((d: any) => {
  191. let str;
  192. getMinutes(d.value) > 0
  193. ? (str =
  194. getMinutes(d.value) + '分' + getSecend(d.value) + '秒')
  195. : (str = getSecend(d.value) + '秒');
  196. return `<br/>${d.marker}<span style="margin-top:10px;margin-left:5px;font-size: 13px;font-weight: 500;
  197. color: #131415;font-weight: 600;
  198. margin-top:12px
  199. line-height: 18px;">练习时长: ${str} </span>`;
  200. })
  201. ].join('');
  202. } else {
  203. return item;
  204. }
  205. }
  206. // dataZoom: [
  207. // {
  208. // type: 'slider',
  209. // start: 5,
  210. // end: 100,
  211. // filterMode: 'empty'
  212. // }
  213. // ]
  214. });
  215. };
  216. const getChartDetail = async () => {
  217. try {
  218. const res = await getTrainingStat({
  219. studentId: props.studentId,
  220. ...getTimes(timer.value, ['startTime', 'endTime'], 'YYYY-MM-DD')
  221. });
  222. payForm.practiceDays = res.data.practiceDays;
  223. payForm.practiceDurationAvg = res.data.practiceDurationAvg;
  224. payForm.practiceDurationTotal = res.data.practiceDurationTotal;
  225. payForm.dateList = res.data.trainingStatDetailList.map((item: any) => {
  226. return item.date;
  227. });
  228. payForm.timeList = res.data.trainingStatDetailList.map((item: any) => {
  229. return item.practiceDuration;
  230. });
  231. setChart();
  232. console.log(payForm);
  233. } catch (e) {
  234. console.log(e);
  235. }
  236. };
  237. const search = () => {
  238. state.pagination.page = 1;
  239. getChartDetail();
  240. getList()
  241. console.log('search');
  242. };
  243. const onReset = () => {
  244. timer.value = [
  245. getNowDateAndMonday(new Date().getTime()),
  246. getNowDateAndSunday(new Date().getTime())
  247. ];
  248. search();
  249. getList()
  250. console.log('onReset');
  251. };
  252. onMounted(() => {
  253. console.log(props.studentId);
  254. getChartDetail();
  255. getList()
  256. });
  257. return () => (
  258. <>
  259. <NForm label-placement="left" inline>
  260. <NFormItem>
  261. <CDatePicker
  262. v-model:value={timer.value}
  263. separator={'至'}
  264. type="daterange"
  265. timerValue={timer.value}></CDatePicker>
  266. </NFormItem>
  267. <NFormItem>
  268. <NSpace justify="end">
  269. <NButton type="primary" class="searchBtn" onClick={search}>
  270. 搜索
  271. </NButton>
  272. <NButton type="primary" ghost class="resetBtn" onClick={onReset}>
  273. 重置
  274. </NButton>
  275. </NSpace>
  276. </NFormItem>
  277. </NForm>
  278. <div class={styles.homeTrainData}>
  279. <div class={styles.TrainDataTop}>
  280. <div class={styles.TrainDataTopLeft}>
  281. <div class={styles.TrainDataItem}>
  282. <p class={styles.TrainDataItemTitle}>
  283. {getMinutes(payForm.practiceDurationTotal) > 0 ? (
  284. <div>
  285. <span>
  286. <NNumberAnimation
  287. from={0}
  288. to={getMinutes(
  289. payForm.practiceDurationTotal
  290. )}></NNumberAnimation>
  291. </span>{' '}
  292. </div>
  293. ) : null}
  294. <div>
  295. <span>
  296. <NNumberAnimation
  297. from={0}
  298. to={getSecend(
  299. payForm.practiceDurationTotal
  300. )}></NNumberAnimation>
  301. </span>{' '}
  302. </div>
  303. </p>
  304. <p class={styles.TrainDataItemsubTitle}>累计练习时长</p>
  305. </div>
  306. <div class={styles.TrainDataItem}>
  307. <p class={styles.TrainDataItemTitle}>
  308. {getMinutes(payForm.practiceDurationAvg) > 0 ? (
  309. <div>
  310. <span>
  311. <NNumberAnimation
  312. from={0}
  313. to={getMinutes(
  314. payForm.practiceDurationAvg
  315. )}></NNumberAnimation>
  316. </span>{' '}
  317. </div>
  318. ) : null}
  319. <div>
  320. <span>
  321. <NNumberAnimation
  322. from={0}
  323. to={getSecend(
  324. payForm.practiceDurationAvg
  325. )}></NNumberAnimation>
  326. </span>{' '}
  327. </div>
  328. </p>
  329. <p class={styles.TrainDataItemsubTitle}>平均练习时长</p>
  330. </div>
  331. <div class={styles.TrainDataItem}>
  332. <p class={styles.TrainDataItemTitle}>
  333. <div>
  334. <span>
  335. <NNumberAnimation
  336. from={0}
  337. to={payForm.practiceDays}></NNumberAnimation>
  338. </span>{' '}
  339. </div>
  340. </p>
  341. <p class={styles.TrainDataItemsubTitle}>练习天数</p>
  342. </div>
  343. </div>
  344. <div class={styles.TrainDataTopRight}>
  345. <div
  346. // onClick={() => {
  347. // practiceFlag.value = !practiceFlag.value;
  348. // setChart();
  349. // }}
  350. class={[
  351. styles.DataTopRightItem,
  352. practiceFlag.value ? '' : styles.DataTopRightItemDis
  353. ]}>
  354. <div
  355. class={[
  356. styles.DataTopRightDot,
  357. styles.DataTopRightDotBlue
  358. ]}></div>
  359. <p>练习时长(分钟)</p>
  360. </div>
  361. </div>
  362. </div>
  363. <div class={styles.chatrs}>
  364. <div
  365. ref={chartRef}
  366. style={{ height: payForm.height, width: payForm.width }}></div>
  367. </div>
  368. <div class={styles.tableWrap}>
  369. <NDataTable
  370. class={styles.classTable}
  371. loading={state.loading}
  372. columns={columns()}
  373. data={state.tableList}></NDataTable>
  374. {/* <Pagination
  375. v-model:page={state.pagination.page}
  376. v-model:pageSize={state.pagination.rows}
  377. v-model:pageTotal={state.pagination.pageTotal}
  378. onList={getList}
  379. sync
  380. saveKey="orchestraRegistration-key"
  381. /> */}
  382. </div>
  383. </div>
  384. </>
  385. );
  386. }
  387. });