index.vue 5.75 KB
<template>
  <v-chart
      ref="vChartRef"
      :init-options="initOptions"
      :theme="themeColor"
      :option="option"
      :manual-update="isPreview()"
      :update-options="{
      replaceMerge: replaceMergeArr
    }"
      autoresize
      @mouseover="handleHighlight"
      @mouseout="handleDownplay"
  >
  </v-chart>
</template>

<script setup lang="ts">
import {ref, nextTick, computed, watch, PropType, onMounted} from 'vue'
import VChart from 'vue-echarts'
import {useCanvasInitOptions} from '@/hooks/useCanvasInitOptions.hook'
import {use} from 'echarts/core'
import {CanvasRenderer} from 'echarts/renderers'
import {BarChart} from 'echarts/charts'
import config, {includes, seriesItem} from './config'
import {mergeTheme} from '@/packages/public/chart'
import {useChartDataFetch} from '@/hooks'
import {CreateComponentType} from '@/packages/index.d'
import {useChartEditStore} from '@/store/modules/chartEditStore/chartEditStore'
import {isPreview} from '@/utils'
import {DatasetComponent, GridComponent, TooltipComponent, LegendComponent} from 'echarts/components'
import isObject from 'lodash/isObject'
import dataJson from './data.json'
import {useFullScreen} from '../../utls/fullScreen'

const props = defineProps({
  themeSetting: {
    type: Object,
    required: true
  },
  themeColor: {
    type: Object,
    required: true
  },
  chartConfig: {
    type: Object as PropType<config>,
    required: true
  }
})

const initOptions = useCanvasInitOptions(props.chartConfig.option, props.themeSetting)

use([DatasetComponent, CanvasRenderer, BarChart, GridComponent, TooltipComponent, LegendComponent])

const replaceMergeArr = ref<string[]>()

const option = computed(() => {
  return mergeTheme(props.chartConfig.option, props.themeSetting, includes)
})

const toolBoxOption = {
  show: true,
  right: 20,
  feature: {
    myFullButton: {
      show: true,
      title: '全屏查看',
      icon: 'path://M733.549304 0l116.434359 116.23452-226.402521 226.40252 57.053835 57.068109 226.459617-226.445342 120.616689 120.41685V0H733.549304zM689.513507 619.855586l-57.068108 57.068109 224.232847 224.232847-122.64362 122.843458h293.676657V729.838022l-114.007751 114.207588-224.190025-224.190024zM338.197775 404.144414l57.068109-57.068109L171.033037 122.843458 293.676657 0H0v294.161978l114.022025-114.207588 224.17575 224.190024zM347.076305 624.294851L120.616689 850.754468 0 730.323343v293.676657h294.161978l-116.420084-116.23452 226.40252-226.40252-57.068109-57.068109z',
      onclick: () => {
        const getEchartDom = vChartRef.value?.getDom()
        const domName = document.getElementById(getEchartDom.id) as HTMLElement
        const htmlName = document.querySelector('html') as HTMLHtmlElement
        useFullScreen(domName, htmlName)
      }
    }
  }
}
props.chartConfig.option = {
  ...props.chartConfig.option,
  ...{toolbox: toolBoxOption}
}

// dataset 无法变更条数的补丁
watch(
    () => props.chartConfig.option.dataset,
    (newData: { dimensions: any }, oldData) => {
      try {
        if (!isObject(newData) || !('dimensions' in newData)) return
        if (Array.isArray(newData?.dimensions)) {
          const seriesArr = []
          for (let i = 0; i < newData.dimensions.length - 1; i++) {
            seriesArr.push(seriesItem)
          }
          replaceMergeArr.value = ['series']
          props.chartConfig.option.series = seriesArr
          nextTick(() => {
            replaceMergeArr.value = []
          })
        }
      } catch (error) {
        console.log(error)
      }
    },
    {
      deep: false
    }
)

let seriesDataNum = -1
let seriesDataMaxLength = 0
let intervalInstance: any = null
//轮播时长
const duration = 1500

// 会重新选择需要选中和展示的数据
const handleSeriesData = () => {
  if (seriesDataNum > -1) {
    vChartRef.value?.dispatchAction({
      type: 'downplay',
      dataIndex: seriesDataNum
    })
  }
  seriesDataNum = seriesDataNum >= seriesDataMaxLength - 1 ? 0 : seriesDataNum + 1
  vChartRef.value?.dispatchAction({
    type: 'showTip',
    seriesIndex: 0,
    dataIndex: seriesDataNum
  })
}

// 新增轮播
const addPieInterval = (newData?: typeof dataJson, skipPre = false) => {
  if (!skipPre && !Array.isArray(newData?.source)) return
  if (!skipPre) seriesDataMaxLength = newData?.source.length || 0
  clearInterval(intervalInstance)
  intervalInstance = setInterval(() => {
    handleSeriesData()
  }, duration)
}

// 取消轮播
const clearPieInterval = () => {
  vChartRef.value?.dispatchAction({
    type: 'hideTip',
    seriesIndex: 0,
    dataIndex: seriesDataNum
  })
  vChartRef.value?.dispatchAction({
    type: 'downplay',
    dataIndex: seriesDataNum
  })
  clearInterval(intervalInstance)
  intervalInstance = null
}

// 处理鼠标聚焦高亮内容
const handleHighlight = () => {
  clearPieInterval()
}

// 处理鼠标取消悬浮
const handleDownplay = () => {
  if (props.chartConfig.option.isCarousel && !intervalInstance) {
    // 恢复轮播
    addPieInterval(undefined, true)
  }
}

watch(
    () => props.chartConfig.option.isCarousel,
    newData => {
      if (newData) {
        addPieInterval(undefined, true)
        props.chartConfig.option.legend.show = false
      } else {
        props.chartConfig.option.legend.show = true
        clearPieInterval()
      }
    }
)

//fix 修复v-chart图表绑定联动组件视图不更新问题
const updateVChart = (newData: any) => {
  if (!newData) return
  vChartRef.value?.clear()
  vChartRef.value?.setOption({
    ...option.value,
    dataset: newData
  })
}

const {vChartRef} = useChartDataFetch(props.chartConfig, useChartEditStore, (newData) => {
  // addPieInterval(newData)
  updateVChart(newData)
})

onMounted(() => {
  seriesDataMaxLength = dataJson.source.length
  if (props.chartConfig.option.isCarousel) {
    addPieInterval(undefined, true)
  }
})
</script>