ResidenceTime.nvue 3.24 KB
<template>
  <CardNvue :title="t('PreMallIndex.StayTimeDistribution')">
    <template #content>
      <ChartsNvue ref="chartsRef" />
    </template>
  </CardNvue>

</template>

<script setup>
  import {
    getResidenceTimeApi
  } from '@/api'
  import CardNvue from './Card.nvue'
  import ChartsNvue from '@/components/Charts.nvue'
  import {
    onMounted,
    ref
  } from 'vue'

  // #ifdef APP || H5
  import * as echarts from 'echarts';
  // #endif
  // #ifdef MP-WEIXIN
  const echarts = require('../../../uni_modules/lime-echart/static/echarts.min');
  // #endif
  import { t } from '@/plugins/index.js'


  const chartsRef = ref(null)


  onMounted(() => {
    // getTrendChartData()
  })
  const options = ref({})
  const initData = (params) => {
    options.value = params
    getTrendChartData(params)
  }
  defineExpose({
    initData
  })

  const getTrendChartData = async () => {
    try {
      // 参数设置位置:
      const params = {
        orgId: options.value.storeId,
        startDate: options.value.startDate,
        endDate: options.value.endDate,
        type: 'bar'
      }
      const {
        data
      } = await getResidenceTimeApi(params)
      const {
        ResidenceTimeReport
      } = data.body

      renderCharts(ResidenceTimeReport)
    } catch (e) {
      console.log(e);
    }
  }

  const renderCharts = (val = {
    series: [{
      data: [],
      name: ''
    }],
    title: '',
    xaxis: {
      data: []
    }
  }) => {

    const option = {
      grid: {
        bottom: '0%',
        left: '0',
        right: '2%',
        top: '10%',
        containLabel: true
      },
      tooltip: {
        trigger: 'axis',
        triggerOn:'click',
        confine: true,
        // #ifdef APP-NVUE
        formatter:`function(params){
          const item = params[0]
          return item.seriesName + '\\n' + item.name + ':' + item.value + '%'
        }`,
        // #endif
        // #ifndef APP-NVUE
        formatter: function(params) {
          return `${params[0].seriesName}\n${params[0].name}:${params[0].value}% `
        },
        // #endif
      },
      xAxis: {
        type: 'category',
        boundaryGap: true,
        data: val.xaxis.data,
        axisTick: {
          show: false
        },
        axisLine: {
          lineStyle: {
            color: '#E1E4EA'
          }
        },
        axisLabel: {
          color: '#202328'
        }
      },
      yAxis: {
        type: 'value',
        splitLine: {
          show: true,
          lineStyle: {
            color: "#E8E8E8",
            type: "dotted" // dotted 虚线
          }
        },
        // 最小值1
        axisLabel: {
          color: '#202328',

          // #ifndef APP-NVUE
          formatter: `function(value){
            return value + '%'
          }`,
          // #endif
          // #ifndef APP-NVUE
          formatter: function(value, index) {
            return value + '%'
          },
          // #endif
        }
      },
      series: [{
        name: t('PreAccIndex.ResidenceTime'), // '停留时长',
        data: val.series?.[0]?.data || [],
        type: 'bar',
        itemStyle: {
          color: '#387CF5'
        },
        barWidth: '20px',
      }]
    }
    chartsRef.value?.initCharts(option)
  }
</script>

<style>
</style>