index.vue 2.1 KB

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