index.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 + index]
  49. },
  50. {
  51. offset: 1,
  52. color: 'rgba(0,0,0, 0)'
  53. }
  54. ])
  55. })
  56. }
  57. option.options = mergeTheme(props.chartConfig.option, props.themeSetting, includes)
  58. },
  59. {
  60. immediate: true
  61. })
  62. </script>