index.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 { reactive, watch, PropType } from 'vue'
  6. import VChart from 'vue-echarts'
  7. import { use, graphic } 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, index: number) => {
  48. value.areaStyle.color = new graphic.LinearGradient(0, 0, 0, 1, [
  49. {
  50. offset: 0,
  51. color: themeColor[3 + index]
  52. },
  53. {
  54. offset: 1,
  55. color: 'rgba(0,0,0, 0)'
  56. }
  57. ])
  58. })
  59. }
  60. option.options = mergeTheme(props.chartConfig.option, props.themeSetting, includes)
  61. }, {
  62. immediate: true
  63. })
  64. watch(() => props.chartConfig.option.dataset, () => {
  65. option.options = props.chartConfig.option
  66. }, {
  67. deep: true
  68. })
  69. const { vChartRef } = useChartDataFetch(props.chartConfig)
  70. </script>