rodentAnalysis.vue 18 KB

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