index.vue 5.5 KB
<template>
  <div
    class="board-detail-box"
    v-loading="loading"
  >
    <div class="board-header">
      <div class="business-type">
        归属业务:{{{
          2:'轻改',
          3:'权益',
          4:'延保'
        }[businessType]}}
      </div>
      <div class="search-component">
        <search-box
          @search="getBoardData"
          :hasConfig="false"
          hasStatisticalPeriod
        />
      </div>
    </div>
    <div class="board-content">
      <div
        class="line-chart"
        :id="pageData.pageId"
      ></div>
    </div>
  </div>
</template>

<script type='text/ecmascript-6'>
import SearchBox from '../SearchBox'
import * as echarts from 'echarts'
import { getBoardDetail } from '@api/earlyWarningManager'
import dayjs from 'dayjs'
import { tooltipConfig, xAxisConfig, dataZoomSliderConfig } from '../chartConfig'
import { KPI_DATA } from '../kpiConfig.js'
import NP from 'number-precision'
export default {
  components: {
    SearchBox
  },
  name: '',
  props: {
    pageData: {
      type: Object,
      default: () => { }
    },
    show: Boolean
  },
  data () {
    return {
      chart: null,
      searchTime: {},
      dialogShow: false,
      loading: false
    }
  },
  computed: {
    businessType () {
      return this.pageData.businessType
    },
  },
  watch: {
    businessType: {
      handler (newVal) {
        if (this.show) {
          if (newVal && this.initStatus) this.getData()
        } else this.initStatus = false
      }
    }
  },
  mounted () {

  },
  methods: {
    async getBoardData (params) {
      this.searchTime = params
      this.getData()
      this.initStatus = true
    },
    async getData () {
      try {
        this.loading = true
        let { searchTime: { startDate, endDate, dateType }, businessType } = this
        this.chart = echarts.init(document.getElementById(this.pageData.pageId))
        const { merchantId, code } = this.pageData,
          unit = KPI_DATA[code]?.valueUnit ? KPI_DATA[code].valueUnit : '',
          res = await getBoardDetail({
            businessType,
            merchantId,
            startDate,
            dateType,
            endDate,
            code
          })
        res.map(e => {
          let value = e.value || 0
          if (unit) e.value = NP.times(value, 100)
        })
        this.loading = false
        this.drawChart(res)
      } catch (error) {
        this.loading = false
      }
    },
    drawChart (list) {
      this.chart.clear()
      let { pageData: { name, code } } = this,
        _this = this,
        { allYear, dateType } = this.searchTime,
        timeList = [],
        data = [],
        unit = KPI_DATA[code]?.valueUnit ? KPI_DATA[code].valueUnit : ''
      list.map(e => {
        let { startDate, endDate } = e,
          axisLabel = dayjs(startDate).format("YYYY-MM-DD"),
          weekStr = `${dayjs(startDate).format('MM/DD')}~${dayjs(endDate).format('MM/DD')}`,
          value = e.value || 0
        if (dateType == 1) axisLabel = weekStr
        data.push([axisLabel, value])
        timeList.push(dateType == 1 ? weekStr : e.startDate)
      })

      let option = {
        tooltip: {
          ...tooltipConfig,
          formatter: params => {
            let [item] = params
            if (!item) return
            let { dataIndex, axisValue } = item,
              { value } = list[dataIndex],
              title = dayjs(axisValue).format(allYear ? 'YYYY年MM月' : 'MM-DD')
            if (dateType == 1) title = axisValue
            return `${title}<br>
            <div class="tooltips">
              <i class="circle"></i>${name}:${value?.toLocaleString() || 0}${unit}
            </div>`;
          }
        },
        title: {
          left: '20',
          text: `${name}趋势图`
        },
        grid: {
          left: 100,
          right: '60',
          bottom: '80'
        },
        xAxis: {
          ...xAxisConfig(_this),
          axisLabel: {
            showMaxLabel: true,
            formatter: function (value, index) {
              let str = ''
              str = allYear ? dayjs(value).format('YYYY年MM月') : dayjs(value).format('MM-DD')
              if (dateType == 1) str = value
              return str

            }
          }

        },
        yAxis: {
          type: 'value',
          boundaryGap: [0, '100%'],
          axisLine: {
            show: false
          }
        },
        series: [
          {
            type: 'line',
            symbol: 'none',
            data: data,
            lineStyle: {
              color: '#185EFE'
            }
          }
        ]
      };
      option.dataZoom = [
        dataZoomSliderConfig(_this, timeList)
      ]
      option && this.chart.setOption(option)
    }
  }
}
</script>

<style scoped lang='scss'>
.board-detail-box {
  margin: 0 20px;
  background: #fff;
  height: 100%;
  border-radius: 0 4px 4px 4px;
  .board-header {
    display: flex;
    align-items: center;
    .business-type {
      color: #333333;
      font-size: 14px;
      margin-left: 24px;
    }
    .search-component {
      margin-left: auto;
    }
  }
  .board-content {
    box-sizing: border-box;
    width: 100%;
    height: calc(100% - 64px);
  }
  .line-chart {
    width: 100%;
    height: 393px;
    ::v-deep.tooltips {
      padding-left: 20px;
      position: relative;
    }
    ::v-deep.circle {
      position: absolute;
      top: 7px;
      left: 6px;
      width: 6px;
      height: 6px;
      border: 1px solid #ffffff;
      background: #2a58ad;
      display: block;
      border-radius: 100%;
    }
  }
}
</style>