pestEchart.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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">
  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. // 三个关键时期数据
  109. periodData: {
  110. firstDate: '-',
  111. peakDate: '-',
  112. lastDate: '-'
  113. },
  114. chartData: {},
  115. // 每个标签对应的数据
  116. tabData: []
  117. };
  118. },
  119. methods: {
  120. async getPestNameDetail(name){
  121. const res = await this.$myRequest({
  122. url: '/api/pest_name_detail',
  123. method: 'POST',
  124. data: {
  125. name
  126. },
  127. });
  128. this.deviceInfo = res
  129. this.$emit('getInfo',res)
  130. },
  131. async setChartData(){
  132. if(!this.currentPest){
  133. return;
  134. }
  135. if(!this.d_id || !this.endDate){
  136. return;
  137. }
  138. const res = await this.$myRequest({
  139. url: '/api/api_gateway?method=forecast.cbd_analysis.pest_predict_time',
  140. method: 'POST',
  141. data: {
  142. model:'B',
  143. d_id: this.d_id,
  144. year: this.endDate.split('-')[0],
  145. pest: this.currentPest,
  146. },
  147. });
  148. this.periodData = {
  149. firstDate: res[0][0],
  150. peakDate: res[1][0],
  151. lastDate: res[2][0],
  152. }
  153. },
  154. initChart() {
  155. this.$nextTick(() => {
  156. setTimeout(() => {
  157. this.drawChart();
  158. }, 100);
  159. });
  160. },
  161. drawChart() {
  162. // 销毁已有的图表实例
  163. if (chartInstance) {
  164. chartInstance.dispose();
  165. chartInstance = null;
  166. }
  167. // 初始化图表
  168. const chartDom = document.getElementById('pestChart');
  169. if (!chartDom) return;
  170. // 确保 pest 数据是数字类型
  171. const dayData = this.day || [];
  172. const pestData = (this.pest || []).map(item => Number(item) || 0);
  173. chartInstance = echarts.init(chartDom);
  174. const option = {
  175. backgroundColor: '#FFFFFF',
  176. tooltip: {
  177. trigger: 'axis'
  178. },
  179. legend: {
  180. show: false
  181. },
  182. grid: {
  183. left: '3%',
  184. right: '4%',
  185. bottom: '3%',
  186. top: '3%',
  187. containLabel: true
  188. },
  189. xAxis: {
  190. type: 'category',
  191. boundaryGap: false,
  192. data: dayData,
  193. axisLine: {
  194. lineStyle: {
  195. color: '#CCCCCC'
  196. }
  197. },
  198. axisLabel: {
  199. fontSize: 11,
  200. color: '#999999'
  201. },
  202. splitLine: {
  203. show: false
  204. }
  205. },
  206. yAxis: {
  207. type: 'value',
  208. min: 0,
  209. minInterval: 1,
  210. splitNumber: 4,
  211. axisLine: {
  212. show: true,
  213. lineStyle: {
  214. color: '#CCCCCC'
  215. }
  216. },
  217. axisLabel: {
  218. fontSize: 11,
  219. color: '#999999',
  220. formatter: (value) => Math.floor(value)
  221. },
  222. splitLine: {
  223. show: true,
  224. lineStyle: {
  225. color: '#E5E5E5',
  226. type: 'dashed'
  227. }
  228. }
  229. },
  230. series: [
  231. {
  232. name: '虫量',
  233. type: 'line',
  234. smooth: true,
  235. data: pestData,
  236. lineStyle: {
  237. color: '#0085FF',
  238. width: 2
  239. },
  240. itemStyle: {
  241. color: '#0085FF',
  242. borderColor: '#0085FF',
  243. borderWidth: 2
  244. },
  245. symbol: 'circle',
  246. symbolSize: 6,
  247. }
  248. ]
  249. };
  250. chartInstance.setOption(option, true);
  251. // 监听窗口大小变化,调整图表大小
  252. window.addEventListener('resize', () => {
  253. chartInstance.resize();
  254. });
  255. },
  256. switchTab(index) {
  257. this.activeTab = index;
  258. // 更新数据
  259. const name = this.tabs[index]?.name;
  260. this.currentPest = name
  261. this.getPestNameDetail(name);
  262. this.setChartData();
  263. // this.periodData = this.tabData[index].periodData;
  264. // this.chartData = this.tabData[index].chartData;
  265. // 重新绘制图表
  266. this.$nextTick(() => {
  267. this.drawChart();
  268. });
  269. }
  270. }
  271. };
  272. </script>
  273. <style lang="scss" scoped>
  274. .pest-echart {
  275. overflow: hidden;
  276. margin-top: 24rpx;
  277. }
  278. .pest-echart__header {
  279. padding-top: 24rpx;
  280. }
  281. .tab-list {
  282. width: 100%;
  283. padding-bottom: 24rpx;
  284. overflow-x: scroll;
  285. white-space: nowrap;
  286. overflow-y: hidden;
  287. box-sizing: border-box;
  288. // 去掉滚动条
  289. -ms-overflow-style: none;
  290. scrollbar-width: none;
  291. }
  292. .tab-item {
  293. margin-right: 16rpx;
  294. font-size: 28rpx;
  295. font-family: 'Source Han Sans CN VF', sans-serif;
  296. font-weight: 500;
  297. color: #999999;
  298. display: inline-block;
  299. box-sizing: border-box;
  300. background: #ffffff;
  301. font-family: "Source Han Sans CN VF";
  302. font-size: 28rpx;
  303. padding: 10rpx 32rpx;
  304. border-radius: 8rpx;
  305. &:last-child {
  306. margin-right: 0;
  307. }
  308. &.active {
  309. color: #0BBC58;
  310. font-weight: 700;
  311. }
  312. &:first-child.active {
  313. color: #0BBC58;
  314. }
  315. }
  316. .pest-echart__content {
  317. background: #FFFFFF;
  318. border-radius: 16rpx;
  319. padding: 0 32rpx 32rpx;
  320. }
  321. .period-section {
  322. display: flex;
  323. justify-content: space-between;
  324. padding: 24rpx;
  325. background: #ffffff;
  326. border-radius: 12rpx;
  327. }
  328. .period-item {
  329. flex: 1;
  330. display: flex;
  331. flex-direction: column;
  332. align-items: center;
  333. }
  334. .period-label {
  335. font-size: 28rpx;
  336. color: #303133;
  337. font-family: 'Source Han Sans CN VF', sans-serif;
  338. font-weight: 700;
  339. margin-bottom: 8rpx;
  340. }
  341. .period-value {
  342. font-size: 24rpx;
  343. color: #999999;
  344. font-family: 'Source Han Sans CN VF', sans-serif;
  345. font-weight: 400;
  346. }
  347. .chart-container {
  348. position: relative;
  349. width: 100%;
  350. height: 400rpx;
  351. border-radius: 12rpx;
  352. overflow: hidden;
  353. }
  354. .chart-canvas {
  355. width: 100%;
  356. height: 100%;
  357. }
  358. </style>