BarLineChart.vue 2.54 KB
<!--
 * @Author: 前端 qianduan01@didihu.com.cn
 * @Date: 2022-10-25 10:12:26
 * @LastEditors: huyue
 * @LastEditTime: 2023-07-07 14:04:18
 * @FilePath: \dj-data-view\src\components\ViewTitle.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div>
    <div :id="domId" style="width: 100%;" :style="{ height }"></div>
  </div>
</template>

<script type="text/ecmascript-6">
import * as echarts from 'echarts'
import getBarLineOptions from './BarLineOptions'
export default {
  name: 'BarLineChart',
  props: {
    height: {
      type: String
    },
    domId: {
      type: String,
      default: 'bar-line'
    },
    dataList: {
      type: Array,
      default: () => []
    },
    tooltipFormatterObj: {
      type: Array,
      default: () => {
        return [
          {
            label: '数量',
            attrName: 'num'
          },
          {
            label: '环比',
            attrName: 'hrate',
            addSymbol: '%'
          },
          {
            label: '同比',
            attrName: 'trate',
            addSymbol: '%'
          }
        ]
      }
    }
  },
  data() {
    return {
      barLineOptions: [],
      moveAttr: {
        // speed: 900,
        seriesIndex: 0,
        dataIndex: 0
      },
      timer: null
    }
  },
  created() {
    this.barLineOptions = getBarLineOptions({ tooltipFormatter: this.tooltipFormatter })
  },
  mounted() {
    this.barLineDom = document.getElementById(this.domId)
    this[this.domId] = echarts.init(this.barLineDom)
  },
  methods: {
    tooltipFormatter(obj) {
      var _currentItem = this.dataList[obj[0].dataIndex]
      var _txt = ''
      this.tooltipFormatterObj.forEach(item => {
        if (_txt) {
          _txt = _txt + '<br>'
        }
        _txt = _txt + item.label + ':' + _currentItem[item.attrName] + (item.addSymbol || '')
      })
      return _txt
    },
    drawBarLine() {
      console.log(this.dataList)
      this.barLineOptions.xAxis.data = this.dataList.map(item => item.month)
      this.barLineOptions.series[0].data = this.dataList.map(item => item.data1)
      this.barLineOptions.series[1].data = this.dataList.map(item => item.data2)
      this.barLineOptions.series[2].data = this.dataList.map(item => item.data3)
      this[this.domId].setOption(this.barLineOptions)
    }
  }
}
</script>

<style scoped lang="scss">
</style>