barChart.vue 4.82 KB
<!--
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2022-10-25 10:43:46
 * @LastEditors: 前端 qianduan01@didihu.com.cn
 * @LastEditTime: 2022-10-31 17:25:12
 * @FilePath: \dj-data-view\src\components\barChart.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class='line-box'>
    <div :id='domId' class='line-chart' :style='{ height: boxHeight }'></div>
  </div>
</template>
<script>
const ValueAnalysis = [
  { name: 'gendev', label: '一般发展', order: 6 },
  { name: 'genrecall', label: '一般召回', order: 7 },
  { name: 'gensave', label: '一般挽留', order: 8 },
  { name: 'genval', label: '一般价值', order: 5 },
  { name: 'majordev', label: '重要发展', order: 2 },
  { name: 'majorrecall', label: '重要召回', order: 3 },
  { name: 'majorsave', label: '重要挽留', order: 4 },
  { name: 'majorval', label: '重要价值', order: 1 }
]
import datas from '@/utils/data'
import * as echarts from 'echarts'
import utils from '../utils/util'
import { getBarOptions, getBarReverseOptions } from './options'

export default {
  name: 'barChart',
  props: {
    domId: {
      type: String,
      default: 'line-chart'
    },
    labelName: {
      type: String,
      default: 'goodsName'
    },
    rotate: {
      type: Number,
      default: 0
    },
    revese: {
      type: Boolean,
      default: false
    },
    boxHeight: {
      type: String,
      default: '13.83vh'
    },
    storeOrBrandTxt: {
      type: String,
      default: '品牌'
    },
    analysisList: {
      type: [Array, Object],
      default: () => []
    },
    // 0-月表;1-日表
    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: [],
      areaList2: this.areaList,
      echartsLine: '',
      moveAttr: {
        // speed: 900,
        seriesIndex: 0,
        dataIndex: 0
      },
      timer: null,
      currentData: {
        dataIndex: 0
      }
    }
  },
  //   created() {
  //     const { rotate, analysisList } = this
  //     const obj = {
  //       rotate,
  //       analysisList
  //     }
  //     this.lineOptions = this.revese ? getBarReverseOptions(obj) : getBarOptions(obj)
  //   },
  watch: {
    analysisList: {
      handler() {
        const { rotate, analysisList,labelName } = this
        const obj = {
          rotate,
          analysisList,
          ValueAnalysis,
          labelName
        }
        this.lineOptions = this.revese ? getBarReverseOptions(obj) : getBarOptions(obj)
        this.chartBindEvent()
      },
      deep: true
    }
  },
  mounted() {
    this.lineDom = document.getElementById(this.domId)
    this[this.domId] = echarts.init(this.lineDom)
    // this.chartBindEvent()
  },
  methods: {
    chartBindEvent() {
      this[this.domId].setOption(this.lineOptions)
      if (this.revese === false) return
      this.chartAction()
      this[this.domId].getZr().on(
        'mousemove',
        utils.debounce(params => {
          clearInterval(this.timer)
          this.timer = null
          const pointInPixel = [params.offsetX, params.offsetY]
          const pointInGrid = this[this.domId].convertFromPixel({ seriesIndex: 0 }, pointInPixel)
          const yIndex = pointInGrid[1]
          this.currentData = {
            dataIndex: yIndex
          }
          this.chartAction(yIndex)
        }, 500)
      )
      this[this.domId].getZr().on(
        'mouseout',
        utils.debounce(() => {
          if (!this.timer) {
            this.timer = setInterval(this.setInter, 5000)
          }
        }, 500)
      )
      this.timer = setInterval(this.setInter, 5000)
    },
    setInter() {
      if (this.currentData.dataIndex == 0) {
        this.currentData.dataIndex = 7
      } else {
        this.currentData.dataIndex--
      }
      this.chartAction(this.currentData.dataIndex)
    },
    chartAction(index = 7) {
      this[this.domId].dispatchAction({
        type: 'showTip',
        seriesIndex: 0,
        dataIndex: index
      })
      this.$bus.$emit('drawChart', index)
    }
  }
}
</script>
<style lang='scss' scoped>
.line-box {
  height: 100%;
  color: #ffffff;

  .txt-orange {
    color: #eb6001;
  }

  .line-chart {
    width: 100%;
  }
}
</style>