123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- // pages/realtimewater.js
- import * as echarts from '../../components/ec-canvas/echarts.js';
- import {
- barChartOptions,
- lineChartOptions
- } from '../../utils/chart.js';
- let moment = require('../../utils/moment.js');
- const app = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- currentYear: moment().year(),
- ec: {
- lazyLoad: true,
- },
- list: [{
- label: '累计用水量',
- useVolume: 0
- }, {
- label: '平均用水量',
- useVolume: 0
- }, {
- label: '最高用水量',
- useVolume: 0
- }, {
- label: '最低用水量',
- useVolume: 0
- }],
- },
- // 初始化echart图表
- initChart({
- id,
- type,
- data,
- color
- }) {
- const {
- legend = [], xAxis = [], series = []
- } = data;
- const setOption = () => {
- if (type == 'bar') {
- this[id].setOption(barChartOptions({
- legend,
- xAxis,
- series
- }, color), true);
- } else {
- this[id].setOption(lineChartOptions({
- legend,
- xAxis,
- series
- }, color), true);
- }
- }
- if (this[id]) {
- setOption();
- return
- }
- this.selectComponent(`#${id}`).init((canvas, width, height) => {
- this[id] = echarts.init(canvas, null, {
- width: width,
- height: height
- });
- canvas.setChart(this[id]);
- setOption();
- return this[id];
- });
- },
- getThirtyDaysUseWater() {
- app.$http.get('/statistics/getThirtyDaysUseWater').then(({
- status,
- data = {}
- }) => {
- if (status == 0) {
- const {
- totalUseVolume = 0, averageUseVolume = 0, maxUseWater = {}, minUseWater = {}
- } = data;
- this.setData({
- 'list[0].useVolume': totalUseVolume,
- 'list[1].useVolume': averageUseVolume,
- 'list[2].useVolume': maxUseWater.useVolume || 0,
- 'list[3].useVolume': minUseWater.useVolume || 0,
- })
- }
- })
- },
- getWater() {
- wx.showLoading({
- title: '加载中',
- mask: true
- });
- app.$http.get('/statistics/getRealTimeUseWater', {
- period: 30
- }).then(({
- status,
- msg,
- data
- }) => {
- if (status === 0) {
- data = data.sort(function (a, b) {
- return a.date - b.date
- })
- const legend = ['用水量'],
- xAxis = [],
- series = [
- []
- ];
- if (data.length) {
- data.forEach(v => {
- let date = v.date.toString();
- date = date.substr(4, 2) + '.' + date.substr(6, 2);
- xAxis.push(date);
- series[0].push(v.useVolume);
- })
- const option = {
- id: 'monthChart',
- type: 'line',
- data: {
- legend,
- series,
- xAxis
- },
- color: ['#4386DE']
- }
- this.initChart(option);
- }
- }
- wx.hideLoading();
- }).catch(() => {
- wx.hideLoading();
- })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- setTimeout(() => {
- this.getWater();
- this.getThirtyDaysUseWater();
- }, 300);
- },
- })
|