派生自 wuyushui/SewerAndRainNetwork

chenzeping
2021-04-12 2ccfcb5a17c31ade0b532d7cc77c9a65aed1343c
echarts实时数据
1个文件已修改
216 ■■■■ 已修改文件
src/components/BaseNav/PublicBounced/GasComponents/Echarts.vue 216 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/components/BaseNav/PublicBounced/GasComponents/Echarts.vue
@@ -4,7 +4,7 @@
<script>
// import mapApi from '@/api/mapApi'
import mapApi from '@/api/mapApi'
import 'dayjs/locale/es'
import dayjs from 'dayjs'
@@ -12,90 +12,188 @@
  name: 'ECharts',
  data () {
    return {
      myChart: [],
      days: dayjs(new Date()).format('YYYYMMDDHHmmss'),
      data: []
      myChart: [],
      // x轴数据
      xAxis: [],
      seriesData: [],
      seriesName: []
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.upDateEcharts()
      this.upDateEchartsData()
    })
  },
  methods: {
    async upDateEchartsData () {
      const addDays = dayjs().add(1, 's')
      const data = {
        $tagCodeList: 'TJIP45.yyqAI90305,TJIP45.yyqAI90303,TJIP45.yyqFI_90310N,TJIP45.yyqAI90304,TJIP45.yyqAIR001',
        $startTime: this.dayjs,
        $endTime: addDays,
        $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
        // let num = 0
        // if (result[i].UnionTagCode === 'TJIP45.lscl2tb552AI10710') {
        //   num++
        //   console.log(num)
        // }
      }
      this.upDateEchartsinit()
    },
    // 初始化echarts
    upDateEcharts () {
    upDateEchartsinit () {
      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)
          ]
      // 图表的配置项数据
      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',
          data: this.seriesData,
          symbolSize: 8,
          smooth: true,
          silent: false,
          barWidth: '50%',
          animation: true
        },
        {
          name: 'TJIP45.lscl2tb552AISA11201B',
          type: 'line',
          stack: '总量',
          smooth: true,
          symbolSize: 8,
          data: [2.20, 1.82, 1.91, 2.34, 2.90, 3.30, 3.10]
        }
      }
      // 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 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
          trigger: 'axis'
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          show: false,
          feature: {
            saveAsImage: {}
          }
        },
        dataZoom: [
          {
            type: 'inside'
            // start: 50,
            // end: 100,
            // filterMode: 'empty'
          }
        ],
        legend: {
          data: legendData,
          textStyle: {
            color: '#ffffff',
            fontSize: 12
          }
        },
        xAxis: {
          type: 'time',
          type: 'category',
          boundaryGap: false,
          axisLabel: {
            rotate: 20,
            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)
    }
  }