派生自 wuyushui/SewerAndRainNetwork

chenyabin
2021-04-12 23e134520785efe3bbce4e759776e360a7967b62
src/components/BaseNav/PublicBounced/GasComponents/Echarts.vue
@@ -1,10 +1,10 @@
<template>
  <div style="width:100%;height:2rem;margin-top: 0.3rem" ref="echarts"></div>
  <div style="width:5rem;height:2rem;margin-top: 0.3rem" ref="echarts"></div>
</template>
<script>
// import mapApi from '@/api/mapApi'
import mapApi from '@/api/mapApi'
import 'dayjs/locale/es'
import dayjs from 'dayjs'
@@ -12,90 +12,241 @@
  name: 'ECharts',
  data () {
    return {
      myChart: [],
      days: dayjs(new Date()).format('YYYYMMDDHHmmss'),
      data: []
      myChart: [],
      // x轴数据
      xAxis: [],
      seriesData: [],
      seriesName: []
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.upDateEcharts()
      this.initEchartsData()
      this.upDateEchartsData()
    })
  },
  methods: {
    // 初始化echarts
    upDateEcharts () {
      this.myChart = this.$echarts.init(this.$refs.echarts)
      // this.myChart.setOption(this.options)
      // setInterval(this.echartsData, 1000)
      function randomData () {
        now = new Date(+now + oneDay)
        value = value + Math.random() * 21 - 10
        return {
          name: now.toString(),
          value: [
            [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
            Math.round(value)
          ]
    // 初始化数据
    async initEchartsData () {
      const endTime = dayjs().add(1, 's')
      const data = {
        $tagCodeList: 'TJIP45.yyqAI90305,TJIP45.yyqAI90303,TJIP45.yyqFI_90310N,TJIP45.yyqAI90304,TJIP45.yyqAIR001',
        $startTime: this.dayjs,
        $endTime: endTime,
        $step: 15
      }
      const result = (await mapApi.DataItems(data)).data
      for (var i = 0; i < result.length; i++) {
        // console.log(result[i])
        this.xAxis.push(result[i].ReadTime)
        this.seriesData.push(result[i].TagValue)
        this.seriesName = result[i].UnionTagCode
      }
      this.upDateEchartsinit()
    },
    // 实时数据刷新
    upDateEchartsData () {
      // const interValHander =
      setInterval(async () => {
        const endTime = dayjs().add(1, 's')
        const data = {
          $tagCodeList: 'TJIP45.yyqAI90305,TJIP45.yyqAI90303,TJIP45.yyqFI_90310N,TJIP45.yyqAI90304,TJIP45.yyqAIR001',
          $startTime: this.dayjs,
          $endTime: endTime,
          $step: 15
        }
      }
      // this.data = []
      var now = +new Date(1997, 9, 3)
      var oneDay = 24 * 3600 * 1000
      var value = Math.random() * 1000
      for (var i = 0; i < 1000; i++) {
        this.data.push(randomData())
      }
        const result = (await mapApi.DataItems(data)).data
        for (var i = 0; i < result.length; i++) {
          // console.log(result[i])
          // this.xAxis.shift()
          this.xAxis.push(result[i].ReadTime)
          this.seriesData.push(result[i].TagValue)
          this.seriesName = result[i].UnionTagCode
        }
        this.upDateEchartsinit()
      }, 1500)
    },
    // 初始化echarts
    upDateEchartsinit () {
      this.myChart = this.$echarts.init(this.$refs.echarts)
      // 图表的配置项数据
      const xAxisData = this.xAxis
      const legendData = [this.seriesName, 'TJIP45.lscl2tb552AISA11201B', 'TJIP45.lscl2tb552AISA11202A', 'TJIP45.lscl2tbAIA-10505-1', 'TJIP45.lscl2tbAIA-10505-2']
      const seriesData = [
        {
          name: this.seriesName,
          type: 'line',
          stack: '总量',
          data: this.seriesData
        },
        {
          name: 'TJIP45.lscl2tb552AISA11201B',
          type: 'line',
          stack: '总量',
          data: [2.20, 1.82, 1.91, 2.34, 2.90, 3.30, 3.10]
        }
      ]
      const option = {
        title: {
          text: '动态数据 + 时间坐标轴'
          // text: '实时数据',
          target: 'blank',
          left: '5%',
          textStyle: {
            color: '#fff',
            fontSize: 15,
            bottom: 20
          }
        },
        tooltip: {
          trigger: 'axis',
          formatter: function (params) {
            params = params[0]
            var date = new Date(params.name)
            return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1]
          },
          axisPointer: {
            animation: false
            type: 'cross',
            label: {
              color: '#1a4245'
            }
          }
          // formatter: function (params) {
          //   // console.log(params)
          //   var UnionTagCode = params[0].seriesName + '<br />'
          //   for (var i = 0; i < params.length; i++) {
          //     var seriesName = params[i].seriesName
          //     // 值
          //     var value = params[i].value[1]
          //
          //     // var valueFliter = formatter(value)
          //     var valueFliter = value
          //
          //     var maker = params[i].marker
          //     var colo = ''
          //     switch (seriesName) {
          //       case 'TJIP45.lscl2tb552AI10710':
          //         colo = '#fff21c'
          //         break
          //       default:
          //         colo = 'red'
          //         break
          //     }
          //     maker = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:' + colo + ';"></span>'
          //     UnionTagCode += maker + seriesName + ':' + valueFliter + '<br />'
          //   }
          //   return UnionTagCode
          // }
        },
        grid: {
          top: '20%'
        },
        toolbox: {
          show: false,
          feature: {
            saveAsImage: {}
          }
        },
        dataZoom: [{
          type: 'inside',
          start: 0,
          end: 100
        }, {
          start: 0,
          end: 100,
          show: false,
          handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
          handleSize: '80%',
          handleStyle: {
            color: '#fff',
            shadowBlur: 3,
            shadowColor: 'rgba(0, 0, 0, 0.6)',
            shadowOffsetX: 2,
            shadowOffsetY: 2
          }
        }],
        legend: {
          data: legendData,
          textStyle: {
            color: '#ffffff',
            fontSize: 12
          }
        },
        xAxis: {
          type: 'time',
          type: 'category',
          // type: 'time',
          boundaryGap: false,
          axisLabel: {
            margin: 6,
            interval: 0,
            textStyle: {
              color: '#fff'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          },
          axisTick: {
            show: false
          }
          },
          axisLine: {
            lineStyle: {
              color: '#FFFFFF',
              width: 1
            }
          },
          data: xAxisData
        },
        yAxis: {
        yAxis: [{
          type: 'value',
          boundaryGap: [0, '100%'],
          name: 'yname',
          // max: function (value) {
          //   var max_val_list = []; //所有显示折线的标准值
          //   if (bzzList && bzzList.length > 0) {
          //     $.each(bzzList, function (index, item) {
          //       max_val_list.push(item.bzhui);
          //     });
          //   }
          //   max_val_list = max_val_list.sort(function (a, b) {
          //     return a - b
          //   }); //排序
          //   var ma = value.max > max_val_list[max_val_list.length - 1] ? value.max + 5 : max_val_list[max_val_list.length - 1];
          //   return parseInt(ma);
          // },
          axisLabel: {
            formatter: '{value}',
            textStyle: {
              color: '#fff'
            }
          },
          splitLine: {
            show: false
          }, // y轴 网格线不显示,
          axisLine: {
            lineStyle: {
              color: '#FFFFFF',
              width: 1
            }
          }
        },
        series: [{
          name: '模拟数据',
          type: 'line',
          showSymbol: false,
          hoverAnimation: false,
          data: this.data
        }]
        }, {
          type: 'value',
          name: 'dataUnit',
          axisLabel: {
            formatter: '{value}',
            textStyle: {
              color: '#fff'
            }
          },
          splitLine: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#FFFFFF',
              width: 1
            }
          }
        }],
        series: seriesData
      }
      setInterval(function () {
        for (var i = 0; i < 5; i++) {
          this.data.shift()
          this.data.push(randomData())
        }
        // this.myChart.setOption({
        //   series: [{
        //     data: this.data
        //   }]
        // })
      }, 1000)
      this.myChart.setOption(option)
    }
  }