wateranalysis.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // pages/wateranalysis/wateranalysis.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. waterInfo: {},
  16. ec: {
  17. lazyLoad: true,
  18. },
  19. },
  20. // 初始化echart图表
  21. initChart({
  22. id,
  23. type,
  24. data,
  25. color,
  26. configs
  27. }) {
  28. const {
  29. legend = [], xAxis = [], series = []
  30. } = data;
  31. const setOption = () => {
  32. if (type == 'bar') {
  33. this[id].setOption(barChartOptions({
  34. legend,
  35. xAxis,
  36. series
  37. }, color, configs), true);
  38. } else {
  39. this[id].setOption(lineChartOptions({
  40. legend,
  41. xAxis,
  42. series
  43. }, color), true);
  44. }
  45. }
  46. if (this[id]) {
  47. setOption();
  48. return
  49. }
  50. this.selectComponent(`#${id}`).init((canvas, width, height) => {
  51. this[id] = echarts.init(canvas, null, {
  52. width: width,
  53. height: height
  54. });
  55. canvas.setChart(this[id]);
  56. setOption();
  57. return this[id];
  58. });
  59. },
  60. /**
  61. * 生命周期函数--监听页面加载
  62. */
  63. getWaterAnalysis() {
  64. wx.showLoading({
  65. title: '加载中',
  66. mask: true
  67. });
  68. app.$http.get('/statistics/getUseWaterAnalyze').then(({
  69. status,
  70. msg,
  71. data: {
  72. list = [],
  73. averageUseVolume = 0,
  74. maxUseWater = 0,
  75. minUseWater = 0,
  76. totalUseVolume = 0
  77. }
  78. }) => {
  79. if (status === 0) {
  80. this.setData({
  81. waterInfo: {
  82. list,
  83. averageUseVolume,
  84. maxUseWater,
  85. minUseWater,
  86. totalUseVolume
  87. }
  88. })
  89. list = list.sort(function(a, b) {
  90. return a.date - b.date
  91. })
  92. const legend = ['用水量'],
  93. xAxis = [],
  94. series = [
  95. []
  96. ];
  97. if (list.length) {
  98. this.setData({
  99. list: list
  100. })
  101. list.forEach(v => {
  102. let date = v.date.toString();
  103. date = date.substr(4, 2);
  104. xAxis.push(date);
  105. series[0].push(v.useVolume)
  106. })
  107. const option = {
  108. id: 'weekChart',
  109. type: 'bar',
  110. data: {
  111. legend,
  112. series,
  113. xAxis
  114. },
  115. color: ['#FFF'],
  116. configs: {
  117. barWidth: 16
  118. }
  119. }
  120. this.initChart(option);
  121. }
  122. }
  123. wx.hideLoading();
  124. }).catch(() => {
  125. wx.hideLoading();
  126. })
  127. },
  128. onLoad: function(options) {
  129. this.getWaterAnalysis();
  130. },
  131. })