RoundPieChart.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div class="hc-bar-chart-box">
  3. <div ref="echart" class="hc-bar-chart-echarts" :style="`width : ${chart?.clientWidth}px`"/>
  4. </div>
  5. </template>
  6. <script setup>
  7. import * as echarts from 'echarts'
  8. import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
  9. const props = defineProps({
  10. datas: {
  11. type: Array,
  12. default: () => ([])
  13. },
  14. })
  15. //初始变量
  16. let chart = null;
  17. const echart = ref(null)
  18. const datas = ref(props.datas)
  19. //监听
  20. watch(() => [
  21. props.datas
  22. ], ([data]) => {
  23. datas.value = data
  24. setOptions(data)
  25. })
  26. //初始化图表
  27. const initChart = () => {
  28. chart = echarts.init(echart.value)
  29. setOptions(props.datas)
  30. }
  31. //监听浏览器窗口变化
  32. const windowResize = () => {
  33. window.addEventListener("resize", resizeEvent);
  34. }
  35. const resizeEvent = () => {
  36. window.requestAnimationFrame(() => {
  37. chart.resize();
  38. })
  39. }
  40. //设置图表
  41. const setOptions = (AxisData) => {
  42. chart.setOption({
  43. tooltip: {
  44. trigger: 'item'
  45. },
  46. color: ['#5279AE','#F5CC71','#B8595A','#67B57E'],
  47. series: [
  48. {
  49. type: 'pie',
  50. radius: '80%',
  51. center: ["50%", "45%"],
  52. data: AxisData,
  53. emphasis: {
  54. itemStyle: {
  55. shadowBlur: 10,
  56. shadowOffsetX: 0,
  57. shadowColor: 'rgba(0, 0, 0, 0.4)'
  58. }
  59. },
  60. label:{
  61. formatter: '{b}\ {c}',
  62. }
  63. }
  64. ]
  65. })
  66. }
  67. //渲染完成
  68. onMounted(() => {
  69. nextTick(() => {
  70. initChart()
  71. windowResize()
  72. })
  73. })
  74. //被卸载
  75. onUnmounted(() => {
  76. window.removeEventListener("resize",resizeEvent);
  77. chart.dispose()
  78. chart = null
  79. })
  80. const onResize = () => {
  81. nextTick(() => {
  82. chart.resize();
  83. })
  84. }
  85. // 暴露出去
  86. defineExpose({
  87. onResize
  88. })
  89. </script>
  90. <style lang="scss" scoped>
  91. .hc-bar-chart-box {
  92. height: 100%;
  93. overflow: hidden;
  94. position: relative;
  95. .hc-bar-chart-echarts {
  96. position: absolute;
  97. bottom: 0;
  98. left: 0;
  99. right: 0;
  100. z-index: 2;
  101. width: 100%;
  102. height: 100%;
  103. }
  104. }
  105. </style>