echart.js 5.84 KB
import * as echarts from '../ec-canvas/echarts'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabIndex: {
      type: Number,
      value: 0
    },
    chartData: {
      type: Object,
      value: {}
    }
    // keys: {
    //   type: Array,
    //   value: ['year', 'sales']
    // },
    // tickCount: {
    //   type: Number,
    //   value: 4
    // }
    // color: {
    //   type: String,
    //   value: '#3975C6'
    // }
  },
  observers: {
    chartData: function (data) {
      console.log(data, '----------------')
      data.diagramList && data.diagramList.length && this.initData()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    ec: {
      // lazyLoad: false
      lazyLoad: true
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onInitChart() {
      let ecComponent = this.ecComponent || this.selectComponent('#mychart-dom-bar')
      ecComponent &&
        ecComponent.init((canvas, width, height, dpr) => {
          const chart = echarts.init(canvas, null, {
            width: width,
            height: height,
            devicePixelRatio: dpr
          })
          this.setOption(chart)
          return chart
        })
    },

    setOption(chart) {
      let { chartData, tabIndex } = this.data

      const abscissaDataList = [],
        orderMoneyTotalList = [],
        upgradeAveragePriceList = [],
        upgradeRateList = []

      chartData.diagramList.forEach(element => {
        abscissaDataList.push(element.abscissaData)
        // console.log(element.orderMoneyTotal, 'orderMoneyTotal')
        orderMoneyTotalList.push(tabIndex == 0 ? element.orderMoneyTotal : element.outputValue)
        upgradeAveragePriceList.push(tabIndex == 0 ? element.upgradeAveragePrice : element.avgPrice)
        upgradeRateList.push(element.upgradeRate || 0)
      })
      // 使用 Math.max 方法找到数组中的最大值
      const max = Math.max(...orderMoneyTotalList)
      // console.log(orderMoneyTotalList, 'orderMoneyTotalList')
      const colors = ['#3582EE', '#35C376', '#FF9D0A']
      var option = {
        color: colors,
        grid: {
          left: max > 100000 ? '25%' : '15%',
          right: '15%'
        },
        tooltip: {
          trigger: 'axis',
          formatter: function (params) {
            console.log(params, 'formatter')
            // debugger
            //  + '\n'
            var html = params[0].name
            html += '\n'
            for (var i = 0; i < params.length; i++) {
              let unit = i + 1 == params.length ? '' : '\n'
              let template = params[i].marker + params[i].seriesName + ':' + ' ' + params[i].value
              if (option.series[params[i].seriesIndex].name == '转化率') {
                template = template + '%' + unit
              } else {
                template = template + unit
              }
              html += template
            }
            // html +=  params[0].name
            console.log(html, 'html')
            return html
          }
        },
        // 滚动设置
        dataZoom: [
          {
            type: 'slider', //隐藏或显示(true)组件
            show: false,
            showDetail: false, //是否显示detail,即拖拽时候显示详细数值信息
            startValue: 0,
            endValue: 5,
            start: 50,
            end: 100,
            zoomLoxk: true // 是否锁定选择区域(或叫做数据窗口)的大小
          },
          {
            //没有下面这块的话,只能拖动滚动条,鼠标滚轮在区域内不能控制外部滚动条
            type: 'inside',
            zoomOnMouseWheel: false, //滚轮是否触发缩放
            moveOnMouseMove: true, //鼠标滚轮触发滚动
            moveOnMouseWheel: true
          }
        ],
        legend: {
          data: ['营业额', tabIndex ? '权益客单价' : '客单价', '转化率'],
          top: 'bottom'
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true
            },
            data: abscissaDataList || ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '营业额',
            position: 'left',
            alignTicks: true,
            offset: -5,
            axisLabel: {
              formatter: '{value} 元'
            }
          },
          {
            type: 'value',
            name: tabIndex ? '权益客单价' : '客单价',
            position: 'right',
            offset: 1000,
            axisLabel: {
              formatter: '{value} 元'
            }
          },
          {
            type: 'value',
            name: '转化率',
            position: 'right',
            alignTicks: true,
            axisLabel: {
              formatter: '{value} %'
            }
          }
        ],
        series: [
          {
            name: '营业额',
            type: 'bar',
            yAxisIndex: 0,
            data: orderMoneyTotalList
          },
          {
            name: tabIndex ? '权益客单价' : '客单价',
            type: 'bar',
            yAxisIndex: 0,
            data: upgradeAveragePriceList
          },
          {
            name: '转化率',
            type: 'line',
            yAxisIndex: 2,
            data: upgradeRateList
          }
        ]
      }
      chart.setOption(option)
    },
    //初始化数据
    initData(data) {
      // const xData = [],
      //   yData = []
      // const [x, y] = this.data.keys
      // data.forEach(item => {
      //   xData.push(item[x])
      //   yData.push(item[y])
      // })
      // this.setData({
      //   xData,
      //   yData
      // })
      this.onInitChart()
    }
  },
  ready() {
    // 获取组件
    this.ecComponent = this.selectComponent('#mychart-dom-bar')
  }
})