pestEchart.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <template>
  2. <view class="pest-echart">
  3. <view class="pest-echart__header">
  4. <view class="tab-list">
  5. <view
  6. v-for="(tab, index) in tabs"
  7. :key="index"
  8. class="tab-item"
  9. :class="{ active: activeTab === index }"
  10. @click="switchTab(index)"
  11. >
  12. {{ tab.name }}
  13. </view>
  14. </view>
  15. </view>
  16. <view class="pest-echart__content" v-if="dayData.length">
  17. <!-- 三个关键时期 -->
  18. <view class="period-section">
  19. <view class="period-item">
  20. <view class="period-label">始见期</view>
  21. <view class="period-value">{{ periodData.firstDate }}</view>
  22. </view>
  23. <view class="period-item">
  24. <view class="period-label">高峰期</view>
  25. <view class="period-value">{{ periodData.peakDate }}</view>
  26. </view>
  27. <view class="period-item">
  28. <view class="period-label">终见期</view>
  29. <view class="period-value">{{ periodData.lastDate }}</view>
  30. </view>
  31. </view>
  32. <!-- 图表区域 -->
  33. <view class="chart-container">
  34. <div id="pestChart" class="chart-canvas"></div>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import * as echarts from 'echarts';
  41. let chartInstance = null;
  42. export default {
  43. name: 'PestEchart',
  44. props:{
  45. pest_order:{
  46. type: Object,
  47. default: () => ({})
  48. },
  49. endDate: {
  50. type: String,
  51. default: ''
  52. },
  53. d_id: {
  54. type: String | Number,
  55. default: ''
  56. },
  57. day:{
  58. type: Array,
  59. default: () => []
  60. },
  61. pest:{
  62. type: Array,
  63. default: () => []
  64. },
  65. },
  66. watch:{
  67. pest_order:{
  68. handler(val){
  69. this.tabs = [];
  70. for(let key in val){
  71. this.tabs.push({
  72. name: key,
  73. })
  74. }
  75. const name = this.tabs[0]?.name;
  76. this.currentPest = name;
  77. if(this.currentPest){
  78. this.getPestNameDetail(name);
  79. this.setChartData();
  80. }
  81. },
  82. deep: true
  83. },
  84. d_id:{
  85. handler(val){
  86. val && this.setChartData();
  87. },
  88. deep: true
  89. },
  90. endDate:{
  91. handler(val){
  92. val && this.setChartData();
  93. },
  94. deep: true
  95. },
  96. day:{
  97. handler(){
  98. this.initChart();
  99. },
  100. deep: true
  101. },
  102. },
  103. data() {
  104. return {
  105. tabs: [],
  106. currentPest:'',
  107. activeTab: 0,
  108. dayData:[],
  109. // 三个关键时期数据
  110. periodData: {
  111. firstDate: '-',
  112. peakDate: '-',
  113. lastDate: '-'
  114. },
  115. chartData: {},
  116. // 每个标签对应的数据
  117. tabData: []
  118. };
  119. },
  120. methods: {
  121. async getPestNameDetail(name){
  122. const res = await this.$myRequest({
  123. url: '/api/pest_name_detail',
  124. method: 'POST',
  125. data: {
  126. name
  127. },
  128. });
  129. this.deviceInfo = res
  130. this.$emit('getInfo',res)
  131. },
  132. async setChartData(){
  133. if(!this.currentPest){
  134. return;
  135. }
  136. if(!this.d_id || !this.endDate){
  137. return;
  138. }
  139. const res = await this.$myRequest({
  140. url: '/api/api_gateway?method=forecast.cbd_analysis.pest_predict_time',
  141. method: 'POST',
  142. data: {
  143. model:'B',
  144. d_id: this.d_id,
  145. year: this.endDate.split('-')[0],
  146. pest: this.currentPest,
  147. },
  148. });
  149. this.periodData = {
  150. firstDate: res[0][0],
  151. peakDate: res[1][0],
  152. lastDate: res[2][0],
  153. }
  154. },
  155. initChart() {
  156. this.$nextTick(() => {
  157. setTimeout(() => {
  158. this.drawChart();
  159. }, 100);
  160. });
  161. },
  162. drawChart() {
  163. // 销毁已有的图表实例
  164. if (chartInstance) {
  165. chartInstance.dispose();
  166. chartInstance = null;
  167. }
  168. // 初始化图表
  169. const chartDom = document.getElementById('pestChart');
  170. if (!chartDom) return;
  171. // 确保 pest 数据是数字类型
  172. const dayData = this.day || [];
  173. this.dayData = dayData;
  174. const pestData = (this.pest || []).map(item => Number(item) || 0);
  175. chartInstance = echarts.init(chartDom);
  176. const option = {
  177. backgroundColor: '#FFFFFF',
  178. tooltip: {
  179. trigger: 'axis'
  180. },
  181. legend: {
  182. show: false
  183. },
  184. grid: {
  185. left: '3%',
  186. right: '4%',
  187. bottom: '3%',
  188. top: '3%',
  189. containLabel: true
  190. },
  191. xAxis: {
  192. type: 'category',
  193. boundaryGap: false,
  194. data: dayData,
  195. axisLine: {
  196. lineStyle: {
  197. color: '#CCCCCC'
  198. }
  199. },
  200. axisLabel: {
  201. fontSize: 11,
  202. color: '#999999'
  203. },
  204. splitLine: {
  205. show: false
  206. }
  207. },
  208. yAxis: {
  209. type: 'value',
  210. min: 0,
  211. minInterval: 1,
  212. splitNumber: 4,
  213. axisLine: {
  214. show: true,
  215. lineStyle: {
  216. color: '#CCCCCC'
  217. }
  218. },
  219. axisLabel: {
  220. fontSize: 11,
  221. color: '#999999',
  222. formatter: (value) => Math.floor(value)
  223. },
  224. splitLine: {
  225. show: true,
  226. lineStyle: {
  227. color: '#E5E5E5',
  228. type: 'dashed'
  229. }
  230. }
  231. },
  232. series: [
  233. {
  234. name: '虫量',
  235. type: 'line',
  236. smooth: true,
  237. data: pestData,
  238. lineStyle: {
  239. color: '#0085FF',
  240. width: 2
  241. },
  242. itemStyle: {
  243. color: '#0085FF',
  244. borderColor: '#0085FF',
  245. borderWidth: 2
  246. },
  247. symbol: 'circle',
  248. symbolSize: 6,
  249. }
  250. ]
  251. };
  252. chartInstance.setOption(option, true);
  253. // 监听窗口大小变化,调整图表大小
  254. window.addEventListener('resize', () => {
  255. chartInstance.resize();
  256. });
  257. },
  258. switchTab(index) {
  259. this.activeTab = index;
  260. // 更新数据
  261. const name = this.tabs[index]?.name;
  262. this.currentPest = name
  263. this.getPestNameDetail(name);
  264. this.setChartData();
  265. // this.periodData = this.tabData[index].periodData;
  266. // this.chartData = this.tabData[index].chartData;
  267. // 重新绘制图表
  268. this.$nextTick(() => {
  269. this.drawChart();
  270. });
  271. }
  272. }
  273. };
  274. </script>
  275. <style lang="scss" scoped>
  276. .pest-echart {
  277. overflow: hidden;
  278. margin-top: 24rpx;
  279. }
  280. .pest-echart__header {
  281. padding-top: 24rpx;
  282. }
  283. .tab-list {
  284. width: 100%;
  285. padding-bottom: 24rpx;
  286. overflow-x: scroll;
  287. white-space: nowrap;
  288. overflow-y: hidden;
  289. box-sizing: border-box;
  290. // 去掉滚动条
  291. -ms-overflow-style: none;
  292. scrollbar-width: none;
  293. }
  294. .tab-item {
  295. margin-right: 16rpx;
  296. font-size: 28rpx;
  297. font-family: 'Source Han Sans CN VF', sans-serif;
  298. font-weight: 500;
  299. color: #999999;
  300. display: inline-block;
  301. box-sizing: border-box;
  302. background: #ffffff;
  303. font-family: "Source Han Sans CN VF";
  304. font-size: 28rpx;
  305. padding: 10rpx 32rpx;
  306. border-radius: 8rpx;
  307. &:last-child {
  308. margin-right: 0;
  309. }
  310. &.active {
  311. color: #0BBC58;
  312. font-weight: 700;
  313. }
  314. &:first-child.active {
  315. color: #0BBC58;
  316. }
  317. }
  318. .pest-echart__content {
  319. background: #FFFFFF;
  320. border-radius: 16rpx;
  321. padding: 0 32rpx 32rpx;
  322. }
  323. .period-section {
  324. display: flex;
  325. justify-content: space-between;
  326. padding: 24rpx;
  327. background: #ffffff;
  328. border-radius: 12rpx;
  329. }
  330. .period-item {
  331. flex: 1;
  332. display: flex;
  333. flex-direction: column;
  334. align-items: center;
  335. }
  336. .period-label {
  337. font-size: 28rpx;
  338. color: #303133;
  339. font-family: 'Source Han Sans CN VF', sans-serif;
  340. font-weight: 700;
  341. margin-bottom: 8rpx;
  342. }
  343. .period-value {
  344. font-size: 24rpx;
  345. color: #999999;
  346. font-family: 'Source Han Sans CN VF', sans-serif;
  347. font-weight: 400;
  348. }
  349. .chart-container {
  350. position: relative;
  351. width: 100%;
  352. height: 400rpx;
  353. border-radius: 12rpx;
  354. overflow: hidden;
  355. }
  356. .chart-canvas {
  357. width: 100%;
  358. height: 100%;
  359. }
  360. </style>