LineChart.vue 4.78 KB
<template>
  <div class="line-box">
    <div id="line-chart" 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">
          <marquee class="line-item" :direction="directionTimer" :delay="delayTimer" :text="item.name"></marquee>
          <p class="line-item txt-orange">{{ item.num }}</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: {
    storeOrBrandTxt: {
      type: String,
      default: '门店'
    },
    dataList: {
      type: Array,
      default: () => []
    }
  },

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

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

  mounted() {
    this.lineDom = document.getElementById('line-chart')
    this.echartsLine = echarts.init(this.lineDom)
    this.chartBindEvent()
  },

  watch: {
    dataList() {
      this.drawLine()
    }
  },

  methods: {
    tooltipFormatter(obj) {
      var currentItem = this.dataList[obj[0].dataIndex]
      return `数量:${currentItem.num}<br>环比:${currentItem.hrate}%<br>同比:${currentItem.trate}%`
    },

    autoMove() {
      clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.moveAttr.dataIndex = this.moveAttr.dataIndex + 1
        if (this.moveAttr.dataIndex === this.dataList.length) {
          this.moveAttr.dataIndex = 0
        }

        this.chartAction()
        this.autoMove()
        this.getRankList(this.moveAttr.dataIndex, this.rankFlag + 'List')
      }, this.moveAttr.speed)
    },

    chartBindEvent() {
      // resize
      window.addEventListener('resize', () => {
        this.echartsLine.resize()
      })

      // 鼠标划入
      this.lineDom.addEventListener('mouseover', () => {
        clearInterval(this.timer)
        this.chartAction('downplay')
      })

      // 鼠标划出
      this.lineDom.addEventListener('mouseout', () => {
        this.moveAttr.dataIndex = -1
        this.autoMove()
      })
    },

    chartAction(type) {
      this.echartsLine.dispatchAction({
        type: type || 'showTip',
        seriesIndex: this.moveAttr.seriesIndex,
        dataIndex: this.moveAttr.dataIndex
      })
    },

    getRankList(idx, attrName = 'topList') {
      this.rankList = this.dataList[idx][attrName]
    },

    drawLine() {
      this.lineOptions.xAxis.data = this.dataList.map(item => item.statDate)

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

      this.echartsLine.setOption(this.lineOptions)

      this.getRankList(0)

      this.autoMove()
    },

    handleRankChange(value) {
      this.rankFlag = value
      this.getRankList(this.moveAttr.seriesIndex, 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;
      .rank-title {
        box-sizing: border-box;
        width: 100%;
        text-align: center;
        font-size: 0.6em;
        background: #353535;
        padding: 0.2vh 5px;
        height: 1.8vh;
        line-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: 25%;
        padding: 0 6px;
        border-left: 1px solid rgba(96, 98, 102, 0.4);
        .line-item {
          height: 1.8vh;
          font-size: 10px;
          padding-bottom: 0.6vh;
        }
      }
      .rank-line:top-child {
        border-width: 0;
      }
    }
  }
}
</style>