index.vue
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<template>
  <v-chart
    ref="vChartRef"
    :init-options="initOptions"
    :theme="themeColor"
    :option="option"
    :manual-update="isPreview()"
    :update-options="{
      replaceMerge: replaceMergeArr
    }"
    autoresize
  ></v-chart>
</template>
<script setup lang="ts">
import { ref, computed, PropType, watch } 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 from './config'
import { mergeTheme } from '@/packages/public/chart'
import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { isPreview } from '@/utils'
import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
import { isObject, isString } from '@/utils/external/is'
const props = defineProps({
  themeSetting: {
    type: Object,
    required: true
  },
  themeColor: {
    type: Object,
    required: true
  },
  chartConfig: {
    type: Object as PropType<config>,
    required: true
  }
})
//判断数据格式是否是字符串
const datasetIsString = () => {
  return isString(props.chartConfig.option.dataset)
    ? eval('(' + props.chartConfig.option.dataset + ')')
    : props.chartConfig.option.dataset
}
const initOptions = useCanvasInitOptions(datasetIsString, props.themeSetting)
use([DatasetComponent, CanvasRenderer, BarChart, GridComponent, TooltipComponent, LegendComponent])
const replaceMergeArr = ref<string[]>()
const option = computed(() => {
  return mergeTheme(datasetIsString, props.themeSetting, [])
})
const { vChartRef } = useChartDataFetch(props.chartConfig, useChartEditStore, newData => {
  chartDataset(newData)
})
const chartDataset = (newValue: Recordable | string) => {
  if (isObject(newValue)) {
    updateVChart(newValue)
  } else if (isString(newValue)) {
    //嵌套字符串使用JSON.parse会报错,这里使用eval函数
    updateVChart(eval('(' + newValue + ')'))
  }
}
const updateVChart = (dataset: object) => {
  new Promise(resolve => {
    setTimeout(() => {
      resolve(
        vChartRef.value?.setOption(
          {
            ...dataset
          },
          {
            notMerge: true
          }
        )
      )
    }, 100)
  })
}
watch(
  () => props.chartConfig.option.dataset,
  newValue => {
    try {
      chartDataset(newValue)
    } catch (e) {
      console.error(
        `文件位置:src\\packages\\components\\external\\InformationsMores\\CustomEcharts\\index.vue`,
        '错误信息:您的json格式有误'
      )
    }
  },
  {
    immediate: true,
    deep: true
  }
)
</script>