index.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  13. import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
  14. import { DatasetComponent, GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
  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. DatasetComponent,
  31. CanvasRenderer,
  32. LineChart,
  33. GridComponent,
  34. TooltipComponent,
  35. LegendComponent,
  36. ])
  37. const chartEditStore = useChartEditStore()
  38. const option = reactive({
  39. options: {}
  40. })
  41. // 渐变色处理
  42. watch(() => chartEditStore.getEditCanvasConfig.chartThemeColor, (newColor: keyof typeof chartColorsSearch) => {
  43. if (!document.location.hash.includes('preview')) {
  44. const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme]
  45. props.chartConfig.option.series.forEach((value: any, index: number) => {
  46. value.areaStyle.color = new graphic.LinearGradient(0, 0, 0, 1, [
  47. {
  48. offset: 0,
  49. color: themeColor[3 + index]
  50. },
  51. {
  52. offset: 1,
  53. color: 'rgba(0,0,0, 0)'
  54. }
  55. ])
  56. })
  57. }
  58. option.options = mergeTheme(props.chartConfig.option, props.themeSetting, includes)
  59. }, {
  60. immediate: true
  61. })
  62. watch(() => props.chartConfig.option.dataset, () => {
  63. option.options = props.chartConfig.option
  64. }, {
  65. deep: true
  66. })
  67. </script>