index.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <v-chart ref="vChartRef" :theme="themeColor" :option="option.options" :manual-update="isPreview()" autoresize></v-chart>
  3. </template>
  4. <script setup lang="ts">
  5. import { PropType, watch, reactive } from 'vue';
  6. import VChart from 'vue-echarts'
  7. import { use } from 'echarts/core'
  8. import { CanvasRenderer } from 'echarts/renderers'
  9. import { LineChart } from 'echarts/charts'
  10. import config, { includes } from './config'
  11. import { mergeTheme } from '@/packages/public/chart'
  12. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  13. import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
  14. import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
  15. import { useChartDataFetch } from '@/hooks/useChartDataFetch.hook'
  16. import { isPreview } from '@/utils'
  17. const props = defineProps({
  18. themeSetting: {
  19. type: Object,
  20. required: true
  21. },
  22. themeColor: {
  23. type: Object,
  24. required: true
  25. },
  26. chartConfig: {
  27. type: Object as PropType<config>,
  28. required: true
  29. }
  30. })
  31. use([
  32. DatasetComponent,
  33. CanvasRenderer,
  34. LineChart,
  35. GridComponent,
  36. TooltipComponent,
  37. LegendComponent
  38. ])
  39. const chartEditStore = useChartEditStore()
  40. const option = reactive({
  41. options: {}
  42. })
  43. // 初始化与渐变色处理
  44. watch(() => chartEditStore.getEditCanvasConfig.chartThemeColor, (newColor: keyof typeof chartColorsSearch) => {
  45. if (!isPreview()) {
  46. const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme]
  47. props.chartConfig.option.series.forEach((value: any) => {
  48. value.lineStyle.shadowColor = themeColor[2]
  49. value.lineStyle.color.colorStops.forEach((v: { color: string }, i: number) => {
  50. v.color = themeColor[i]
  51. })
  52. })
  53. option.options = mergeTheme(props.chartConfig.option, props.themeSetting, includes)
  54. }
  55. }, {
  56. immediate: true,
  57. })
  58. watch(() => props.chartConfig.option.dataset, () => {
  59. option.options = props.chartConfig.option
  60. }, {
  61. deep: true
  62. })
  63. const { vChartRef } = useChartDataFetch(props.chartConfig)
  64. </script>