趋势图自适应 自定义指令

<template>
  <div v-resize="resize" id="generaltrend" :style="{height:height,width:width}"/>
</template>

<script>
  import {formatNumToTen, getChartHead} from '../../../../utils/methods'
// 引入 ECharts 主模块
// var echarts = require('echarts/lib/echarts')
// 引入柱状图
require('echarts/lib/chart/bar')
// 引入折线图
require('echarts/lib/chart/line')
// 引入提示框和标题组件
require('echarts/lib/component/tooltip')
// require('echarts/lib/component/title');
require('echarts/lib/component/toolbox')
require('echarts/lib/component/legend')
export default {
  name: 'HomeGrandSalesChart',
  // 自定义指令
  directives: {
    // 使用局部注册指令的方式
    resize: {
      // 指令的名称
      bind(el, binding) {
        // el为绑定的元素,binding为绑定给指令的对象
        let width = '',
          height = ''
        function isReize() {
          const style = document.defaultView.getComputedStyle(el)
          if (width !== style.width || height !== style.height) {
            // binding.value 代表调用 自定义属性绑定的方法
            // console.log(binding.value);
            binding.value() // 关键
          }
          width = style.width
          height = style.height
        }
        el.__vueSetInterval__ = setInterval(isReize, 300)
      },
      unbind(el) {
        clearInterval(el.__vueSetInterval__)
      }
    }
  },
  props: {
    dataList: {},
    storeNo: {
      type: String,
      default: ''
    },
    // yearStart: '',
    // yearEnd: '',
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    monthDayList: {
      type: Array,
      default: () => []
    },
    lunarMonthDate: {
      type: Array,
      default: () => []
    },
    lunarList: {
      type: Array,
      default: () => []
    },
  },
  data: function() {
    return {
      // cityComplementBar: null,
      serven_List: [],
      chart: null,
      timeDate: [],
      previousDaySales: [],
      lSales: [],
      headerTitle: ['本周', '上周', '去年'],
      weekOrMonth: '本周', yearOrMonth: '去年',
      label: '', titledata: '',
      weekData: [],
      grossRateData: [],
      salesData: [],
      grossData: [],
      retail_type_id: 0, dept_id: 1, business_circle_id: 0, management_type: 0,
      date_start_year: '', date_end_year: '', data_date: '',
      store_no: '',
      times: '日'
    }
  },
  watch: {
    storeNo() {
      this.$nextTick(() => {
        if (this.storeNo !== null && this.storeNo !== '') {
          this.store_no = this.storeNo
        }
      })
    },
    dataList() {
      this.$nextTick(() => {
        if (this.dataList) {
          this.titledata = this.dataList[this.dataList.length - 1]
          this.getData()
        }
      })
    },
    lunarList() {
      this.$nextTick(() => {
        this.getData()
      })
    },
  },
  created() {
  },
  mounted: function() {
    this.getData()
    this.initChart()
    // const that = this
    window.addEventListener('resize', () => {
      if (this.cityComplementBar) {
        this.cityComplementBar.resize()
      }
    })
  },
  activated: function() {
    if (this.cityComplementBar) {
      this.cityComplementBar.resize()
    }
  },
  beforeDestroy: function() {
    if (!this.cityComplementBar) {
      return
    }
    this.cityComplementBar.dispose()
    this.cityComplementBar = null
  },
  methods: {
    initChart: function() {
      const that = this
      var colors = ['#00D1CB', '#1070F5', '#FFB03F', '#525252']
      this.cityComplementBar = that.$echarts.init(document.getElementById('generaltrend'))
      const option1 = {

        color: colors,

        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
         style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:' + color + ';"></span>' + name + ':' + formatNumToTen(value) + '万元'
          //   }
          //   if (data) {
          //     htmlStr += '<br/>' + '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:' + A + ';"></span>' + a + ':' + formatNumToTen(data) + '万元'
          //   }
          //   if (percent) {
          //     htmlStr +=
          //       '<br/>' + '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:' + B + ';"></span>' + b + ':' + formatNumToTen(percent) + '%'
          //   }
          //   return htmlStr
          // }
          formatter: function(params, ticket, callback) {
            var htmlStr = ''
            for (var i = 0; i < params.length; i++) {
              var param = params[i]
              var xName = param.name// x轴的名称
              var seriesName = param.seriesName// 图例名称
              var value = param.value// y轴值
              var color = param.color// 图例颜色
              var uni = ''
              if (value === null || value === undefined) {
                value = ''
              }
              if (seriesName === '销售额') {
                uni = '元'
              }
              if (seriesName === '毛利额') {
                uni = '元'
              }
              if (seriesName === '毛利率') {
                uni = '%'
              }
              if (i === 0) {
                htmlStr += xName + that.times + '<br/>'// x轴的名称
              }
              if (value !== '-') {
                htmlStr += '<div>'
                // 为了保证和原来的效果一样,这里自己实现了一个点的效果
                htmlStr += '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:' + color + ';"></span>'

                // 圆点后面显示的文本
                htmlStr += seriesName + ':' + formatNumToTen(value) + uni

                htmlStr += '</div>'
              }
            }
            return htmlStr
          }
          // >>>>>>> fd8d3d31ee6997be7c5bd7c293daf77ed36aa57e
        },
        legend: {
          data: ['销售额', '毛利额', '毛利率'],
          top: '5px'
        },
        backgroundColor: '#fff',
        grid: {
          show: true,
          top: '20%', left: '8%', right: '12%', bottom: '10%',
          backgroundColor: '#fff'
        },
        toolbox: {
          // feature: {
          //   dataView: {
          //     show: true,
          //     readOnly: true,
          //     optionToContent: function(opt) {
          //       var axisData = opt.xAxis[0].data
          //       var series = opt.series
          //       var table = '<table style="width:40%;text-align:center"><tbody><tr>' +
          //                      '<td>时间</td>' +
          //                      '<td>' + series[0].name + '</td>' +
          //                      '<td>' + series[1].name + '</td>' +
          //                      '<td>' + series[2].name + '</td>' +
          //                      '</tr>'
          //       for (var i = 0, l = axisData.length; i < l; i++) {
          //         table += '<tr>' +
          //                      '<td>' + axisData[i] + '</td>' +
          //                      '<td>' + series[0].data[i] + '</td>' +
          //                      '<td>' + series[1].data[i] + '</td>' +
          //                      '<td>' + series[2].data[i] + '</td>' +
          //                      '</tr>'
          //       }
          //       table += '</tbody></table>'
          //       return table
          //     }
          //   },
          //   // restore: { show: true },
          //   saveAsImage: { show: true }
          // }
        },
        xAxis: [
          {
            type: 'category',
            name: this.time,
            nameTextStyle: {
              padding: [25, 0, 0, 15]
            },
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              interval: 0
              // rotate: 30
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: '#bbbbbb',
                type: 'dashed'
              }
            },
            boundaryGap: false,
            data: this.monthDayList
          },
          {
            type: 'category',
            nameTextStyle: {
              padding: [25, 0, 0, 15]
            },
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              interval: 0
              // rotate: 30
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: '#bbbbbb',
                type: 'dashed'
              }
            },
            boundaryGap: false,
            axisTick:{ show: false },// 是否显示坐标轴刻度
            axisLine:{ show: false },// 是否显示坐标轴轴线
            position: 'bottom',// 很重要,如果没有这个设置,默认第二个x轴就会在图表的顶部
            offset: 12,
            data: this.lunarMonthDate

          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '销售额(元)',
            // min: 0,
            position: 'left',
            splitLine: {
              show: true,
              lineStyle: {
                color: '#bbbbbb',
                type: 'dashed'
              }
            },
            axisLine: {
              lineStyle: {
                color: colors[3]
              }
            },
            axisLabel: {
              formatter: '{value}'
            }
          },
          {
            type: 'value',
            name: '毛利率(%)',
            /* min: 0,
            max: 100,*/
            position: 'right',
            offset: 80,
            axisLine: {
              lineStyle: {
                color: colors[3]
              }
            },
            splitLine: {
              show: false
            },
            axisLabel: {
              formatter: '{value}'
            }
          },
          {
            type: 'value',
            name: '毛利额(元)',
            // min: 0,
            position: 'right',
            axisLine: {
              lineStyle: {
                color: colors[3]
              }
            },
            splitLine: {
              show: false
            },
            axisLabel: {
              formatter: '{value}'
            }
          }
        ],
        series: [
          {
            name: '销售额',
            type: 'line',
            smooth: true,
            // areaStyle: {
            //   color: new that.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            //     offset: 0,
            //     color: colors[0]
            //   }, {
            //     offset: 1,
            //     color: 'rgb(255, 255, 255,0.2)'
            //   }])
            // },
            data: this.salesData
          },
          {
            name: '毛利额',
            type: 'line',
            yAxisIndex: 2,
            smooth: true,
            // areaStyle: {
            //   color: new that.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            //     offset: 0,
            //     color: colors[1]
            //   }, {
            //     offset: 1,
            //     color: 'rgb(255, 255, 255,0.2)'
            //   }])
            // },
            data: this.grossData
          },
          {
            name: '毛利率',
            type: 'line',
            yAxisIndex: 1,
            smooth: true,
            // areaStyle: {
            //   color: new that.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            //     offset: 0,
            //     color: colors[2]
            //   }, {
            //     offset: 1,
            //     color: 'rgb(255, 255, 255,0.2)'
            //   }])
            // },
            data: this.grossRateData
          },
          {
            type: 'line',
            smooth: true,
            areaStyle: {
              color: colors[0]
            },
            markArea:
              {
                label: {
                  show: true, position: 'top', distance: -15, color: colors[0]
                },
                data: this.titledata === '年' ? []:this.serven_List
              }
          },
        ]
      }
      this.cityComplementBar.setOption(option1)

      // tabs css设置(主页面全局获取)
      var a = document.querySelectorAll('.el-tabs__nav')
      for (var i = 0; i < a.length; i++) {
        if (i === 0) {
          a[i].style.padding = '0 0 0 35%'
        } else {
          a[i].style.padding = '0 0 0 40%'
        }
      }
      var b = document.querySelectorAll('.el-tabs__active-bar')
      for (var j = 0; j < b.length; j++) {
        b[j].style.display = 'none'
      }
    },
    getData() {
      this.titledata = this.GLOBAL.label
      if (this.titledata === '月') {
        this.times = ['日']
        this.time = ['日期']
        this.timeDate = this.monthData
        this.grossRateData = []
        this.salesData = []
        this.grossData = []
        this.weekData = []
        this.timeDate = []
        this.serven_List =  getChartHead('本月',this.lunarList)
        for(var da of this.monthDayList) {
          var grossRateData = ''
          var salesData = ''
          var grossData = ''
          for (const data of this.dataList) {
            if (data.data_date !== undefined) {
              if(da == Number(data.data_date.substring(8,10))){
                grossRateData = data.gross_rate
                salesData = data.sales
                grossData = data.gross
              }
            }
          }
          this.grossRateData.push(grossRateData)
          this.salesData.push(salesData)
          this.grossData.push(grossData)
        }
        this.timeDate = this.weekData
        this.initChart()
      }
      if (this.titledata === '年') {
        this.times = ['月']
        this.time = ['月份']
        this.timeDate = this.yearData
        this.grossRateData = []
        this.salesData = []
        this.grossData = []
        this.weekData = []
        this.timeDate = []
        for(var da of this.monthDayList) {
          var grossRateData = ''
          var salesData = ''
          var grossData = ''
          for (const data of this.dataList) {
            if (data.data_date !== undefined) {
              if(da == Number(data.date_year_month.substring(4,6))){
                grossRateData = data.gross_rate
                salesData = data.sales
                grossData = data.gross
              }
            }
          }
          this.grossRateData.push(grossRateData)
          this.salesData.push(salesData)
          this.grossData.push(grossData)
        }
        this.timeDate = this.weekData
        this.serven_List = []
        this.initChart()
      }
      if (this.titledata === '周') {
        this.times = ['日']
        this.time = ['日期']
        this.grossRateData = []
        this.salesData = []
        this.grossData = []
        this.weekData = []
        this.timeDate = []
        this.serven_List =  getChartHead('本月',this.lunarList)
        for(var da of this.monthDayList) {
          var grossRateData = ''
          var salesData = ''
          var grossData = ''
          for (const data of this.dataList) {
            if (data.data_date !== undefined) {
              if(da == Number(data.data_date.substring(8,10))){
                grossRateData = data.gross_rate
                salesData = data.sales
                grossData = data.gross
              }
            }
          }
          this.grossRateData.push(grossRateData)
          this.salesData.push(salesData)
          this.grossData.push(grossData)
        }
        this.timeDate = this.weekData
        this.initChart()
      }
    },
    setWeekDate(date) {
      var data_date = date.substring(8, 10)
      return data_date
    },
    setYearDate(date) {
      var data_yer = date.substring(4, 6)
      return data_yer
    },
    resize() {
      if (this.cityComplementBar) {
        this.cityComplementBar.resize()
      }
    }
  }
}
</script>

<style scoped>

</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值