Newer
Older
baseResourceFront / src / views / alarm / listData.vue
zhangyingjie on 24 Mar 2021 9 KB 合并master分支
<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-date-picker
          v-model="timeRange"
          :picker-options="pickerOptions"
          :clearable="false"
          type="daterange"
          value-format="yyyy-MM-dd"
          align="right"
          size="small"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"/>
      </search-item>
    </search-area>
    <el-row :gutter="20" class="data-row">
      <el-col :span="12">
        <div ref="chart" :style="{height: '450px'}" class="chart-container"/>
      </el-col>
      <el-col :span="12" style="padding-right: 30px" >
        <div class="table-col">
          <!--查询结果Table显示-->
          <el-table v-loading="listLoading" v-if="list.length!==0" :data="bottomlist" class="table" size="small" max-height="400" border @selection-change="handleSelectionChange">
            <el-table-column :index="indexMethod" align="center" type="index" label="序号" width="55"/>
            <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
              <template slot-scope="scope">
                <span :class="column.class">{{ scope.row[column.value] }}</span>
              </template>
            </el-table-column>
            <el-table-column v-for="dept in deptList" :key="dept.deptId" :label="dept.deptName" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <span>{{ scope.row[dept.deptId] }}</span>
              </template>
            </el-table-column>
          </el-table>
          <!--分页-->
          <div class="pagination-container" style="margin-bottom: 80px" >
            <el-pagination
              v-show="total>listQuery.limit && list.length!==0"
              :current-page="listQuery.offset"
              :page-sizes="[15,20,30]"
              :page-size="listQuery.limit"
              :total="total"
              align="center"
              layout="total, sizes, prev, pager, next"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"/>
          </div>
        </div>

      </el-col>
    </el-row>
  </app-container>
</template>
<script>
import * as echarts from 'echarts'
import { getDataList } from '@/api/alarm'
export default {
  name: 'ListData',
  data() {
    return {
      deptShow: true,
      timeRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      listQuery: {
        startDate: '',
        endDate: '',
        offset: 1,
        limit: 20
      }, // 查询条件
      columns: [
        {
          text: '日期',
          value: 'date',
          align: 'center'
        }
      ], // 动态加载的表头
      list: [], // 列表数据
      bottomlist: [], // 列表数据
      deptList: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      dialogFormVisible: false, // 是否显示编辑框
      cartypelist: [], // 类型列表
      deptlist: [] // 类型列表
    }
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.startDate = val[0]
        this.listQuery.endDate = val[1]
      } else {
        this.listQuery.startDate = ''
        this.listQuery.endDate = ''
      }
    }
  },
  created() {
    const date = new Date()
    const year = date.getFullYear() // 年
    const month = (date.getMonth() + 1).toString().length === 1 ? '0' + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString() // 月
    const day = date.getDate().toString().length === 1 ? '0' + date.getDate().toString() : date.getDate().toString() // 日 // 日
    const start = new Date()
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
    const syear = start.getFullYear() // 年
    const smonth = (start.getMonth() + 1).toString().length === 1 ? '0' + (start.getMonth() + 1).toString() : (start.getMonth() + 1).toString() // 月
    const sday = start.getDate().toString().length === 1 ? '0' + start.getDate().toString() : start.getDate().toString() // 日
    this.timeRange = [syear + '-' + smonth + '-' + sday, year + '-' + month + '-' + day]
    this.fetchData()
  },
  methods: {
    search() {
      this.fetchData(false)
    },
    fetchData(isNowPage = true) {
      if (!isNowPage) {
        this.listQuery.offset = 1
      }
      this.listLoading = true
      getDataList(this.listQuery).then(response => {
        this.list = response.data.dateList
        this.deptList = response.data.deptList
        for (var i = 0; i < this.list.length; i++) {
          for (var j = 0; j < this.deptList.length; j++) {
            var str = this.deptList[j].deptId
            this.list[i][str] = 0
          }
          for (j = 0; j < this.list[i].depts.length; j++) {
            str = this.list[i].depts[j].deptId
            this.list[i][str] = this.list[i].depts[j].num
          }
        }
        var dept = []
        for (j = 0; j < this.deptList.length; j++) {
          dept.push(this.deptList[j].deptName)
        }
        var date = []
        for (j = 0; j < this.list.length; j++) {
          date.push(this.list[j].date)
        }
        var series = []
        for (j = 0; j < this.deptList.length; j++) {
          var nums = []
          for (i = 0; i < this.list.length; i++) {
            nums.push(Number(this.list[i][this.deptList[j].deptId]))
          }
          var item = {
            name: this.deptList[j].deptName,
            type: 'line',
            data: nums,
            markPoint: {
              data: [
                { type: 'max', name: '最大值' },
                { type: 'min', name: '最小值' }
              ]
            },
            markLine: {
              data: [
                { type: 'average', name: '平均值' }
              ]
            }
          }
          series.push(item)
        }
        if (series.length === 0) {
          nums = []
          for (i = 0; i < this.list.length; i++) {
            nums.push(0)
          }
          item = {
            name: '',
            type: 'line',
            data: nums
          }
          series.push(item)
        }
        console.log(series)
        this.chart = echarts.init(this.$refs.chart)
        this.chart.setOption({
          title: {
            text: '车辆违规统计图'
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: dept
          },
          toolbox: {
            show: true,
            feature: {
              dataZoom: {
                yAxisIndex: 'none'
              },
              // dataView: { readOnly: false },
              magicType: { type: ['line', 'bar'] },
              restore: {},
              saveAsImage: {}
            }
          },
          dataZoom: [
            {
              show: true,
              realtime: true,
              start: 0,
              end: 100
            },
            {
              type: 'inside',
              realtime: true,
              start: 0,
              end: 100
            }
          ],
          xAxis: {
            type: 'category',
            boundaryGap: false,
            // axisLine: { onZero: false },
            data: date
          },
          yAxis: {
            type: 'value',
            axisLabel: {
              formatter: '{value}'
            }
          },
          series: series
        })
        if (this.deptList.length === 0) {
          this.list = []
        }
        this.total = parseInt(response.data.dateList.length)
        if (this.total < this.listQuery.limit) {
          this.bottomlist = this.list
        } else {
          this.bottomlist = this.list.slice(0, this.listQuery.limit)
        }
        this.listLoading = false
      })
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      if (!isNowPage) {
        this.listQuery.offset = 1
      }
      if (this.total < this.listQuery.limit) {
        this.bottomlist = this.list
      } else {
        this.bottomlist = this.list.slice(0, this.listQuery.limit)
      }
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      console.log(this.list)
      if (this.total >= this.listQuery.limit * this.listQuery.offset) {
        this.bottomlist = this.list.slice(this.listQuery.limit * (this.listQuery.offset - 1), this.listQuery.limit * this.listQuery.offset)
      } else {
        this.bottomlist = this.list.slice(this.listQuery.limit * (this.listQuery.offset - 1))
      }
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

  .table{
    margin-bottom: 20px;
    margin-top: 40px;
  }
  .data-row{
    border-top: 12px solid #ebebeb;
  }
  .chart-container{
    padding-top: 20px;
    padding-left: 20px;
    padding-right: 10px;
  }
</style>