GateFlowDirection.nvue 3.61 KB
<template>
  <CardNvue :title="t('PreMallChart.mall_visitors_flow_direction.sankey')" :auto-height="true">
    <template #content>
      <ChartsNvue v-if="renderFlag === '1'" height="560rpx" ref="flowDirectRef" />
      <view class="p-empty" v-if="renderFlag === '0'" style="margin-top: 40rpx;">
        <image class="p-empty-img" src="/static/common/empty.png" mode=""></image>
        <text class="p-empty-text chart-text">{{ t('maintenance.monitor.project.empty.text') }}</text>
      </view>
    </template>
  </CardNvue>
</template>

<script setup>
  import {
    nextTick,
    onMounted,
    ref
  } from 'vue'
  import {
    getGateFlowDirectionNewApi
  } from '@/api'
  import CardNvue from './Card.nvue'
  import ChartsNvue from '@/components/Charts.nvue'
  import {
    t
  } from '@/plugins/index.js'

  const renderFlag = ref('')

  const flowDirectRef = ref(null)


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


  const getTrendChartData = async () => {
    try {
      // 参数设置位置:
      const params = {
        accountIds: options.value.accountId,
        endDate: options.value.endDate,
        startDate: options.value.startDate,
        mallId: options.value.storeId
      }
      const {
        data
      } = await getGateFlowDirectionNewApi(params)
      if (data.length > 0) {
        renderFlag.value = '1'
      } else {
        renderFlag.value = '0'
      }

      const option = {
        tooltip: {
          trigger: "item",
          confine: true,
          triggerOn: 'click'
        },
        color: [
          "#e35241",
          "#d83965",
          "#f39c38",
          "#9036aa",
          "#6041b0",
          "#4054af",
          "#4396ec",
          "#4fb9d1",
          "#3f9488",
          "#97c05c",
          "#154bee",
        ],
        animation: false,
        series: [{
          left: 0,
          right: 0,
          top: 10,
          bottom: 5,
          type: "sankey",
          focusNodeAdjacency: "allEdges",
          nodeAlign: "right",
          label: {
            position: 'right',
          },
          data: [],
          links: [],
          lineStyle: {
            color: "source",
            curveness: 0.5,
          },
        }],
      }
      const idataSet = new Set()
      const links = []
      data.forEach((item) => {
        idataSet.add(item.source)
        idataSet.add(item.target + ' ') // 增加空格,保证echarts不报错
        links.push({
          ...item,
          target: item.target + ' ',
        })
      });
      option.series[0].data = [...idataSet].map(n => {
        const lastStr = n[n.length - 1]
        return {
          name: n,
          label: {
            position: lastStr === ' ' ? 'left' : 'right'
          }
        }
      })
      option.series[0].links = links;
      
      
      // #ifndef MP-WEIXIN
      nextTick(() => {
        setTimeout(() => {
          flowDirectRef.value?.initCharts(option)
        }, 0)
      })
      // #endif
      
      // #ifdef MP-WEIXIN
        let attempts = 0;
        while (!flowDirectRef.value && attempts < 40) {
          await new Promise(resolve => setTimeout(resolve, 50));
          attempts++;
        }
        if (flowDirectRef.value?.initCharts) {
          flowDirectRef.value?.initCharts(option);
        } else {
          console.warn('分区流向图表组件未加载完成', flowDirectRef.value);
        }
      // #endif

    } catch (e) {
      console.log(e);
    }
  }
</script>

<style>
</style>