The format of the return of the background, based on a common field grouping

In echars 13 according to the background data, 2019,2018,2017,2016 by year. Data points are arranged in an array according to the law of the month.

Proceed as follows:

third step

The fourth step, the final results to the

The final show

 

 console.log('能耗同期对比')
        console.log('第一步')
        console.log(this.mapEnerConsumComparData)
        var arr = this.mapEnerConsumComparData  //  得到的十三条数据
        var monthFlg = []
        var map = {}
        var dest = []
        for (var i = 0; i < arr.length; i++) {
          var ai = arr[i]
          if (!map[ai.monthFlg]) {
            dest.push({
              monthFlg: ai.monthFlg,    //根据月份分
              data: [ai]
            })
            map[ai.monthFlg] = ai
          } else {
            for (var j = 0; j < dest.length; j++) {
              var dj = dest[j]
              if (dj.monthFlg == ai.monthFlg) {
                dj.data.push(ai)
                break
              }
            }
          }
        }
        // console.log(dest)
        for (let i in dest) {
          monthFlg.push(`${dest[i].monthFlg}月`)
        }
        let serie = []
        for (let i in dest) {
          for (let j in  dest[i].data) {
//根据每条数据data  循环得到最终的数据
            var item = {
              show: dest[i].data[j].monthFlg,
              name: `${dest[i].data[j].yearFlg}年`,
              data: dest[i].data[j].totalConsumption
            }
            serie.push(item)
          }
        }
        // console.log('第三步')
        // console.log(serie)
        // console.log(monthFlg)
        var mapFun = {}
        var destFun = []
        for (var i = 0; i < serie.length; i++) {
          var ai = serie[i]
          if (!mapFun[ai.name]) {
            destFun.push({
              name: ai.name,
              type: 'bar',
              data: [ai.data]
            })
            mapFun[ai.name] = ai
          } else {
            for (var j = 0; j < destFun.length; j++) {
              var dj = destFun[j]
              if (dj.name == ai.name) {
                dj.data.push(ai.data)
                break
              }
            }
          }
        }
        // this.destFun = destFun
        // console.log('第四步')
        // console.log(destFun)
        // console.log(monthFlg)

 

Guess you like

Origin www.cnblogs.com/zhenga/p/11002764.html