PathFlowChart.vue 3.71 KB
<template>
  <div class="path-flow-container">
    <div class="chart-title">用户行为路径分析</div>
    <div v-show="hasData" ref="chart" class="chart-container"></div>
    <div v-show="!hasData" class="no-data-container">
      <div class="no-data-text">暂无数据</div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'PathFlowChart',
  props: {
    chartNodes: {
      type: Array,
      default: () => []
    },
    chartLinks: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    hasData() {
      return this.chartNodes && this.chartNodes.length > 0
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartNodes: {
      handler() {
        this.updateChart()
      },
      deep: true
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      window.addEventListener('resize', this.resizeChart)
    })
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart)
    if (this.chart) {
      this.chart.dispose()
    }
  },
  methods: {
    resizeChart() {
      if (this.chart) {
        this.chart.resize()
      }
    },
    updateChart() {
      if (this.chart) {
        this.chart.setOption({
          series: [
            {
              data: this.chartNodes,
              links: this.chartLinks
            }
          ]
        })
      }
    },
    initChart() {
      const chartDom = this.$refs.chart
      this.chart = echarts.init(chartDom)
      
      const option = {
        tooltip: {
          trigger: 'item',
          triggerOn: 'mousemove'
        },
        series: [
            {
              type: 'sankey',
              data: this.chartNodes,
              links: this.chartLinks,
              left: '3%',
              right: '3%',
              top: '5%',
              bottom: '5%',
              nodeWidth: 100,
              nodeGap: 15,
              layoutIterations: 0,
              emphasis: {
                focus: 'adjacency'
              },
              itemStyle: {
                borderWidth: 1,
                borderColor: '#fff'
              },
              lineStyle: {
                color: 'gradient',
                curveness: 0.5,
                opacity: 0.3
              },
              label: {
                position: 'inside',
                formatter: function(params) {
                  // Hide label for loss nodes if any (though we set show:false in data)
                  if (params.data.isLoss) return ''
                  const name = params.name.replace(/\s\(\d+\)$/, '')
                  return `{name|${name}}\n{value|${params.data.value}}`
                },
                rich: {
                  name: {
                    fontSize: 12,
                    fontWeight: 'bold',
                    color: '#fff',
                    lineHeight: 18
                  },
                  value: {
                    fontSize: 12,
                    color: '#fff',
                    lineHeight: 18
                  }
                }
              }
            }
          ]
      }

      this.chart.setOption(option)
    }
  }
}
</script>

<style scoped>
.path-flow-container {
  width: 100%;
  background: #fff;
  margin-top: 20px;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.chart-title {
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 20px;
}
.chart-container {
  width: 100%;
  height: 800px;
}
.no-data-container {
  width: 100%;
  height: 800px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
  border-radius: 4px;
}
.no-data-text {
  font-size: 16px;
  color: #909399;
}
</style>