realtimewater.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // pages/realtimewater.js
  2. import * as echarts from '../../components/ec-canvas/echarts.js';
  3. import {
  4. barChartOptions,
  5. lineChartOptions
  6. } from '../../utils/chart.js';
  7. let moment = require('../../utils/moment.js');
  8. const app = getApp();
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. currentYear: moment().year(),
  15. ec: {
  16. lazyLoad: true,
  17. },
  18. list: [{
  19. label: '累计用水量',
  20. useVolume: 0
  21. }, {
  22. label: '平均用水量',
  23. useVolume: 0
  24. }, {
  25. label: '最高用水量',
  26. useVolume: 0
  27. }, {
  28. label: '最低用水量',
  29. useVolume: 0
  30. }],
  31. },
  32. // 初始化echart图表
  33. initChart({
  34. id,
  35. type,
  36. data,
  37. color
  38. }) {
  39. const {
  40. legend = [], xAxis = [], series = []
  41. } = data;
  42. const setOption = () => {
  43. if (type == 'bar') {
  44. this[id].setOption(barChartOptions({
  45. legend,
  46. xAxis,
  47. series
  48. }, color), true);
  49. } else {
  50. this[id].setOption(lineChartOptions({
  51. legend,
  52. xAxis,
  53. series
  54. }, color), true);
  55. }
  56. }
  57. if (this[id]) {
  58. setOption();
  59. return
  60. }
  61. this.selectComponent(`#${id}`).init((canvas, width, height) => {
  62. this[id] = echarts.init(canvas, null, {
  63. width: width,
  64. height: height
  65. });
  66. canvas.setChart(this[id]);
  67. setOption();
  68. return this[id];
  69. });
  70. },
  71. getThirtyDaysUseWater() {
  72. app.$http.get('/statistics/getThirtyDaysUseWater').then(({
  73. status,
  74. data = {}
  75. }) => {
  76. if (status == 0) {
  77. const {
  78. totalUseVolume = 0, averageUseVolume = 0, maxUseWater = {}, minUseWater = {}
  79. } = data;
  80. this.setData({
  81. 'list[0].useVolume': totalUseVolume,
  82. 'list[1].useVolume': averageUseVolume,
  83. 'list[2].useVolume': maxUseWater.useVolume || 0,
  84. 'list[3].useVolume': minUseWater.useVolume || 0,
  85. })
  86. }
  87. })
  88. },
  89. getWater() {
  90. wx.showLoading({
  91. title: '加载中',
  92. mask: true
  93. });
  94. app.$http.get('/statistics/getRealTimeUseWater', {
  95. period: 30
  96. }).then(({
  97. status,
  98. msg,
  99. data
  100. }) => {
  101. if (status === 0) {
  102. data = data.sort(function (a, b) {
  103. return a.date - b.date
  104. })
  105. const legend = ['用水量'],
  106. xAxis = [],
  107. series = [
  108. []
  109. ];
  110. if (data.length) {
  111. data.forEach(v => {
  112. let date = v.date.toString();
  113. date = date.substr(4, 2) + '.' + date.substr(6, 2);
  114. xAxis.push(date);
  115. series[0].push(v.useVolume);
  116. })
  117. const option = {
  118. id: 'monthChart',
  119. type: 'line',
  120. data: {
  121. legend,
  122. series,
  123. xAxis
  124. },
  125. color: ['#4386DE']
  126. }
  127. this.initChart(option);
  128. }
  129. }
  130. wx.hideLoading();
  131. }).catch(() => {
  132. wx.hideLoading();
  133. })
  134. },
  135. /**
  136. * 生命周期函数--监听页面加载
  137. */
  138. onLoad: function (options) {
  139. setTimeout(() => {
  140. this.getWater();
  141. this.getThirtyDaysUseWater();
  142. }, 300);
  143. },
  144. })