pie-chart.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div class="chart-main" ref="ref_main">
  3. <div class="chart-ref" ref="ref_chart" />
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. import {
  8. getCurrentInstance,
  9. reactive,
  10. ref,
  11. onMounted,
  12. watch,
  13. nextTick,
  14. } from 'vue'
  15. import * as echarts from 'echarts'
  16. const props = defineProps({
  17. data: <any>{},
  18. })
  19. const { proxy } = getCurrentInstance()
  20. const state = reactive({
  21. resizeObserver: <any>null,
  22. chart: <any>null,
  23. })
  24. const ref_chart = ref()
  25. const ref_main = ref()
  26. const colors = ['58, 136, 255', '96, 217, 169', '31, 182, 221', '255, 116, 44']
  27. const initChart = () => {
  28. echarts.dispose(ref_chart.value)
  29. const chart = echarts.init(ref_chart.value)
  30. const padAngle = 1
  31. const option = {
  32. tooltip: {
  33. trigger: 'item',
  34. },
  35. series: [
  36. {
  37. type: 'pie',
  38. data: props.data,
  39. radius: ['68%', '70%'],
  40. padAngle: padAngle,
  41. itemStyle: {
  42. borderRadius: 10,
  43. color: (p) => {
  44. const colorRgb = colors[p.dataIndex % (colors.length - 1)]
  45. return `rgba(${colorRgb}, 1)`
  46. },
  47. },
  48. label: {
  49. formatter: '{b}\n{d}%',
  50. color: '#606266',
  51. fontSize: 16,
  52. fontFamily: 'PingFang SC',
  53. fontWeight: 'bold',
  54. },
  55. labelLine: {
  56. length2: 100,
  57. },
  58. emphasis: {
  59. disabled: true,
  60. },
  61. },
  62. {
  63. type: 'pie',
  64. data: props.data,
  65. radius: ['52%', '62%'],
  66. padAngle: padAngle,
  67. itemStyle: {
  68. borderRadius: 10,
  69. color: (p) => {
  70. const colorRgb = colors[p.dataIndex % (colors.length - 1)]
  71. return {
  72. type: 'linear',
  73. x: 1,
  74. y: 0,
  75. x2: 0,
  76. y2: 1,
  77. colorStops: [
  78. {
  79. offset: 0,
  80. color: `rgba(${colorRgb}, 0.3)`, // 0% 处的颜色
  81. },
  82. {
  83. offset: 1,
  84. color: `rgba(${colorRgb}, 1)`, // 100% 处的颜色
  85. },
  86. ],
  87. global: false, // 缺省为 false
  88. }
  89. },
  90. },
  91. emphasis: {
  92. disabled: true,
  93. },
  94. label: {
  95. show: false,
  96. },
  97. },
  98. ],
  99. }
  100. chart.setOption(option)
  101. state.resizeObserver = new ResizeObserver((entries) => {
  102. for (const entry of entries) {
  103. chart && chart.resize()
  104. }
  105. })
  106. state.resizeObserver.observe(ref_main.value)
  107. return chart
  108. }
  109. watch(
  110. () => props.data,
  111. () => {
  112. state.chart = initChart()
  113. },
  114. )
  115. onMounted(() => {
  116. nextTick(() => {
  117. state.chart = initChart()
  118. })
  119. return () => {
  120. state.resizeObserver?.unobserve(ref_main?.value)
  121. state.resizeObserver?.disconnect()
  122. }
  123. })
  124. </script>
  125. <style lang="scss" scoped>
  126. .chart-main {
  127. flex: 1;
  128. width: 100%;
  129. height: 100%;
  130. .chart-ref {
  131. width: 100%;
  132. height: 100%;
  133. }
  134. }
  135. </style>