LineChart.vue 6.1 KB
<template>
  <div class="line-box">
    <div :id="domId" class="line-chart"></div>
    <div class="rank-box">
      <div class="rank-title-block">
        <p :class="['rank-title', rankFlag == 'top' ? 'rank-title-active' : '']" @click="handleRankChange('top')">前5{{ storeOrBrandTxt }}</p>
        <p :class="['rank-title', rankFlag == 'last' ? 'rank-title-active' : '']" @click="handleRankChange('last')">后5{{ storeOrBrandTxt }}</p>
      </div>
      <div class="rank-block">
        <div class="rank-line" v-for="(item, index) in rankList" :key="index">
          <div class="line-item">
            <marquee :direction="directionTimer" :delay="delayTimer" :text="item.label"></marquee>
          </div>
          <p class="line-item txt-orange">{{ item.value }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import datas from '@/utils/data'
import * as echarts from 'echarts'
import getLineOptions from './lineOptions'
export default {
  name: 'LineChart',
  props: {
    domId: {
      type: String,
      default: 'line-chart'
    },
    storeOrBrandTxt: {
      type: String,
      default: '品牌'
    },
    dataList: {
      type: Array,
      default: () => []
    },
    queryType: {
      type: Number,
      default: 0
    },
    tooltipFormatterObj: {
      type: Array,
      default: () => {
        return [
          {
            label: '数量',
            attrName: 'num'
          },
          {
            label: '环比',
            attrName: 'hrate',
            addSymbol: '%'
          },
          {
            label: '同比',
            attrName: 'trate',
            addSymbol: '%'
          }
        ]
      }
    }
  },

  data() {
    return {
      directionTimer: datas.DefaultData.directionTimer,
      delayTimer: datas.DefaultData.delayTimer,
      rankFlag: 'top',
      lineOptions: [],
      rankList: [],
      echartsLine: '',
      moveAttr: {
        // speed: 900,
        seriesIndex: 0,
        dataIndex: 0
      },
      timer: null
    }
  },

  created() {
    this.lineOptions = getLineOptions({ tooltipFormatter: this.tooltipFormatter })
  },

  mounted() {
    this.lineDom = document.getElementById(this.domId)
    this[this.domId] = echarts.init(this.lineDom)
    this.chartBindEvent()
  },

  methods: {
    resetRankList() {
      this.rankList = new Array(5).fill({ label: '- -', value: '- -' })
    },
    tooltipFormatter(obj) {
      var currentItem = this.dataList[obj[0].dataIndex]
      console.log(currentItem, 'currentItem')
      var txt = ''
      txt = currentItem.statDate
      this.tooltipFormatterObj.forEach(item => {
        if (txt) {
          txt = txt + '<br>'
        }
        txt = txt + item.label + ':' + currentItem[item.attrName] + (item.addSymbol || '')
      })
      return txt
    },

    chartBindEvent() {
      // resize
      window.addEventListener('resize', () => {
        this[this.domId].resize()
      })

      // 鼠标划入
      this[this.domId].on('mouseover', data => {
        this.moveAttr.dataIndex = data.dataIndex
        this.getRankList(this.moveAttr.dataIndex, this.rankFlag + 'List')
      })

      // 绑定点击事件
      this[this.domId].getZr().on('mouseover', params => {
        const pointInPixel = [params.offsetX, params.offsetY]
        if (this[this.domId].containPixel('grid', pointInPixel)) {
          var xIndex = this[this.domId].convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0]
          /* 事件处理代码书写位置 */
          this.moveAttr.dataIndex = xIndex
          this.getRankList(this.moveAttr.dataIndex, this.rankFlag + 'List')
        }
      })

      // 移除图表
      this[this.domId].on('globalout', () => {
        this.chartAction()
      })
    },

    chartAction(type) {
      this[this.domId].dispatchAction({
        type: type || 'showTip',
        seriesIndex: +this.moveAttr.seriesIndex,
        dataIndex: +this.moveAttr.dataIndex
      })
    },

    getRankList(idx, attrName = 'topList') {
      this.dataList[idx][attrName].length ? this.$set(this, 'rankList', this.dataList[idx][attrName]) : this.resetRankList()
    },

    drawLine() {
      this.lineOptions.xAxis.data = []

      this.dataList.map(e => {
        let month = new Date(e.statDate).getMonth()
        this.lineOptions.xAxis.data.push(this.queryType == 0 ? `${month == 0 ? '1月' : `${Number(month) + 1}月`}` : e.statDate.split('-').slice(1).join('-'))
      })

      this.lineOptions.series[0].data = this.dataList.map(item => item.num)

      this[this.domId].setOption(this.lineOptions)

      // 默认展示tooltip
      this.chartAction()

      this.getRankList(0)
    },

    handleRankChange(value) {
      this.rankFlag = value
      this.getRankList(this.moveAttr.dataIndex, this.rankFlag + 'List')
    }
  }
}
</script>

<style lang="scss" scoped>
p {
  margin: 0 0;
}
.line-box {
  color: #ffffff;
  // padding: 4px 8px;
  font-size: 0.75em;
  .txt-orange {
    color: #eb6001;
  }
  .line-chart {
    width: 100%;
    height: calc(100% - 3.1vh - 4.6vh);
  }
  .rank-box {
    box-sizing: border-box;
    display: flex;
    height: 4.6vh;
    padding: 0.5vh 0.87vh;
    border-top: 1px solid #373739;
    .rank-title-block {
      // flex: 0 0 55px;
      width: 55px;
      .rank-title {
        box-sizing: border-box;
        width: 100%;
        text-align: center;
        // font-size: 0.6em;
        font-size: 9px;
        background: #353535;
        padding: 0.2vh 5px;
        height: 1.8vh;
        opacity: 0.7;
        cursor: pointer;
      }
      .rank-title-active {
        opacity: 1;
        width: 100%;
        color: #ffffff;
        background: #eb6001;
      }
    }
    .rank-block {
      width: calc(100% - 55px);
      display: flex;
      .rank-line {
        box-sizing: border-box;
        width: 20%;
        padding: 0 6px;
        border-left: 1px solid rgba(96, 98, 102, 0.4);
        .line-item {
          height: 1.8vh;
          width: 100%;
          font-size: 10px;
          padding-bottom: 0.6vh;
        }
        .line-item:last-of-type{
          padding-bottom: 0;
        }
      }
      .rank-line:first-of-type{
        border-width: 0;
      }
    }
  }
}
</style>