1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
| // pages/modeCompute/modeCompute.js
| const dayjs = require('dayjs');
| const { get } = require('../../api/request');
|
| import * as echarts from '../../ec-canvas/echarts';
|
| function initChart(canvas, width, height, dpr) {
| const chart = echarts.init(canvas, null, {
| width: width,
| height: height,
| devicePixelRatio: dpr
| });
|
| // 将chart实例保存到页面实例中
| const pages = getCurrentPages();
| const currentPage = pages[pages.length - 1];
| currentPage.chart = chart;
|
| // 确保图表完全初始化后再更新数据
| setTimeout(() => {
| if (currentPage.updateEChartsData && currentPage.data.chartData && currentPage.data.chartData.length > 0) {
| currentPage.updateEChartsData();
| }
| }, 200);
|
| return chart;
| }
|
| Page({
| /**
| * 页面的初始数据
| */
| data: {
| // 显示类型:chart 图表,list 列表
| displayType: 'chart',
|
| // 加载状态
| isLoading: false,
|
| // 农作物相关
| selectedCrop: {},
| cropList: [],
| showCropPopup: false,
|
| // 日期相关
| startDate: '',
| endDate: '',
| showDatePicker: false,
| pickerDate: '',
| currentDateType: '', // 'start' 或 'end'
|
| // 数据相关
| tableData: [],
| chartData: [],
|
| // ECharts相关
| ec: {
| onInit: initChart
| }
| },
|
| /**
| * 生命周期函数--监听页面加载
| */
| onLoad(options) {
| this.initDefaultData();
| },
|
| /**
| * 生命周期函数--监听页面初次渲染完成
| */
| onReady() {
| // 图表初始化由ec-canvas组件自动处理
| },
|
| /**
| * 初始化图表(备用方法)
| */
| initChart() {
| // 这个方法主要用于重新初始化图表
| console.log('尝试重新初始化图表');
| },
|
| /**
| * 生命周期函数--监听页面显示
| */
| onShow() {
|
| },
|
| /**
| * 初始化默认数据
| */
| initDefaultData() {
| const today = dayjs();
| const startDate = today.subtract(30, 'day').format('YYYY-MM-DD');
| const endDate = today.format('YYYY-MM-DD');
|
| this.setData({
| startDate: startDate,
| endDate: endDate
| });
|
| // 获取农作物列表
| this.getCropList();
| },
|
| /**
| * 获取农作物列表
| */
| async getCropList() {
| try {
| this.setData({ isLoading: true });
|
| const result = await get({
| url: '/wx/mdVapor/allCropsWithYesterday'
| });
|
| if (result.success && result.content) {
| this.setData({
| cropList: result.content,
| selectedCrop: result.content[0] || {},
| isLoading: false
| });
|
| // 如果有默认选中的农作物,获取数据
| if (result.content.length > 0) {
| this.getCropData();
| }
| } else {
| this.setData({ isLoading: false });
| wx.showToast({
| title: result.msg || '获取农作物列表失败',
| icon: 'none'
| });
| }
| } catch (error) {
| this.setData({ isLoading: false });
| wx.showToast({
| title: '网络请求失败',
| icon: 'none'
| });
| console.error('获取农作物列表失败:', error);
| }
| },
|
| /**
| * 获取作物数据
| */
| async getCropData() {
| if (!this.data.selectedCrop.cropId || !this.data.startDate || !this.data.endDate) {
| console.log('缺少必要参数');
| return;
| }
|
| try {
| this.setData({ isLoading: true });
|
| const result = await get({
| url: '/wx/mdVapor/oneCropsSomeEt0',
| data: {
| cropId: this.data.selectedCrop.cropId,
| timeStart: this.data.startDate,
| timeStop: this.data.endDate
| }
| });
|
| if (result.success && result.content) {
| const tableData = [];
| const chartData = [];
|
| result.content.forEach((item, index) => {
| const tableItem = {
| cropName: item.cropName,
| date: item.dt,
| transpiration: item.et0 ? item.et0.toFixed(2) : '0.00',
| maxTemp: item.maxTmp || 0,
| minTemp: item.minTmp || 0,
| cropCoefficient: item.factor ? item.factor.toFixed(2) : '0.00'
| };
|
| tableData.push(tableItem);
| chartData.push({
| date: item.dt,
| value: item.et0 || 0
| });
| });
|
| this.setData({
| tableData: tableData,
| chartData: chartData,
| isLoading: false
| });
|
| // 如果图表已初始化,更新图表数据
| if (this.chart && this.data.displayType === 'chart') {
| // 延迟更新,确保数据已设置
| setTimeout(() => {
| this.updateEChartsData();
| }, 100);
| }
| } else {
| this.setData({ isLoading: false });
| wx.showToast({
| title: result.msg || '获取作物数据失败',
| icon: 'none'
| });
| }
| } catch (error) {
| this.setData({ isLoading: false });
| wx.showToast({
| title: '网络请求失败',
| icon: 'none'
| });
| console.error('获取作物数据失败:', error);
| }
| },
|
| /**
| * 切换到图表视图
| */
| switchToChart() {
| this.setData({
| displayType: 'chart'
| });
|
| // 延迟更新图表,确保DOM已渲染
| setTimeout(() => {
| if (this.chart) {
| this.updateEChartsData();
| } else {
| console.log('图表实例不存在,尝试重新初始化');
| // 如果图表实例不存在,尝试重新初始化
| this.initChart();
| }
| }, 200);
| },
|
| /**
| * 切换到列表视图
| */
| switchToList() {
| this.setData({
| displayType: 'list'
| });
| },
|
| /**
| * 显示农作物选择弹窗
| */
| showCropPicker() {
| this.setData({
| showCropPopup: true
| });
| },
|
| /**
| * 农作物弹窗显示状态改变
| */
| onCropPopupChange(e) {
| this.setData({
| showCropPopup: e.detail.visible
| });
| },
|
| /**
| * 选择农作物
| */
| selectCrop(e) {
| const crop = e.currentTarget.dataset.crop;
| this.setData({
| selectedCrop: crop,
| showCropPopup: false
| });
|
| // 重新获取数据
| this.getCropData();
| },
|
| /**
| * 显示起始日期选择器
| */
| showStartDatePicker() {
| this.setData({
| showDatePicker: true,
| currentDateType: 'start',
| pickerDate: this.data.startDate || dayjs().format('YYYY-MM-DD')
| });
| },
|
| /**
| * 显示结束日期选择器
| */
| showEndDatePicker() {
| this.setData({
| showDatePicker: true,
| currentDateType: 'end',
| pickerDate: this.data.endDate || dayjs().format('YYYY-MM-DD')
| });
| },
|
| /**
| * 日期改变
| */
| onDateChange(e) {
| // 格式化日期,只保留年月日
| const formattedDate = dayjs(e.detail.value).format('YYYY-MM-DD');
| this.setData({
| pickerDate: formattedDate
| });
| },
|
| /**
| * 取消日期选择
| */
| onDateCancel() {
| this.setData({
| showDatePicker: false
| });
| },
|
| /**
| * 确认日期选择
| */
| onDateConfirm(e) {
| const selectedDate = e.detail.value;
| // 格式化日期,只保留年月日
| const formattedDate = dayjs(selectedDate).format('YYYY-MM-DD');
|
| const updateData = {
| showDatePicker: false
| };
|
| if (this.data.currentDateType === 'start') {
| updateData.startDate = formattedDate;
| } else {
| updateData.endDate = formattedDate;
| }
|
| this.setData(updateData);
|
| // 重新获取数据
| this.getCropData();
| },
|
| /**
| * 更新ECharts图表数据
| */
| updateEChartsData() {
| if (!this.chart) {
| console.log('图表实例不存在,无法更新数据');
| return;
| }
|
| // 处理数据
| const hasData = this.data.chartData && this.data.chartData.length > 0;
| const xAxisData = hasData ?
| this.data.chartData.map(item => dayjs(item.date).format('MM-DD')) :
| ['暂无数据'];
| const seriesData = hasData ?
| this.data.chartData.map(item => item.value) :
| [0];
|
| const option = {
| backgroundColor: 'transparent',
| grid: {
| left: '10%',
| right: '10%',
| bottom: '15%',
| top: '10%',
| containLabel: true
| },
| tooltip: {
| show: true,
| trigger: 'axis',
| backgroundColor: 'rgba(0, 0, 0, 0.8)',
| borderColor: '#1890FF',
| borderWidth: 1,
| textStyle: {
| color: '#fff',
| fontSize: 14
| },
| formatter: function(params) {
| if (!params || params.length === 0) return '';
| const param = params[0];
| return `${param.axisValue} 蒸腾量: ${param.value} mm/day`;
| }
| },
| xAxis: {
| type: 'category',
| data: xAxisData,
| axisLine: {
| lineStyle: { color: '#d0d7de', width: 2 }
| },
| axisLabel: {
| color: '#666',
| fontSize: 12
| }
| },
| yAxis: {
| type: 'value',
| name: '蒸腾量(mm/day)',
| nameTextStyle: {
| color: '#333',
| fontSize: 14
| },
| axisLine: {
| lineStyle: { color: '#d0d7de', width: 2 }
| },
| axisLabel: {
| color: '#666',
| fontSize: 12
| },
| splitLine: {
| lineStyle: {
| color: 'rgba(0, 0, 0, 0.05)',
| width: 1
| }
| }
| },
| series: [{
| data: seriesData,
| type: 'line',
| smooth: true,
| symbol: hasData ? 'circle' : 'none',
| symbolSize: 8,
| lineStyle: {
| color: '#1890FF',
| width: 3
| },
| itemStyle: {
| color: '#1890FF',
| borderColor: '#fff',
| borderWidth: 2
| },
| areaStyle: hasData ? {
| color: {
| type: 'linear',
| x: 0, y: 0, x2: 0, y2: 1,
| colorStops: [
| { offset: 0, color: 'rgba(24, 144, 255, 0.3)' },
| { offset: 1, color: 'rgba(24, 144, 255, 0.05)' }
| ]
| }
| } : null
| }]
| };
|
| this.chart.setOption(option, true);
| },
|
|
|
| /**
| * 页面相关事件处理函数--监听用户下拉动作
| */
| onPullDownRefresh() {
| this.getCropData();
| wx.stopPullDownRefresh();
| },
|
| /**
| * 页面上拉触底事件的处理函数
| */
| onReachBottom() {
|
| },
|
| /**
| * 用户点击右上角分享
| */
| onShareAppMessage() {
| return {
| title: '模型计算',
| path: '/pages/modeCompute/modeCompute'
| };
| }
| })
|
|