bar.vue 5.54 KB
<template>
  <div :id="echartId" :style="'width:'+width+'px'" class="chart-container"></div>
</template>

<script>
import { couponsTotalByDay } from '../../api/modules/rules'
import { converterApi } from '../../api/modules/converter'
import utils from '../../util/util'
import * as echarts from 'echarts'
const base_url = 'https://img01.glsx.com.cn/weapp/resource/saas/scrm/'
export default {
  name: '',
  data() {
    return {
      xAxisData: [],
      getTotal: [],
      usedTotal: [],
      upgradeTotal: [],
      base_url
    }
  },

  props: {
    echartId: {
      type: String,
      default: ''
    },
    width: {
      type: Number,
      default: 400
    }
  },

  mounted() {
    this.initTIme()
  },
  methods: {
    getOptions(len) {
      var dataZoom = [
        //给x轴设置滚动条
        {
          start: 0, //默认为0
          end: 100 - 1500 / 31, //默认为100
          type: 'slider',
          show: true,
          xAxisIndex: [0],
          handleSize: 0, //滑动条的 左右2个滑动条的大小
          height: 1, //组件高度
          left: '10%', //左边的距离
          right: '10%', //右边的距离
          bottom: 26, //右边的距离
          borderColor: '#fff',
          fillerColor: '#909399',
          borderRadius: 5,
          backgroundColor: '#f5f5f5', //两边未选中的滑动条区域的颜色
          showDataShadow: false, //是否显示数据阴影 默认auto
          showDetail: false, //即拖拽时候是否显示详细数值信息 默认true
          realtime: true, //是否实时更新
          filterMode: 'filter',
          zoomLock: false
        },
        //下面这个属性是里面拖到
        {
          type: 'inside',
          show: false,
          xAxisIndex: [0],
          start: 1, //默认为1
          end: 100 - 1500 / 31 //默认为100
        }
      ]
      var chartDom = document.getElementById(this.echartId)
      var myChart = echarts.init(chartDom)
      var labelOption = {
        show: false
      }
      var option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          height: '60%',
        },
        legend: {
          data: ['领券数', '核销数', '精品升级订单']
          // itemWidth: 12,
          // itemHeight: 12,
          // data: [
          //   {
          //     icon: `image://${base_url}echart_icon_1.png`,
          //     name: '领券数'
          //   },
          //   {
          //     icon: `image://${base_url}echart_icon_2.png`,
          //     name: '核销数'
          //   },
          //   {
          //     icon: `image://${base_url}echart_icon_3.png`,
          //     name: '精品升级订单'
          //   }
          // ]
        },
        xAxis: [
          {
            // name: '日期',
            type: 'category',
            axisTick: { show: false },
            data: this.xAxisData.reverse(),
            axisLabel: {
              interval: 0
            },
            inverse:true   
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '数量'
          }
        ],
        series: [
          {
            name: '领券数',
            type: 'bar',
            barGap: 0,
            label: labelOption,
            barWidth: 6,
            emphasis: {
              focus: 'series'
            },
            itemStyle: {
              normal: {
                color: '#B3B3FF'
              }
            },
            data: this.getTotal.reverse()
          },
          {
            name: '核销数',
            type: 'bar',
            label: labelOption,
            emphasis: {
              focus: 'series'
            },
            barWidth: 6,
            itemStyle: {
              normal: {
                color: '#7C7CFF'
              }
            },
            data: this.usedTotal.reverse()
          },
          {
            name: '精品升级订单',
            type: 'bar',
            label: labelOption,
            barWidth: 6,
            emphasis: {
              focus: 'series'
            },
            itemStyle: {
              normal: {
                color: '#5850FF'
              }
            },
            data: this.upgradeTotal.reverse()
          }
        ]
      }
      if (len > 6) {
        option.dataZoom = dataZoom
      }
      option && myChart.setOption(option)
    },
    // 初始化时间
    initTIme() {
      // let openInfo = JSON.parse(localStorage.getItem('openInfo'))
      // let startDate = utils.dateFormat(openInfo.openDate)
      let startDate = utils.getDateStr(-15)
      let endDate = utils.dateFormat(new Date())
      this.couponsChartData(startDate, endDate)
    },
    couponsChartData(startDate, endDate) {
      this.xAxisData = []
      this.getTotal = []
      this.usedTotal = []
      this.upgradeTotal = []
      let method = couponsTotalByDay()
      let para = {
        routerMethod: method,
        startDate,
        endDate
      }
      converterApi(para).then(res => {
        let arr = res || []
        if (arr.length) {
          arr.map(item => {
            item.dataDate = utils.dateFormat(item.dataDate)
            this.xAxisData.push(item.dataDate)
            this.getTotal.push(item.getTotal)
            this.usedTotal.push(item.usedTotal)
            this.upgradeTotal.push(item.upgradeTotal)
            return item
          })
        }
        this.getOptions(arr.length)
      })
    }
  }
}
</script>
<style>
.chart-container {
  width: 100%;
  height: 300px;
  overflow: hidden;
}
</style>