index.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <VChart :theme="themeColor" :option="option.options" autoresize></VChart>
  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 { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
  13. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  14. import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
  15. const props = defineProps({
  16. themeSetting: {
  17. type: Object,
  18. required: true
  19. },
  20. themeColor: {
  21. type: Object,
  22. required: true
  23. },
  24. chartConfig: {
  25. type: Object as PropType<config>,
  26. required: true
  27. }
  28. })
  29. use([
  30. CanvasRenderer,
  31. LineChart,
  32. GridComponent,
  33. TooltipComponent,
  34. LegendComponent,
  35. ])
  36. const chartEditStore = useChartEditStore()
  37. const option = reactive({
  38. options: {}
  39. })
  40. // 渐变色处理
  41. watch(()=>chartEditStore.getEditCanvasConfig.chartThemeColor, (newColor: keyof typeof chartColorsSearch) => {
  42. if (!document.location.hash.includes('preview')) {
  43. const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme]
  44. props.chartConfig.option.series.forEach((value: any, index: number) => {
  45. value.areaStyle.color = new graphic.LinearGradient(0, 0, 0, 1, [
  46. {
  47. offset: 0,
  48. color: themeColor[3]
  49. },
  50. {
  51. offset: 1,
  52. color: 'rgba(0,0,0, 0)'
  53. }
  54. ])
  55. themeColor[index]
  56. })
  57. }
  58. option.options = mergeTheme(props.chartConfig.option, props.themeSetting, includes)
  59. },
  60. {
  61. immediate: true
  62. })
  63. </script>