SimpleChart.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <echartsBase :option="setOption"/>
  3. </template>
  4. <script setup>
  5. import echartsBase from './index.vue'
  6. import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
  7. const props = defineProps({
  8. color: {
  9. type: Array,
  10. default: () => (['#5087EC', '#68BBC4'])
  11. },
  12. datas: {
  13. type: Array,
  14. default: () => ([])
  15. }
  16. })
  17. //初始变量
  18. const setOption = ref({})
  19. const colors = ref(props.color)
  20. const datas = ref(props.datas)
  21. watch(() => [
  22. props.color
  23. ], ([color]) => {
  24. colors.value = color
  25. setOptions()
  26. })
  27. //深度监听数据变化
  28. watch(() => [
  29. props.datas,
  30. ], ([data]) => {
  31. datas.value = data
  32. setOptions()
  33. }, { deep: true })
  34. //设置图表
  35. const setOptions = () => {
  36. setOption.value = {
  37. color: colors.value,
  38. tooltip: {
  39. trigger: "item"
  40. },
  41. legend: {
  42. type: 'scroll',
  43. orient: 'vertical',
  44. left: '-7',
  45. },
  46. series: [
  47. {
  48. type: 'pie',
  49. radius: '80%',
  50. center: ["76%", "50%"],
  51. label: {
  52. show: false,
  53. position: "center",
  54. },
  55. labelLine: {
  56. show: false,
  57. },
  58. data: datas.value,
  59. emphasis: {
  60. itemStyle: {
  61. shadowBlur: 10,
  62. shadowOffsetX: 0,
  63. shadowColor: 'rgba(0, 0, 0, 0.5)'
  64. }
  65. }
  66. }
  67. ]
  68. }
  69. }
  70. //渲染完成
  71. onMounted(() => {
  72. nextTick(() => {
  73. setOptions()
  74. })
  75. })
  76. </script>