ChoiceStatistics.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <div class="choice-statistics-panel">
  3. <div v-if="isChoiceQuestion && statistics" class="statistics-content">
  4. <div class="statistics-title">选择题统计</div>
  5. <div class="statistics-summary">
  6. 总提交:{{ statistics.totalSubmissions }} 人
  7. </div>
  8. </div>
  9. <div v-else class="statistics-empty">
  10. <div v-if="!isChoiceQuestion">当前页面不是选择题</div>
  11. <div v-else-if="!statistics">暂无统计数据</div>
  12. </div>
  13. <div class="statistics-chart">
  14. <div class="tool45" v-if="statistics && statistics.tool=='45'">
  15. <div v-for="(item,index) in statistics.titles" :key="index">
  16. <div class="t45_title">
  17. <span>{{ index+1 }}、</span>
  18. <div>{{ item.title }}</div>
  19. </div>
  20. <div class="t45_list">
  21. <div class="t45_l_item" v-for="(i,index2) in item.checkList" :key="index+'_'+index2">
  22. <div class="t45_l_i_option">
  23. <span>{{ i.index }}:</span>
  24. <div>
  25. <div :style="{width:i.proportion}"></div>
  26. </div>
  27. <span>{{ i.proportion }}</span>
  28. </div>
  29. <div class="t45_l_i_user">
  30. <template v-for="(j,index3) in i.user" :key="index+'_'+index2+'_'+index3">
  31. <div v-if="item.showAllUser || (!item.showAllUser && index3 < 3)">
  32. <span>{{ j }}</span>
  33. </div>
  34. </template>
  35. <span class="t45_o_btn" v-if="i.user.length > 3 && !item.showAllUser" @click="()=>{item.showAllUser = true,$forceUpdate()}">
  36. <svg t="1756278651830" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7545" width="200" height="200"><path d="M293.546667 253.013333l264.533333 266.666667-264.106667 266.24 70.4 70.826667 334.506667-337.066667-334.933333-337.066667z" p-id="7546"></path></svg>
  37. </span>
  38. <span class="t45_o_btn" v-if="i.user.length > 3 && item.showAllUser" @click="()=>{item.showAllUser = false,$forceUpdate()}">
  39. <svg style="transform: rotate(180deg);" t="1756278651830" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7545" width="200" height="200"><path d="M293.546667 253.013333l264.533333 266.666667-264.106667 266.24 70.4 70.826667 334.506667-337.066667-334.933333-337.066667z" p-id="7546"></path></svg>
  40. </span>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script lang="ts" setup>
  50. import { computed } from 'vue'
  51. import { ElementTypes } from '@/types/slides'
  52. interface Props {
  53. workArray: any[]
  54. elementList: any[]
  55. }
  56. const props = defineProps<Props>()
  57. // 检查当前是否为选择题(toolType为45)
  58. const isChoiceQuestion = computed(() => {
  59. const frame = props.elementList.find(element => element.type === ElementTypes.FRAME)
  60. return frame?.toolType === 45
  61. })
  62. const optionList: Array<string> = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  63. // 选择题统计信息
  64. const statistics = computed(() => {
  65. if (!isChoiceQuestion.value || !props.workArray || props.workArray.length === 0) return null
  66. // 统计信息结构优化
  67. let titles: Array<{
  68. title: string,
  69. showAllUser: boolean,
  70. checkList: Array<{ options: string, num: number, proportion: string, index :string, user: Array<[]>}>
  71. }> = []
  72. let totalSubmissions = 0
  73. let tool: string = ''
  74. // 只初始化一次题目结构
  75. let isTitleInitialized = false
  76. props.workArray.forEach((work) => {
  77. if (!work.content) return
  78. try {
  79. if (work.atool === '45') {
  80. const content = JSON.parse(decodeURIComponent(work.content))
  81. const testJson = content.testJson
  82. // 初始化题目结构
  83. if (!isTitleInitialized && Array.isArray(testJson)) {
  84. tool = work.atool
  85. titles = testJson.map((item: any) => ({
  86. title: item.teststitle || '',
  87. showAllUser: false,
  88. checkList: Array.isArray(item.checkList)
  89. ? item.checkList.map((i: any) => ({ options: i, num: 0, user: [], proportion: '', index: '' }))
  90. : []
  91. }))
  92. isTitleInitialized = true
  93. }
  94. // 统计每道题的选项被选择次数
  95. if (Array.isArray(titles) && Array.isArray(testJson)) {
  96. testJson.forEach((item: any, index: number) => {
  97. const answer = item.userAnswer
  98. if (typeof answer === 'number') {
  99. if (titles[index] && titles[index].checkList[answer]) {
  100. titles[index].checkList[answer].num += 1
  101. titles[index].checkList[answer].user.push(work.name)
  102. }
  103. }
  104. else if (Array.isArray(answer) && answer.length > 0) {
  105. answer.forEach((i: number) => {
  106. if (titles[index] && titles[index].checkList[i]) {
  107. titles[index].checkList[i].num += 1
  108. titles[index].checkList[i].user.push(work.name)
  109. }
  110. })
  111. }
  112. })
  113. }
  114. totalSubmissions++
  115. }
  116. }
  117. catch (e) {
  118. // eslint-disable-next-line no-console
  119. console.log('解析选择题答案失败:', e, work.content)
  120. }
  121. })
  122. titles.forEach((item: any) => {
  123. const total = item.checkList.reduce((sum: number, cur: any) => sum + cur.num, 0)
  124. item.checkList.forEach((option: any, index :number) => {
  125. option.proportion = total > 0 ? ((option.num / total) * 100).toFixed(1) + '%' : '0.0%'
  126. option.index = optionList[index]
  127. })
  128. })
  129. // 返回结构优化,便于前端展示
  130. return {
  131. totalSubmissions,
  132. titles,
  133. tool
  134. }
  135. })
  136. </script>
  137. <style lang="scss" scoped>
  138. .choice-statistics-panel {
  139. height: 100%;
  140. padding: 16px;
  141. }
  142. .statistics-content {
  143. background: #f8f9fa;
  144. border-radius: 8px;
  145. border: 1px solid #e9ecef;
  146. padding: 16px;
  147. }
  148. .statistics-title {
  149. font-size: 14px;
  150. font-weight: 600;
  151. color: #333;
  152. margin-bottom: 12px;
  153. text-align: center;
  154. }
  155. .statistics-summary {
  156. text-align: center;
  157. color: #666;
  158. font-size: 13px;
  159. margin-bottom: 16px;
  160. padding: 8px;
  161. background: #fff;
  162. border-radius: 6px;
  163. }
  164. .statistics-chart {
  165. display: flex;
  166. flex-direction: column;
  167. gap: 12px;
  168. }
  169. .statistics-item {
  170. display: flex;
  171. align-items: center;
  172. gap: 8px;
  173. }
  174. .option-label {
  175. min-width: 40px;
  176. font-size: 12px;
  177. color: #333;
  178. font-weight: 500;
  179. }
  180. .option-bar {
  181. flex: 1;
  182. height: 8px;
  183. background: #e9ecef;
  184. border-radius: 4px;
  185. overflow: hidden;
  186. }
  187. .option-fill {
  188. height: 100%;
  189. background: linear-gradient(90deg, #1890ff, #40a9ff);
  190. border-radius: 4px;
  191. transition: width 0.3s ease;
  192. }
  193. .option-count {
  194. min-width: 35px;
  195. font-size: 11px;
  196. color: #666;
  197. text-align: right;
  198. }
  199. .statistics-empty {
  200. padding: 40px 20px;
  201. text-align: center;
  202. color: #999;
  203. font-size: 14px;
  204. }
  205. .tool45{
  206. width: 100%;
  207. height: auto;
  208. &>div{
  209. width: 100%;
  210. height: auto;
  211. margin: 10px 0;
  212. background-color: #F8F9FA;
  213. border-radius: 8px;
  214. border: 1px solid #e9ecef;
  215. box-sizing: border-box;
  216. padding: 16px;
  217. .t45_title{
  218. width: 100%;
  219. display: flex;
  220. align-items: center;
  221. margin-bottom: 5px;
  222. span{
  223. font-size: 16px;
  224. color: #1890ff;
  225. }
  226. }
  227. }
  228. .t45_list{
  229. width: 100%;
  230. height: auto;
  231. .t45_l_item{
  232. margin-top: 5px;
  233. .t45_l_i_option{
  234. display: flex;
  235. align-items: center;
  236. height: auto;
  237. width: 100%;
  238. justify-content: space-between;
  239. span{
  240. display: flex;
  241. width: 40px;
  242. &:nth-of-type(1){
  243. width: 20px;
  244. }
  245. }
  246. &>div{
  247. margin: 0 5px;
  248. width: calc(100% - 10px - 20px - 40px);
  249. height: 15px;
  250. background-color: #FFFFFF;
  251. // box-sizing: border-box;
  252. border: solid 1px #E9ECEF;
  253. border-radius: 4px;
  254. overflow: hidden;
  255. &>div{
  256. height: 100%;
  257. background-color: #3681FC;
  258. }
  259. }
  260. }
  261. .t45_l_i_user{
  262. width: 100%;
  263. height: auto;
  264. display: flex;
  265. align-items: center;
  266. flex-wrap: wrap;
  267. margin-top: 8px;
  268. margin-bottom: 8px;
  269. &>div{
  270. align-items: center;
  271. flex-wrap: wrap;
  272. height: 35px;
  273. span{
  274. width: auto;
  275. height: auto;
  276. padding: 4px 10px;
  277. background-color: #fff;
  278. border: solid 1px #2f80ed;
  279. border-radius: 6px;
  280. margin-right:8px ;
  281. margin-bottom: 5px;
  282. color: #2f80ed;
  283. }
  284. }
  285. &>span{
  286. width: auto;
  287. height: auto;
  288. padding: 4px 10px;
  289. background-color: #fff;
  290. border: solid 1px #E9ECEF;
  291. border-radius: 6px;
  292. margin-right:8px;
  293. cursor: pointer;
  294. transition: .2s;
  295. &:hover{
  296. background-color: #f1f1f1;
  297. }
  298. svg{
  299. width: 15px;
  300. height: 15px;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. }
  307. </style>