pie.vue 6.48 KB
<template>
  <div :id="echartId" class="chart-container"></div>
</template>

<script>
import * as echarts from 'echarts'
import { introConpute } from '../../api/modules/rules'
import util from '../../util/util'
export default {
  name: '',
  data() {
    return {
      optinTitle: {}
    }
  },

  props: {
    echartId: {
      type: String,
      default: ''
    }
  },
  mounted() {
    this.getParams()
  },
  methods: {
    getOptions(res) {
      var chartDom = document.getElementById(this.echartId)
      var myChart = echarts.init(chartDom)
      var option = {
        tooltip: {
          trigger: 'item',
          formatter:(params)=>{
            let str = params.data["name"]+':'+this.priceFormat(params.data['value'])
            return str
          }
        },
        series: [
          {
            type: 'pie',
            radius: '50%',
            center: ['40%', '50%'],
            data: [
              {
                value: res.afterSaleUnitPrice || 0,
                name: `售后代运营新增产值`,
                color: '#FACD67',
                label: {
                  show: true,
                  rich: {
                    color: {
                      color: '#666666'
                    }
                  },
                  formatter: () => {
                    let temp = this.priceFormat(res.afterSaleUnitPrice)
                    if(!temp){
                      return 
                    }
                    return `售后代运营新增产值:约${temp || 0}/年\n\n {color|累计3年可触达用户-渗透率:${res.afterSalePermeabilityString}}`
                  }
                }
              },
              {
                value: res.onSaleUnitPrice || 0,
                name: `售中新品新增产值`,
                color: '#64C0A2',
                label: {
                  show: true,
                  rich: {
                    color: {
                      color: '#666666'
                    }
                  },
                  formatter: () => {
                    let temp = this.priceFormat(res.onSaleUnitPrice)
                    if(!temp){
                       return 
                    }
                    return `售中新品新增产值:约${temp || 0}/年\n\n {color|渗透率:${res.onSalePermeabilityString}}`
                  }
                }
              },
              {
                value: res.preSaleUnitPrice || 0,
                name: `售前权益新增产值`,
                color: '#5850FF',
                label: {
                  show: true,
                  rich: {
                    color: {
                      color: '#666666'
                    }
                  },
                  formatter: () => {
                    let temp = this.priceFormat(res.preSaleUnitPrice)
                    if(!temp){
                      return 
                    }
                    return `售前权益新增产值:约${temp || 0}/年\n\n {color|渗透率:${res.preSalePermeabilityString}}`
                  }
                }
              }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
      option && myChart.setOption(option, true)
    },
    getParams(newval) {
      introConpute({}).then(res => {
        res.preSalePermeabilityString = `${res.preSalePermeability * 100}%`
        res.onSalePermeabilityString = `${res.onSalePermeability * 100}%`
        res.afterSalePermeabilityString = `${res.afterSalePermeability * 100}%`
        this.computeValue = res
        if (newval) {
          res.preSaleUnitPrice = newval.preSaleUnitPrice
          res.onSaleUnitPrice = newval.onSaleUnitPrice
          res.afterSaleUnitPrice = newval.afterSaleUnitPrice
        }
        // this.getOptions(res)
        this.computeProduct(0,1)
      })
    },
    computeProduct(num1, num2) {
      let { preSalePermeability, preSaleUnitPrice, onSalePermeability, onSaleUnitPrice, afterSalePermeability, afterSaleUnitPrice } = this.computeValue
      // 售前产值
      let beforeSale = num1 * 12 * preSalePermeability * preSaleUnitPrice
      let inSale = num1 * 12 * onSalePermeability * onSaleUnitPrice
      let afterSale = num1 * 12 * 3 * afterSalePermeability * afterSaleUnitPrice
      let totalValue = beforeSale + inSale + afterSale
      let singleStoreValue = totalValue / num2
      let totalProduct = this.priceFormat(totalValue)
      let singleProduct = this.priceFormat(singleStoreValue)
      let options = {
        preSaleUnitPrice: beforeSale,
        onSaleUnitPrice: inSale,
        afterSaleUnitPrice: afterSale,
        preSalePermeabilityString: this.computeValue.preSalePermeabilityString,
        onSalePermeabilityString: this.computeValue.onSalePermeabilityString,
        afterSalePermeabilityString: this.computeValue.afterSalePermeabilityString
      }
      this.getOptions(options)
      return { totalProduct, singleProduct }
    },
    // 价格格式化
    priceFormat(num, point = 2) {
      let numStr = num.toString().split('.')[0]
      if (numStr.length < 6) {
        return util.decimal(num,2)
      } else if (numStr >= 6) {
        let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + point)
        return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万'
      }
      return num
      // return util.priceFormat(num)
      // num = util.decimal(num,2)
      // 将数字转换为字符串,然后通过split方法用.分隔,取到第0个
      // let numStr = num.toString().split('.')[0]
      // if (numStr.length < 5) {
      //   // 判断数字有多长,如果小于6,,表示10万以内的数字,让其直接显示
      //   return num
      // } else if (numStr.length >= 5) {
      //   // 如果数字大于6位,小于8位,让其数字后面加单位万
      //   let decimal = numStr.substring(numStr.length - 3, numStr.length - 3 + point)
      //   // 由千位,百位组成的一个数字
      //   return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万'
      // }
      //  else if (numStr.length > 6) {
      //   // 如果数字大于8位,让其数字后面加单位亿
      //   let decimal = numStr.substring(numStr.length - 6, numStr.length - 6 + point)
      //   return parseFloat(parseInt(num / 100000000) + '.' + decimal) + '亿'
      // }
    }
  }
}
</script>
<style lang="scss" scoped>
.chart-container {
  height: 152px;
  // overflow: hidden;
}
</style>