index.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <v-chart :theme="themeColor" :option="option.value" autoresize></v-chart>
  3. </template>
  4. <script setup lang="ts">
  5. import { PropType, watch, reactive } from 'vue'
  6. import VChart from 'vue-echarts'
  7. import { use } from 'echarts/core'
  8. import 'echarts-liquidfill/src/liquidFill.js'
  9. import { CanvasRenderer } from 'echarts/renderers'
  10. import { GridComponent } from 'echarts/components'
  11. import config from './config'
  12. import { isPreview, isString, isNumber } from '@/utils'
  13. import { chartColorsSearch, defaultTheme } from '@/settings/chartThemes/index'
  14. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  15. import { useChartDataFetch } from '@/hooks'
  16. const props = defineProps({
  17. themeSetting: {
  18. type: Object,
  19. required: true
  20. },
  21. themeColor: {
  22. type: Object,
  23. required: true
  24. },
  25. chartConfig: {
  26. type: Object as PropType<config>,
  27. required: true
  28. }
  29. })
  30. use([CanvasRenderer, GridComponent])
  31. const chartEditStore = useChartEditStore()
  32. const option = reactive({
  33. value: {}
  34. })
  35. // 渐变色处理
  36. watch(
  37. () => chartEditStore.getEditCanvasConfig.chartThemeColor,
  38. (newColor: keyof typeof chartColorsSearch) => {
  39. try {
  40. if (!isPreview()) {
  41. const themeColor = chartColorsSearch[newColor] || chartColorsSearch[defaultTheme]
  42. // 背景颜色
  43. props.chartConfig.option.series[0].backgroundStyle.color = themeColor[2]
  44. // 水球颜色
  45. props.chartConfig.option.series[0].color[0].colorStops = [
  46. {
  47. offset: 0,
  48. color: themeColor[0]
  49. },
  50. {
  51. offset: 1,
  52. color: themeColor[1]
  53. }
  54. ]
  55. }
  56. option.value = props.chartConfig.option
  57. } catch (error) {
  58. console.log(error)
  59. }
  60. },
  61. {
  62. immediate: true
  63. }
  64. )
  65. // 数据处理
  66. const dataHandle = (newData: number | string) => {
  67. newData = isString(newData) ? parseFloat(newData) : newData
  68. return parseFloat(newData.toFixed(2))
  69. }
  70. // 编辑
  71. watch(
  72. () => props.chartConfig.option.dataset,
  73. newData => {
  74. if (!isString(newData) && !isNumber(newData)) return
  75. props.chartConfig.option.series[0].data = [dataHandle(newData)]
  76. option.value = props.chartConfig.option
  77. },
  78. {
  79. immediate: true,
  80. deep: false
  81. }
  82. )
  83. // 预览
  84. useChartDataFetch(props.chartConfig, useChartEditStore, (newData: number) => {
  85. // @ts-ignore
  86. option.value.series[0].data = [dataHandle(newData)]
  87. })
  88. </script>