rodentAnalysis.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <template>
  2. <view class="rodent-analysis">
  3. <!-- 发生趋势分析 -->
  4. <view class="analysis-card">
  5. <view class="analysis-card__header">
  6. <view class="analysis-card__title">发生趋势分析</view>
  7. <!-- <view class="range-tabs">
  8. <view
  9. class="range-tab"
  10. :class="{ active: trendRange === 'week' }"
  11. @click="switchTrendRange('week')"
  12. >周</view>
  13. <view
  14. class="range-tab"
  15. :class="{ active: trendRange === 'month' }"
  16. @click="switchTrendRange('month')"
  17. >月</view>
  18. <view
  19. class="range-tab"
  20. :class="{ active: trendRange === 'year' }"
  21. @click="switchTrendRange('year')"
  22. >年</view>
  23. </view> -->
  24. </view>
  25. <view class="trend-legend">
  26. <view class="trend-legend__item" v-for="item in trendLegend" :key="item.name">
  27. <view class="trend-legend__line" :style="{ background: item.color }">
  28. <view class="trend-legend__dot" :style="{ borderColor: item.color }"></view>
  29. </view>
  30. <text class="trend-legend__text">{{ item.name }}</text>
  31. </view>
  32. </view>
  33. <view class="trend-axis-label">鼠害数量:只</view>
  34. <view class="chart-wrap chart-wrap--trend">
  35. <qiun-data-charts
  36. type="line"
  37. :chartData="trendChart"
  38. :opts="trendOpts"
  39. :reshow="active"
  40. :canvas2d="true"
  41. :inScrollView="true"
  42. :ontouch="true"
  43. />
  44. </view>
  45. </view>
  46. <!-- 鼠害占比 -->
  47. <view class="analysis-card">
  48. <view class="analysis-card__header">
  49. <view class="analysis-card__title">鼠害占比</view>
  50. </view>
  51. <view class="community-content">
  52. <view class="chart-wrap chart-wrap--ring">
  53. <qiun-data-charts
  54. type="ring"
  55. :chartData="communityChart"
  56. :opts="communityOpts"
  57. :reshow="active"
  58. :canvas2d="true"
  59. :inScrollView="true"
  60. :ontouch="true"
  61. :tooltip-show="false"
  62. @getIndex="onRingIndex"
  63. />
  64. </view>
  65. <view class="community-total">
  66. <text>总鼠害数量</text>
  67. <text class="total-value">{{ totalCount }}
  68. <text class="unit">只</text>
  69. </text>
  70. </view>
  71. <view class="community-list">
  72. <view
  73. class="community-item"
  74. :class="{ active: communitySelected === index }"
  75. v-for="(item, index) in communityList"
  76. :key="index"
  77. @click="onCommunityClick(index)"
  78. >
  79. <view class="community-item__dot" :style="{ background: item.color }"></view>
  80. <view class="community-item__name">{{ item.name }}</view>
  81. <view class="community-item__count">{{ item.count }}只</view>
  82. <view class="community-item__percent">{{ item.percent }}%</view>
  83. </view>
  84. </view>
  85. </view>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. // 鼠情分析:categories/series → 发生趋势,total → 鼠害占比
  91. const ANALYSIS_URL = '/api/api_gateway?method=forecast.shuhai.shuhai_analysis';
  92. // 趋势图配色:系列为 target_detail 里的各物种,数量不定,循环取色
  93. const TREND_PALETTE = ['#FF5252', '#0BBC58', '#3D5AFE', '#FFC107', '#9C27B0', '#00BCD4', '#FF7043', '#8BC34A', '#7986CB', '#F06292'];
  94. export default {
  95. name: 'RodentAnalysis',
  96. props: {
  97. // 当前 tab 是否激活(tab 用 v-show 切换时,隐藏状态下 canvas 容器测量为 0,需激活后重绘)
  98. active: {
  99. type: Boolean,
  100. default: true
  101. },
  102. // 设备 id:鼠情分析接口入参
  103. deviceId: {
  104. type: [String, Number],
  105. default: ''
  106. },
  107. // 查询范围(秒级时间戳),由页面日期选择驱动
  108. start: {
  109. type: [String, Number],
  110. default: 0
  111. },
  112. end: {
  113. type: [String, Number],
  114. default: 0
  115. }
  116. },
  117. data() {
  118. return {
  119. // 发生趋势
  120. trendChart: null,
  121. trendOpts: {
  122. // 颜色顺序与 series 一一对应
  123. color: ['#FF5252', '#0BBC58', '#3D5AFE'],
  124. dataLabel: false,
  125. // 不绘制折线上的数据点圆点
  126. dataPointShape: false,
  127. // 图例用卡片内自定义样式(见 template 的 trend-legend)
  128. legend: { show: false },
  129. xAxis: {
  130. disableGrid: true,
  131. fontSize: 9,
  132. fontColor: '#656565',
  133. // 'justify' 点落在刻度线上(线条铺满左右);'center' 点位于两刻度中间
  134. boundaryGap: 'justify',
  135. labelCount: 6
  136. },
  137. yAxis: {
  138. disableGrid: false,
  139. gridType: 'dash',
  140. gridColor: '#EEEEEE',
  141. data: [{
  142. min: 0,
  143. max: 50,
  144. splitNumber: 5,
  145. fontSize: 10,
  146. fontColor: '#656565',
  147. tofix: 0
  148. }]
  149. },
  150. extra: {
  151. line: {
  152. type: 'curve',
  153. width: 2,
  154. activeType: 'none'
  155. }
  156. }
  157. },
  158. // 群落结构
  159. communityList: [],
  160. // 当前选中的占比列表项索引(-1 为未选中)
  161. communitySelected: -1,
  162. communityChart: null,
  163. communityOpts: {
  164. color: ['#E53935', '#FFC107', '#2196F3', '#9E9E9E'],
  165. // 点击联动会触发整图重绘(canvas 无局部更新),关掉动画避免每次点击重放入场动画
  166. animation: false,
  167. legend: { show: false },
  168. // 环形图:中心显示当前占比最高的物种
  169. dataLabel: false,
  170. // uCharts 以 opts.tooltip.index 命中扇区高亮(-1 为不高亮),点击列表时更新
  171. tooltip: { index: -1 },
  172. title: {
  173. name: '0%',
  174. fontSize: 16,
  175. color: '#E53935',
  176. offsetY: -6
  177. },
  178. subtitle: {
  179. name: '--',
  180. fontSize: 10,
  181. color: '#333333',
  182. offsetY: 8
  183. },
  184. extra: {
  185. ring: {
  186. // 环粗细约为半径的三分之一
  187. ringWidth: 16,
  188. offsetAngle: 0,
  189. // 去掉分段之间的白色分隔线(qiun-data-charts 的 ring 默认 border: true)
  190. border: false,
  191. // 关闭点击分段时外扩+半透明的高亮效果(默认 activeRadius: 10 / activeOpacity: 0.5)
  192. activeRadius: 5,
  193. activeOpacity: 1
  194. }
  195. }
  196. },
  197. // 总量
  198. totalCount: 0,
  199. };
  200. },
  201. computed: {
  202. // 趋势图图例:跟随接口返回的物种系列
  203. trendLegend() {
  204. if (!this.trendChart) return [];
  205. return this.trendChart.series.map(s => ({ name: s.name, color: s.color }));
  206. },
  207. // 依赖项拼接,任一变化即重新拉取
  208. fetchKey() {
  209. return [this.deviceId, this.start, this.end].join('_');
  210. }
  211. },
  212. mounted() {
  213. // 延迟拉取:等页面布局稳定后再初始化 canvas,避免容器尺寸测量为 0
  214. setTimeout(() => {
  215. this.fetchAnalysis();
  216. }, 300);
  217. },
  218. watch: {
  219. // 设备或日期范围变化时重新拉取
  220. fetchKey() {
  221. this.fetchAnalysis();
  222. }
  223. },
  224. methods: {
  225. // 拉取鼠情分析数据:categories/series → 发生趋势,total → 鼠害占比
  226. async fetchAnalysis() {
  227. if (!this.deviceId || !this.start || !this.end) return;
  228. try {
  229. const res = await this.$myRequest({
  230. url: ANALYSIS_URL,
  231. method: 'POST',
  232. data: {
  233. device_id: this.deviceId,
  234. start: this.start,
  235. end: this.end
  236. }
  237. });
  238. if (!res) return;
  239. this.buildTrendChart(res.categories || [], res.series || []);
  240. this.buildCommunity(res.total || []);
  241. } catch (e) {
  242. // $myRequest 已统一 toast 错误信息
  243. }
  244. },
  245. // 发生趋势:x 轴为日期(MM-DD),系列为接口返回的各物种
  246. buildTrendChart(categories, seriesList) {
  247. const cats = categories.map(date => (date || '').slice(5));
  248. const series = seriesList.map((item, index) => ({
  249. name: item.name,
  250. color: TREND_PALETTE[index % TREND_PALETTE.length],
  251. data: (item.data || []).map(v => v || 0)
  252. }));
  253. this.trendChart = { categories: cats, series };
  254. if (!cats.length) return;
  255. // Y 轴上限随数据量调整,向上取整到 10 的倍数
  256. let maxVal = 0;
  257. series.forEach(s => {
  258. s.data.forEach(v => {
  259. if (v > maxVal) maxVal = v;
  260. });
  261. });
  262. this.trendOpts.yAxis.data[0].max = Math.max(10, Math.ceil(maxVal / 10) * 10);
  263. // 单天数据画不出折线,改为显示数据点;boundaryGap 切为 'center' 让点位/刻度居中,多天时 'justify' 贴边铺满
  264. this.trendOpts.dataPointShape = cats.length === 1;
  265. this.trendOpts.xAxis.boundaryGap = cats.length === 1 ? 'center' : 'justify';
  266. },
  267. // 鼠害占比:环形图 + 列表,物种来自 total;颜色跟随趋势图同名系列,两图保持一致
  268. buildCommunity(totalList) {
  269. const colorByName = {};
  270. ((this.trendChart && this.trendChart.series) || []).forEach(s => {
  271. colorByName[s.name] = s.color;
  272. });
  273. const list = totalList.map((item, index) => ({
  274. name: item.name,
  275. count: item.value || 0,
  276. color: colorByName[item.name] || TREND_PALETTE[index % TREND_PALETTE.length]
  277. }));
  278. const total = list.reduce((sum, item) => sum + item.count, 0);
  279. list.forEach(item => {
  280. item.percent = total ? Math.round((item.count / total) * 100) : 0;
  281. });
  282. this.communityList = list;
  283. this.communitySelected = -1;
  284. this.totalCount = total;
  285. // 环形图扇区颜色与列表圆点保持一致
  286. this.communityOpts.color = list.map(item => item.color);
  287. this.communityOpts.tooltip = { index: -1 };
  288. this.communityChart = {
  289. series: [{
  290. data: list.map(item => ({ name: item.name, value: item.count }))
  291. }]
  292. };
  293. // 环形中心显示占比最高的物种
  294. if (list.length) {
  295. const top = list.reduce((a, b) => (b.count > a.count ? b : a), list[0]);
  296. this.communityOpts.title.name = top.percent + '%';
  297. this.communityOpts.subtitle.name = top.name;
  298. }
  299. },
  300. // 点击占比列表:环形图高亮对应扇区,中心数据联动;再次点击取消选中
  301. onCommunityClick(index) {
  302. const item = this.communityList[index];
  303. if (!item) return;
  304. const next = this.communitySelected === index ? -1 : index;
  305. this.communitySelected = next;
  306. const target = next > -1
  307. ? item
  308. : this.communityList.reduce((a, b) => (b.count > a.count ? b : a), this.communityList[0]);
  309. this.communityOpts.title.name = target.percent + '%';
  310. this.communityOpts.subtitle.name = target.name;
  311. this.communityOpts.tooltip = { index: next };
  312. },
  313. // 点击环形图扇区:中心数据联动,复用列表点击逻辑
  314. onRingIndex(e) {
  315. if (!e || typeof e.currentIndex !== 'number' || e.currentIndex < 0) return;
  316. this.onCommunityClick(e.currentIndex);
  317. }
  318. }
  319. };
  320. </script>
  321. <style lang="scss" scoped>
  322. .rodent-analysis {
  323. width: 100%;
  324. }
  325. .analysis-card {
  326. width: 100%;
  327. padding: 32rpx;
  328. box-sizing: border-box;
  329. background: #fff;
  330. border-radius: 16rpx;
  331. margin-bottom: 24rpx;
  332. &:last-child {
  333. margin-bottom: 0;
  334. }
  335. }
  336. .analysis-card__header {
  337. display: flex;
  338. align-items: center;
  339. justify-content: space-between;
  340. margin-bottom: 20rpx;
  341. }
  342. .analysis-card__title {
  343. position: relative;
  344. font-size: 32rpx;
  345. color: #042118;
  346. font-family: 'Source Han Sans CN VF';
  347. font-weight: 700;
  348. }
  349. .analysis-card__extra {
  350. display: flex;
  351. align-items: center;
  352. .peak-label {
  353. font-size: 24rpx;
  354. color: #656565;
  355. font-family: 'Source Han Sans CN VF';
  356. margin-right: 8rpx;
  357. }
  358. .peak-value {
  359. font-size: 24rpx;
  360. color: #E53935;
  361. font-family: 'Source Han Sans CN VF';
  362. font-weight: 700;
  363. }
  364. }
  365. .chart-legend {
  366. display: flex;
  367. align-items: center;
  368. justify-content: flex-end;
  369. margin-bottom:-20rpx;
  370. .chart-legend__dot--bar {
  371. width: 16rpx;
  372. height: 16rpx;
  373. border-radius: 4rpx;
  374. background: #FFC107;
  375. margin-right: 8rpx;
  376. }
  377. .chart-legend__text {
  378. font-size: 20rpx;
  379. color: #656565;
  380. font-family: 'Source Han Sans CN VF';
  381. }
  382. }
  383. .chart-wrap {
  384. position: relative;
  385. width: 100%;
  386. }
  387. .chart-wrap--rhythm {
  388. height: 400rpx;
  389. }
  390. .chart-wrap--ring {
  391. width: 360rpx;
  392. height: 320rpx;
  393. flex-shrink: 0;
  394. }
  395. .chart-wrap--weather {
  396. height: 420rpx;
  397. }
  398. // 发生趋势分析:自定义图例(曲线图例:短线 + 空心圆点)
  399. .trend-legend {
  400. display: flex;
  401. flex-wrap: wrap;
  402. padding: 20rpx 24rpx;
  403. background: #F5F6FA;
  404. border-radius: 12rpx;
  405. margin-bottom: 20rpx;
  406. }
  407. .trend-legend__item {
  408. display: flex;
  409. align-items: center;
  410. width: 33.33%;
  411. box-sizing: border-box;
  412. padding: 8rpx 0;
  413. }
  414. .trend-legend__line {
  415. position: relative;
  416. width: 40rpx;
  417. height: 4rpx;
  418. margin-right: 12rpx;
  419. flex-shrink: 0;
  420. }
  421. .trend-legend__dot {
  422. position: absolute;
  423. left: 50%;
  424. top: 50%;
  425. transform: translate(-50%, -50%);
  426. width: 16rpx;
  427. height: 16rpx;
  428. border-radius: 50%;
  429. background: #fff;
  430. border: 4rpx solid #fff;
  431. box-sizing: border-box;
  432. }
  433. .trend-legend__text {
  434. font-size: 26rpx;
  435. color: #042118;
  436. font-family: 'Source Han Sans CN VF';
  437. }
  438. .trend-axis-label {
  439. font-size: 22rpx;
  440. color: #B3B9C2;
  441. font-family: 'Source Han Sans CN VF';
  442. margin-bottom: 4rpx;
  443. }
  444. .chart-wrap--trend {
  445. height: 420rpx;
  446. }
  447. // 群落结构:上下布局,环形图在上,图例列表在下
  448. .community-content {
  449. display: flex;
  450. flex-direction: column;
  451. align-items: center;
  452. }
  453. .community-total{
  454. width: 90%;
  455. display: flex;
  456. align-items: center;
  457. justify-content: space-between;
  458. font-family: 'Source Han Sans CN VF';
  459. .total-value {
  460. font-size: 32rpx;
  461. color: #042118;
  462. font-weight: 700;
  463. line-height: 1;
  464. }
  465. .unit {
  466. font-size: 28rpx;
  467. font-weight: 400;
  468. color:#999999;
  469. margin-left: 8rpx;
  470. }
  471. }
  472. .community-list {
  473. width: 90%;
  474. margin-top: 16rpx;
  475. }
  476. .community-item {
  477. display: flex;
  478. align-items: center;
  479. padding: 12rpx 12rpx;
  480. border-radius: 8rpx;
  481. font-family: 'Source Han Sans CN VF';
  482. &.active {
  483. background: #F5F6FA;
  484. }
  485. .community-item__dot {
  486. width: 16rpx;
  487. height: 16rpx;
  488. border-radius: 4rpx;
  489. margin-right: 12rpx;
  490. flex-shrink: 0;
  491. }
  492. .community-item__name {
  493. flex: 1;
  494. font-size: 26rpx;
  495. color: #042118;
  496. }
  497. .community-item__count {
  498. font-size: 26rpx;
  499. color: #042118;
  500. margin-right: 16rpx;
  501. }
  502. .community-item__percent {
  503. font-size: 26rpx;
  504. color: #656565;
  505. width: 72rpx;
  506. text-align: right;
  507. }
  508. }
  509. // 总量害虫数量
  510. .total-main {
  511. display: flex;
  512. align-items: flex-end;
  513. margin-bottom: 24rpx;
  514. font-family: 'Source Han Sans CN VF';
  515. .total-value {
  516. font-size: 56rpx;
  517. color: #042118;
  518. font-weight: 700;
  519. line-height: 1;
  520. .total-unit {
  521. font-size: 28rpx;
  522. font-weight: 500;
  523. margin-left: 8rpx;
  524. }
  525. }
  526. .total-compare {
  527. font-size: 24rpx;
  528. color: #656565;
  529. margin-left: 24rpx;
  530. padding-bottom: 4rpx;
  531. .total-compare__value {
  532. color: #E53935;
  533. font-weight: 700;
  534. }
  535. }
  536. }
  537. .total-bar {
  538. .total-bar__track {
  539. position: relative;
  540. height: 16rpx;
  541. border-radius: 8rpx;
  542. background: #F1F4F8;
  543. overflow: visible;
  544. }
  545. .total-bar__fill {
  546. height: 100%;
  547. border-radius: 8rpx;
  548. background: #0BBC58;
  549. }
  550. .total-bar__marker {
  551. position: absolute;
  552. top: -6rpx;
  553. width: 4rpx;
  554. height: 28rpx;
  555. border-radius: 2rpx;
  556. background: #E53935;
  557. transform: translateX(-50%);
  558. }
  559. .total-bar__scale {
  560. display: flex;
  561. justify-content: space-between;
  562. margin-top: 8rpx;
  563. font-size: 22rpx;
  564. color: #999999;
  565. font-family: 'Source Han Sans CN VF';
  566. }
  567. }
  568. // 周/月/年切换
  569. .range-tabs {
  570. display: flex;
  571. align-items: center;
  572. .range-tab {
  573. width: 64rpx;
  574. height: 48rpx;
  575. line-height: 44rpx;
  576. text-align: center;
  577. font-size: 24rpx;
  578. color: #042118;
  579. font-family: 'Source Han Sans CN VF';
  580. background: #fff;
  581. border: 2rpx solid #E4E7ED;
  582. box-sizing: border-box;
  583. &:first-child {
  584. border-radius: 8rpx 0 0 8rpx;
  585. }
  586. &:last-child {
  587. border-radius: 0 8rpx 8rpx 0;
  588. }
  589. &.active {
  590. background: #0BBC58;
  591. border-color: #0BBC58;
  592. color: #fff;
  593. font-weight: 700;
  594. }
  595. }
  596. }
  597. </style>