AlgoDataPanel.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <template>
  2. <div>
  3. <div class="algo-header">
  4. <span class="algo-tit">算法数据分析</span>
  5. <CensusTabs @check-tab="onCheckTab" />
  6. </div>
  7. <el-divider />
  8. <div style="flex-grow: 1; display: flex">
  9. <v-chart
  10. ref="myChart"
  11. class="chart"
  12. :option="option"
  13. @highlight="handleHighlight"
  14. @downplay="handleDownPlay"
  15. @click="handleClickChart"
  16. @mouseover="handleMouseOver"
  17. @mouseout="handleMouseOut" />
  18. <el-divider direction="vertical" border-style="solid" />
  19. <div class="stat-show">
  20. <ViolationStatItem :data="getVioStatData(0)" class="stat-data" />
  21. <div class="stat-divider"></div>
  22. <ViolationStatItem :data="getVioStatData(1)" class="stat-data" />
  23. <ViolationStatItem :data="getVioStatData(2)" class="stat-data" />
  24. <div class="stat-divider"></div>
  25. <ViolationStatItem :data="getVioStatData(3)" class="stat-data" /> </div
  26. ></div>
  27. </div>
  28. </template>
  29. <script setup lang="ts">
  30. import { computed, ref, onMounted } from 'vue';
  31. import CensusTabs from './AlgoCensusTabs.vue';
  32. import ViolationStatItem from './ViolationStatItem.vue';
  33. import { TimeTabEnum, violationHandleCounts } from '../types';
  34. import { use } from 'echarts/core';
  35. import { CanvasRenderer } from 'echarts/renderers';
  36. import { PieChart } from 'echarts/charts';
  37. import { TooltipComponent, LegendComponent, TitleComponent } from 'echarts/components';
  38. import { ViolationCount } from '@/api/home/home.ts';
  39. import VChart from 'vue-echarts';
  40. import * as echarts from 'echarts'; // 保留此行引入,防止诸如*labelLayout*等属性打包后不生效问题
  41. console.log(echarts); // 此行仅为消除上一行import代码的eslint警告
  42. const props = defineProps<{
  43. data: ViolationCount;
  44. getViolations: (range: string[]) => void;
  45. }>();
  46. use([CanvasRenderer, PieChart, TooltipComponent, TitleComponent, LegendComponent]);
  47. const algoData = computed(() => {
  48. let newData: any[] = [];
  49. const vioList = props.data.violationAlgoList;
  50. if (vioList && vioList.length) {
  51. newData = vioList.map((item) => {
  52. return {
  53. value: item.proportion,
  54. name: item.name,
  55. };
  56. });
  57. }
  58. return newData;
  59. });
  60. const algoLegendData = computed(() => {
  61. return algoData.value.map((item) => item.name);
  62. });
  63. const statData = computed(() => props.data.statusCountList);
  64. const getVioStatData = (index) => {
  65. let count = 0;
  66. if (statData.value && statData.value.length) {
  67. const matchItem = statData.value.find(
  68. (item) => item.name === violationHandleCounts[index].value,
  69. );
  70. if (matchItem) {
  71. count = matchItem.value;
  72. }
  73. }
  74. return { ...violationHandleCounts[index], count };
  75. };
  76. // const titleLeft = ref('');
  77. const myChart = ref();
  78. let timer;
  79. const index = ref(0);
  80. onMounted(() => {
  81. // const contentChart = document.querySelector('.echarts.chart') as HTMLDivElement;
  82. // titleLeft.value = contentChart?.offsetWidth * 0.3 + 'px';
  83. if (myChart.value) startAutoPlay();
  84. });
  85. function startAutoPlay() {
  86. stopAutoPlay();
  87. timer = setInterval(function () {
  88. myChart.value.dispatchAction({
  89. type: 'highlight',
  90. dataIndex: index.value,
  91. });
  92. myChart.value.dispatchAction({
  93. type: 'downplay',
  94. dataIndex: index.value === 0 ? algoData.value.length - 1 : index.value - 1,
  95. });
  96. index.value++;
  97. if (index.value === algoData.value.length) {
  98. index.value = 0;
  99. }
  100. }, 2500);
  101. }
  102. function stopAutoPlay() {
  103. clearInterval(timer);
  104. let length = algoData.value.length;
  105. myChart.value.dispatchAction({
  106. type: 'downplay',
  107. dataIndex: Array.from({ length }, (_, i) => i),
  108. });
  109. }
  110. function pauseAutoPlay(argIndex) {
  111. if (myChart.value) {
  112. let length = algoData.value.length;
  113. index.value = argIndex;
  114. stopAutoPlay();
  115. myChart.value.dispatchAction({
  116. type: 'highlight',
  117. dataIndex: index.value,
  118. });
  119. myChart.value.dispatchAction({
  120. type: 'downplay',
  121. dataIndex: Array.from({ length }, (_, i) => i).filter((item) => item !== index.value),
  122. });
  123. }
  124. }
  125. function continueAutoPlay(argIndex) {
  126. if (myChart.value) {
  127. let length = algoData.value.length;
  128. myChart.value.dispatchAction({
  129. type: 'downplay',
  130. dataIndex: argIndex,
  131. });
  132. index.value = argIndex + 1 === length ? 0 : argIndex + 1;
  133. startAutoPlay();
  134. }
  135. }
  136. const handleHighlight = (event) => {
  137. if ('name' in event) {
  138. const tempIndex = algoData.value.findIndex((item) => item.name === event.name);
  139. if (myChart.value) pauseAutoPlay(tempIndex);
  140. }
  141. };
  142. const handleDownPlay = (event) => {
  143. if ('name' in event) {
  144. const tempIndex = algoData.value.findIndex((item) => item.name === event.name);
  145. if (myChart.value) continueAutoPlay(tempIndex);
  146. }
  147. };
  148. const handleClickChart = (event) => {
  149. const { dataIndex } = event;
  150. if (myChart.value) pauseAutoPlay(dataIndex);
  151. };
  152. const handleMouseOver = (event) => {
  153. const { dataIndex } = event;
  154. if (myChart.value) pauseAutoPlay(dataIndex);
  155. };
  156. const handleMouseOut = (event) => {
  157. const { dataIndex } = event;
  158. if (myChart.value) continueAutoPlay(dataIndex);
  159. };
  160. const option = computed(() => {
  161. return {
  162. title: {
  163. text: '问题占比', // 设置标题文本
  164. // left: titleLeft.value, // 标题居中对齐
  165. left: '32.5%',
  166. top: '37%',
  167. textAlign: 'center',
  168. textStyle: {
  169. fontSize: 20,
  170. },
  171. },
  172. grid: {
  173. left: '10px',
  174. right: '4%',
  175. bottom: '3%',
  176. },
  177. legend: [
  178. {
  179. icon: 'circle',
  180. orient: 'vertical',
  181. type: 'scroll',
  182. left: '65%',
  183. top: 'center',
  184. itemWidth: 10,
  185. itemHeight: 10,
  186. align: 'left',
  187. textStyle: {
  188. fontSize: 14,
  189. color: '#6e69b2',
  190. },
  191. data: algoLegendData.value,
  192. formatter: function (name) {
  193. let maxNameLength = 0;
  194. algoData.value.forEach((item) => {
  195. if (item.name.length > maxNameLength) maxNameLength = item.name.length;
  196. });
  197. if (algoData.value && algoData.value.length) {
  198. for (var i = 0; i < algoData.value.length; i++) {
  199. if (name === algoData.value[i].name) {
  200. return (
  201. '' +
  202. name +
  203. ' '.repeat(maxNameLength - name.length) +
  204. ' ' +
  205. (algoData.value[i].value * 100).toFixed(2) +
  206. '%'
  207. );
  208. }
  209. }
  210. }
  211. },
  212. },
  213. ],
  214. series: [
  215. {
  216. type: 'pie',
  217. radius: ['44%', '55%'],
  218. center: ['33%', '40%'],
  219. avoidLabelOverlap: false,
  220. label: {
  221. show: false,
  222. position: 'outside',
  223. fontSize: 16,
  224. color: '#000',
  225. },
  226. labelLine: {
  227. show: false,
  228. },
  229. labelLayout() {
  230. return {
  231. x: '33%',
  232. y: '86%',
  233. verticalAlign: 'bottom',
  234. align: 'center',
  235. };
  236. },
  237. emphasis: {
  238. label: {
  239. show: true,
  240. fontSize: 16,
  241. fontWeight: 'bold',
  242. formatter: '{b}: {d}%',
  243. },
  244. itemStyle: {
  245. borderColor: '#f3f3f3',
  246. borderWidth: 3,
  247. shadowBlur: 10,
  248. shadowOffsetX: 0,
  249. shadowColor: 'rgba(0, 0, 0, 0.5)',
  250. },
  251. },
  252. data: algoData.value,
  253. },
  254. ],
  255. };
  256. });
  257. const timeTab = ref<TimeTabEnum>(TimeTabEnum.DAY);
  258. const onCheckTab = (info: { tab: TimeTabEnum; data: string[] }) => {
  259. timeTab.value = info.tab;
  260. props.getViolations(info.data);
  261. index.value = 0;
  262. startAutoPlay();
  263. };
  264. </script>
  265. <style scoped>
  266. .algo-header {
  267. display: flex;
  268. justify-content: space-between;
  269. margin-bottom: 10px;
  270. }
  271. .chart {
  272. width: 100%;
  273. min-width: 550px;
  274. flex-grow: 1;
  275. height: 340px;
  276. }
  277. .el-divider--horizontal {
  278. margin: 0px;
  279. }
  280. .algo-tit {
  281. font-size: 16px;
  282. margin-top: 17px;
  283. margin-left: 18px;
  284. }
  285. .stat-show {
  286. flex-grow: 0;
  287. width: 266px;
  288. flex-shrink: 0;
  289. font-size: 14px;
  290. display: flex;
  291. flex-wrap: wrap;
  292. justify-content: center;
  293. padding: 0;
  294. margin-top: 72px;
  295. margin-bottom: 82px;
  296. }
  297. .stat-data {
  298. flex-basis: calc(50% - 1px);
  299. padding-bottom: 35px;
  300. }
  301. .stat-divider {
  302. width: 1px;
  303. height: 40px;
  304. background: #e9e9e9;
  305. }
  306. .el-divider--vertical {
  307. margin: 0px;
  308. height: 339px;
  309. }
  310. /* 控制图例的样式 */
  311. ::v-deep .echarts-legend {
  312. display: flex;
  313. flex-wrap: wrap; /* 图例换行 */
  314. }
  315. ::v-deep .echarts-legend-item {
  316. flex: 0 0 50%; /* 每行显示两个图例 */
  317. }
  318. </style>