index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <template>
  2. <div class="sample-statistics">
  3. <!-- Top: Statistics & Time Filter -->
  4. <el-row :gutter="20" class="top-bar">
  5. <el-col :span="10">
  6. <div style="display: flex;align-items: center;">
  7. <span style="display: block;">NCBI基因组序列编号:</span>
  8. <!-- <el-input style="flex: 1;" v-model="assemblyAccession" placeholder="请输入病原体注册号"></el-input> -->
  9. <el-select
  10. v-model="assemblyAccession"
  11. filterable
  12. remote
  13. reserve-keyword
  14. placeholder="请输入NCBI基因组序列编号"
  15. :remote-method="queryInfo"
  16. :loading="loading"
  17. @change="handleSelect"
  18. style="flex:1"
  19. >
  20. <el-option
  21. v-for="item in Assoptions"
  22. :key="item.id"
  23. :label="item.assemblyAccession"
  24. :value="item.assemblyAccession"
  25. >
  26. </el-option>
  27. </el-select>
  28. </div>
  29. </el-col>
  30. <el-col :span="3" class="filter-col">
  31. <el-select v-model="selectedRange" placeholder="选择时间范围" @change="onRangeChange" size="small">
  32. <el-option label="近24小时" value="1"></el-option>
  33. <el-option label="近3天(72小时)" value="2"></el-option>
  34. <el-option label="近7天" value="3"></el-option>
  35. <el-option label="近30天" value="4"></el-option>
  36. </el-select>
  37. </el-col>
  38. <el-col :span="6">
  39. <el-button type="primary" @click="fetchData">查询</el-button>
  40. </el-col>
  41. </el-row>
  42. <!-- Middle: Line Chart -->
  43. <div class="chart-section">
  44. <div ref="lineChart" class="line-chart"></div>
  45. </div>
  46. <!-- 动态图表 -->
  47. <el-row>
  48. <el-col :span="24" class="pie-row">
  49. <div style="display: flex;align-items: center;">
  50. <div>诊断统计设置:</div>
  51. <el-select style="flex: 1;width:100%" v-model="value" multiple placeholder="请选择">
  52. <el-option
  53. v-for="item in options"
  54. :key="item.value"
  55. :label="item.label"
  56. :value="item.value">
  57. </el-option>
  58. </el-select>
  59. </div>
  60. </el-col>
  61. </el-row>
  62. <div class="chart-section dynamic-charts">
  63. </div>
  64. </div>
  65. </template>
  66. <script>
  67. import * as echarts from 'echarts';
  68. import * as Plotly from 'plotly.js-dist-min';
  69. import {getjcwzbInfo, getyblxtjInfo, getybtjInfo,getConfigList,getConfigData,getAssemblyAccessionList} from '@/api/statistics/report';
  70. export default {
  71. name: 'SampleStatistics',
  72. data() {
  73. return {
  74. selectedRange: '1',
  75. assemblyAccession: '',
  76. totalSamples: 1234,
  77. lineChart: null,
  78. typePieChart: null,
  79. detectedPieChart: null,
  80. lineData: [],
  81. typePieData: [],
  82. detectedPieData: [],
  83. value: [],
  84. options: [],
  85. dynamicChartContainers: [],
  86. isGettingConfigList:false, // 新增:标记是否正在获取配置列表
  87. loading:false,
  88. Assoptions:[],
  89. ASSTimer:null,
  90. }
  91. },
  92. mounted() {
  93. this.lineChart = echarts.init(this.$refs.lineChart);
  94. // this.fetchData()
  95. },
  96. // 新增监听 value 变化
  97. watch: {
  98. value: {
  99. handler(newValue, oldValue) {
  100. // 判断 newValue 和 oldValue 是否相同
  101. if (this.isArrayEqual(newValue, oldValue)) {
  102. return;
  103. }
  104. if (newValue.length > 0 && this.assemblyAccession && !this.isGettingConfigList) {
  105. this.getChartsByValue();
  106. }
  107. },
  108. // deep: true
  109. }
  110. },
  111. methods: {
  112. // 搜索
  113. queryInfo(queryString){
  114. if (this.ASSTimer) {
  115. clearTimeout(this.ASSTimer);
  116. }
  117. this.ASSTimer = setTimeout(() => {
  118. if (queryString.length > 0){
  119. this.loading = true
  120. getAssemblyAccessionList(queryString).then(res => {
  121. console.log('查询基因组序列编号',res)
  122. if (res.data && res.data.length > 0) {
  123. this.Assoptions = res.data
  124. }
  125. this.loading = false
  126. }).finally(() => {
  127. this.loading = false
  128. })
  129. }
  130. }, 500);
  131. },
  132. handleSelect(){
  133. if (this.assemblyAccession) {
  134. this.fetchData();
  135. }
  136. },
  137. // 新增:判断两个数组是否相等的方法
  138. isArrayEqual(arr1, arr2) {
  139. if (arr1.length !== arr2.length) {
  140. return false;
  141. }
  142. // 对数组进行排序后再比较
  143. const sortedArr1 = [...arr1].sort();
  144. const sortedArr2 = [...arr2].sort();
  145. for (let i = 0; i < sortedArr1.length; i++) {
  146. if (sortedArr1[i] !== sortedArr2[i]) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. },
  152. async getAllConfigList() {
  153. this.isGettingConfigList = true; // 设置标记为正在获取
  154. const res = await getConfigList();
  155. this.options = res.data.map(item => ({
  156. label: item.title,
  157. value: item.id,
  158. ...item
  159. }));
  160. this.value = res.data
  161. .map(item => (item.defaultShow === 'Y' ? item.id : ''))
  162. .filter(Boolean);
  163. this.getChartsByValue();
  164. },
  165. async getChartsByValue() {
  166. this.clearDynamicCharts();
  167. const requests = this.value.map(async item => {
  168. const optionItem = this.options.find(opt => opt.value === item);
  169. if (optionItem) {
  170. const res = await getConfigData(
  171. this.selectedRange,
  172. this.assemblyAccession,
  173. item
  174. );
  175. console.log(optionItem,'optionItem.label')
  176. if (optionItem.isNum === 'Y') {
  177. this.renderViolinChart(res.data, optionItem.title);
  178. } else {
  179. this.renderLineChart(res.data, optionItem.title);
  180. }
  181. }
  182. });
  183. await Promise.all(requests);
  184. this.isGettingConfigList = false; // 重置标记为获取完成
  185. },
  186. renderViolinChart(data, label) {
  187. const chartContainer = document.createElement('div');
  188. chartContainer.style.width = '100%';
  189. chartContainer.style.height = '800px';
  190. chartContainer.id = `violinChart_${label}`;
  191. chartContainer.classList.add('dynamic-chart');
  192. document.querySelector('.dynamic-charts').appendChild(chartContainer);
  193. // 新增:将容器添加到数组中
  194. this.dynamicChartContainers.push(chartContainer);
  195. const traces = data.map((item, index) => {
  196. console.log('item', item);
  197. const xValue = item.hour; // 直接使用 item.hour
  198. return {
  199. type: 'violin',
  200. x: [xValue],
  201. y: item.y,
  202. name: xValue,
  203. box: {
  204. visible: true,
  205. line: {
  206. color: '#333',
  207. width: 1.5
  208. }
  209. },
  210. meanline: {
  211. visible: true,
  212. color: '#ff4d4f'
  213. },
  214. line: {
  215. color: `hsl(${index * 360 / data.length}, 70%, 50%)`
  216. },
  217. fillcolor: `hsla(${index * 360 / data.length}, 70%, 50%, 0.2)`,
  218. opacity: 0.8
  219. };
  220. });
  221. // 提取所有 x 轴标签
  222. const xLabels = data.map((item) => item.hour);
  223. const layout = {
  224. title: {
  225. text: label,
  226. font: {
  227. size: 20,
  228. color: '#333'
  229. },
  230. y: 0.95
  231. },
  232. yaxis: {
  233. zeroline: false,
  234. title: {
  235. text: '数值',
  236. font: {
  237. size: 16,
  238. color: '#666'
  239. }
  240. },
  241. tickfont: {
  242. size: 12,
  243. color: '#666'
  244. },
  245. type: 'linear',
  246. automargin: true
  247. },
  248. xaxis: {
  249. title: {
  250. text: '日期',
  251. font: {
  252. size: 16,
  253. color: '#666'
  254. }
  255. },
  256. tickmode: 'array',
  257. tickvals: xLabels,
  258. ticktext: xLabels,
  259. tickfont: {
  260. size: 12,
  261. color: '#666'
  262. },
  263. tickangle: -45,
  264. automargin: true
  265. },
  266. violinmode: 'group',
  267. margin: {
  268. l: 80,
  269. r: 80,
  270. b: 80,
  271. t: 120,
  272. pad: 10
  273. },
  274. legend: {
  275. font: {
  276. size: 12,
  277. color: '#666'
  278. },
  279. orientation: 'h',
  280. yanchor: 'top',
  281. y: -0.3,
  282. xanchor: 'center',
  283. x: 0.5
  284. },
  285. plot_bgcolor: '#fff',
  286. paper_bgcolor: '#fff',
  287. dragmode: false,
  288. };
  289. // 直接隐藏工具栏
  290. const config = {
  291. displayModeBar: false
  292. };
  293. Plotly.newPlot(chartContainer, traces, layout,config);
  294. },
  295. renderLineChart(data, label) {
  296. const chartContainer = document.createElement('div');
  297. chartContainer.style.width = '100%';
  298. chartContainer.style.height = '600px';
  299. chartContainer.id = `lineChart_${label}`;
  300. chartContainer.classList.add('dynamic-chart');
  301. document.querySelector('.dynamic-charts').appendChild(chartContainer);
  302. // 新增:将容器添加到数组中
  303. this.dynamicChartContainers.push(chartContainer);
  304. const chart = echarts.init(chartContainer);
  305. const xData = data.map(item => {
  306. if (typeof item.hour === 'number') {
  307. return `${item.hour}小时`;
  308. }
  309. return item.hour;
  310. });
  311. const xdsData = data.map(item => item.xds);
  312. const xdsyxData = data.map(item => item.xdsyx);
  313. const option = {
  314. // 添加 grid 配置项,设置底部边距
  315. grid: {
  316. left: '3%',
  317. right: '4%',
  318. bottom: '15%', // 增大底部边距,为 X 轴标签留出空间
  319. containLabel: true
  320. },
  321. title: {
  322. text: label,
  323. left: 'center',
  324. top: '3%',
  325. textStyle: {
  326. color: '#333',
  327. fontSize: 20
  328. }
  329. },
  330. tooltip: {
  331. trigger: 'axis',
  332. backgroundColor: 'rgba(0, 0, 0, 0.8)',
  333. textStyle: {
  334. color: '#fff'
  335. },
  336. axisPointer: {
  337. type: 'cross',
  338. crossStyle: {
  339. color: '#999'
  340. }
  341. }
  342. },
  343. legend: {
  344. top: '10%',
  345. textStyle: {
  346. color: '#666',
  347. fontSize: 14
  348. }
  349. },
  350. xAxis: {
  351. type: 'category',
  352. data: xData,
  353. axisLabel: {
  354. color: '#666',
  355. fontSize: 14,
  356. rotate: 45
  357. },
  358. axisLine: {
  359. lineStyle: {
  360. color: '#666'
  361. }
  362. },
  363. axisTick: {
  364. show: false
  365. },
  366. name: typeof data[0]?.hour === 'number' ? '小时' : '日期'
  367. },
  368. yAxis: {
  369. type: 'value',
  370. name: '数值',
  371. nameTextStyle: {
  372. color: '#666',
  373. fontSize: 16
  374. },
  375. axisLabel: {
  376. color: '#666',
  377. fontSize: 14
  378. },
  379. axisLine: {
  380. lineStyle: {
  381. color: '#666'
  382. }
  383. },
  384. axisTick: {
  385. show: false
  386. },
  387. splitLine: {
  388. lineStyle: {
  389. color: '#eee'
  390. }
  391. }
  392. },
  393. series: [
  394. {
  395. name: 'xds',
  396. type: 'line',
  397. data: xdsData,
  398. smooth: true,
  399. lineStyle: {
  400. color: '#409EFF',
  401. width: 2
  402. },
  403. symbol: 'circle',
  404. symbolSize: 8,
  405. itemStyle: {
  406. color: '#409EFF'
  407. },
  408. emphasis: {
  409. symbolSize: 12
  410. }
  411. },
  412. {
  413. name: 'xdsyx',
  414. type: 'line',
  415. data: xdsyxData,
  416. smooth: true,
  417. lineStyle: {
  418. color: '#E6A23C',
  419. width: 2
  420. },
  421. symbol: 'circle',
  422. symbolSize: 8,
  423. itemStyle: {
  424. color: '#E6A23C'
  425. },
  426. emphasis: {
  427. symbolSize: 12
  428. }
  429. }
  430. ]
  431. };
  432. chart.setOption(option);
  433. },
  434. onRangeChange() {
  435. this.fetchData()
  436. },
  437. async fetchData() {
  438. if (!this.assemblyAccession) {
  439. this.$message.error('请输入病原体注册号');
  440. return;
  441. }
  442. this.getAllConfigList();
  443. const result = await getybtjInfo(this.selectedRange, this.assemblyAccession);
  444. this.processData(result.data);
  445. this.updateCharts();
  446. },
  447. clearDynamicCharts() {
  448. this.dynamicChartContainers.forEach(container => {
  449. // 移除图表容器
  450. container.remove();
  451. });
  452. // 清空数组
  453. this.dynamicChartContainers = [];
  454. },
  455. processData(data) {
  456. const groupedData = {};
  457. data.forEach(item => {
  458. if (!groupedData[item.hour]) {
  459. groupedData[item.hour] = { yxs: 0, yxl: 0 };
  460. }
  461. groupedData[item.hour].yxs += item.yxs;
  462. groupedData[item.hour].yxl += item.yxl;
  463. });
  464. this.lineData = Object.keys(groupedData).map(hour => ({
  465. hour, // 直接使用原始的日期字符串
  466. yxs: groupedData[hour].yxs,
  467. yxl: groupedData[hour].yxl
  468. })) // 按日期排序
  469. },
  470. updateCharts() {
  471. const xData = this.lineData.map(item => item.hour);
  472. const yxsData = this.lineData.map(item => item.yxs);
  473. const yxlData = this.lineData.map(item => item.yxl);
  474. const option = {
  475. tooltip: {
  476. trigger: 'axis',
  477. axisPointer: {
  478. type: 'cross',
  479. crossStyle: {
  480. color: '#999'
  481. }
  482. },
  483. // 格式化提示框内容
  484. formatter: function (params) {
  485. // let result = `${params[0].name}<br/>`;
  486. let result = '';
  487. params.forEach(param => {
  488. result += `${param.seriesName}: ${param.value}<br/>`;
  489. });
  490. return result;
  491. }
  492. },
  493. legend: {
  494. data: ['阳性个例', '阳性率'],
  495. show: true,
  496. // 调整图例位置
  497. top: '5%'
  498. },
  499. xAxis: {
  500. type: 'category', // 依然使用 category 类型
  501. data: xData,
  502. axisLabel: {
  503. // 格式化日期显示
  504. formatter: function (value) {
  505. return value;
  506. }
  507. },
  508. // 美化 X 轴轴线
  509. axisLine: {
  510. lineStyle: {
  511. color: '#666'
  512. }
  513. },
  514. // 美化 X 轴刻度线
  515. axisTick: {
  516. show: false
  517. }
  518. },
  519. yAxis: [
  520. {
  521. type: 'value',
  522. name: '阳性个例',
  523. position: 'left',
  524. axisLabel: {
  525. formatter: '{value}'
  526. },
  527. // 美化 Y 轴轴线
  528. axisLine: {
  529. lineStyle: {
  530. color: '#666'
  531. }
  532. },
  533. // 美化 Y 轴刻度线
  534. axisTick: {
  535. show: false
  536. },
  537. // 美化 Y 轴网格线
  538. splitLine: {
  539. lineStyle: {
  540. color: '#eee'
  541. }
  542. }
  543. },
  544. {
  545. type: 'value',
  546. name: '阳性率',
  547. position: 'right',
  548. axisLabel: {
  549. formatter: '{value}'
  550. },
  551. // 美化 Y 轴轴线
  552. axisLine: {
  553. lineStyle: {
  554. color: '#666'
  555. }
  556. },
  557. // 美化 Y 轴刻度线
  558. axisTick: {
  559. show: false
  560. },
  561. // 美化 Y 轴网格线
  562. splitLine: {
  563. lineStyle: {
  564. color: '#eee'
  565. }
  566. }
  567. }
  568. ],
  569. series: [
  570. {
  571. name: '阳性个例',
  572. type: 'bar',
  573. data: yxsData,
  574. // 更换柱状图颜色
  575. itemStyle: {
  576. color: '#91cc75'
  577. },
  578. // 调整柱状图宽度
  579. barWidth: '40%'
  580. },
  581. {
  582. name: '阳性率',
  583. type: 'line',
  584. yAxisIndex: 1,
  585. data: yxlData,
  586. // 让折线更圆滑
  587. smooth: true,
  588. // 更换折线颜色
  589. lineStyle: {
  590. color: '#fac858'
  591. },
  592. // 显示折线数据点
  593. symbol: 'circle',
  594. // 数据点大小
  595. symbolSize: 8,
  596. // 数据点颜色
  597. itemStyle: {
  598. color: '#fac858'
  599. },
  600. // 鼠标悬停时数据点变大
  601. emphasis: {
  602. symbolSize: 12
  603. }
  604. }
  605. ]
  606. };
  607. this.lineChart.setOption(option);
  608. }
  609. }
  610. }
  611. </script>
  612. <style scoped>
  613. .sample-statistics {
  614. padding: 24px;
  615. background: #fff;
  616. }
  617. .top-bar {
  618. margin-bottom: 24px;
  619. align-items: center;
  620. }
  621. .stat-box {
  622. display: flex;
  623. flex-direction: column;
  624. justify-content: center;
  625. height: 60px;
  626. }
  627. .stat-title {
  628. font-size: 16px;
  629. color: #888;
  630. }
  631. .stat-value {
  632. font-size: 28px;
  633. font-weight: bold;
  634. color: #409EFF;
  635. }
  636. .filter-col {
  637. display: flex;
  638. align-items: center;
  639. justify-content: flex-end;
  640. }
  641. .chart-section {
  642. margin-bottom: 24px;
  643. }
  644. .line-chart {
  645. width: 100%;
  646. height: 320px;
  647. }
  648. .pie-row {
  649. margin-top: 12px;
  650. }
  651. .pie-chart {
  652. width: 100%;
  653. height: 260px;
  654. }
  655. .pie-title {
  656. text-align: center;
  657. font-size: 16px;
  658. margin-bottom: 8px;
  659. color: #333;
  660. }
  661. .dynamic-chart {
  662. margin-bottom: 24px;
  663. border: 1px solid #eee;
  664. border-radius: 8px;
  665. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  666. }
  667. </style>