PathFlowChart.vue 2.73 KB
<template>
  <div class="path-flow-container">
    <div ref="chart" class="chart-container"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { nodes, links } from './chartData'

export default {
  name: 'PathFlowChart',
  data() {
    return {
      chart: null
    }
  },
  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()
      }
    },
    initChart() {
      const chartDom = this.$refs.chart
      this.chart = echarts.init(chartDom)
      
      const option = {
        title: {
          text: '用户行为路径分析',
          left: 'center',
          top: 10
        },
        tooltip: {
          trigger: 'item',
          triggerOn: 'mousemove'
        },
        series: [
            {
              type: 'sankey',
              data: nodes,
              links: links,
              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-container {
  width: 100%;
  height: 800px;
}
</style>