LineChart.vue 3.65 KB
<template>
  <div class="line-box">
    <div id="line-chart" class="line-chart"></div>
    <div class="rank-box">
      <div>
        <p :class="[ 'rank-title', rankFlag == 'first' ? 'rank-active' : '' ]" @click="handleRankChange('first')">前5{{ storeOrBrandTxt }}</p>
        <p :class="[ 'rank-title', rankFlag == 'last' ? 'rank-active' : '' ]" @click="handleRankChange('last')">后5{{ storeOrBrandTxt }}</p>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import getLineOptions from './lineOptions'
export default {
  name: 'LineChart',

  props: {
    apiName: {
      type: String,
      default: ''
    },
    storeOrBrandTxt: {
      type: String,
      default: '门店'
    }
  },

  data() {
    return {
      rankFlag: 'first',
      lineOptions: [],
      xAxisLabel: {
        monthList: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
      },
      echartsLine: '',
      dataList: [1, 2, 3, 10, 5, 2, 7, 8, 9, 10, 11, 12],
      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()

    this.drawLine()
  },

  methods: {
    dealxAxisLabel() {
      this.lineOptions.xAxis.data = this.xAxisLabel.monthList
    },

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

    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.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
      })
    },

    drawLine() {
      this.dealxAxisLabel()
      this.lineOptions.series[0].data = this.dataList

      this.echartsLine.setOption(this.lineOptions)

      console.log(this.echartsLine)
      this.autoMove()
    },

    handleRankChange(value) {
      this.rankFlag = value
      this.$emit('handleRankChange', value)
    }
  }
}
</script>

<style lang="scss" scoped>
p {
  margin: 0 0;
}
.line-box {
  color: #ffffff;
  // padding: 4px 8px;
  font-size: 0.75em;
  .line-chart {
    width: 100%;
    height: calc(100% - 3.1vh - 4.6vh);
  }
  .rank-box {
    box-sizing: border-box;
    display: flex;
    height: 4.6vh;
    padding: 0.48vh 0.87vh;
    border-top: 1px solid #373739;
    .rank-title {
      box-sizing: border-box;
      font-size: 0.6em;
      background: #353535;
      padding: 1px 5px;
      height: 1.83vh;
      line-height: 1.83vh;
      opacity: 0.7;
      cursor: pointer;
    }
    .rank-active {
      opacity: 1;
      color: #ffffff;
      background: #eb6001;
    }
  }
}
</style>