RoundChart.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <div class="RoundChart-box">
  3. <div ref="echart" class="RoundChart-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 } from 'vue'
  9. //初始变量
  10. let chart = null;
  11. const echart = ref(null)
  12. //初始化图表
  13. const initChart = () => {
  14. chart = echarts.init(echart.value)
  15. setOptions()
  16. }
  17. //监听浏览器窗口变化
  18. const windowResize = () => {
  19. window.addEventListener("resize", resizeEvent);
  20. }
  21. const resizeEvent = () => {
  22. window.requestAnimationFrame(() => {
  23. chart.resize();
  24. })
  25. }
  26. //设置图表
  27. const setOptions = () => {
  28. chart.setOption({
  29. tooltip: {
  30. trigger: "item",
  31. formatter: "{d}%"
  32. },
  33. series: [
  34. {
  35. name: 'Access From',
  36. type: 'pie',
  37. radius: ['80%', '90%'],
  38. center: ["50%", "50%"],
  39. label: {
  40. show: false,
  41. position: "center",
  42. },
  43. labelLine: {
  44. show: false,
  45. },
  46. data: [
  47. {
  48. value: 30,
  49. name: "完成率",
  50. itemStyle: {
  51. color: "#0081ff",
  52. }
  53. },
  54. {
  55. value: 70,
  56. name: "",
  57. itemStyle: {
  58. color: "#F5F5F5",
  59. }
  60. },
  61. ]
  62. }
  63. ]
  64. })
  65. }
  66. //渲染完成
  67. onMounted(() => {
  68. nextTick(() => {
  69. initChart()
  70. windowResize()
  71. })
  72. })
  73. //被卸载
  74. onUnmounted(() => {
  75. window.removeEventListener("resize",resizeEvent);
  76. chart.dispose()
  77. chart = null
  78. })
  79. </script>
  80. <style lang="scss" scoped>
  81. .RoundChart-box {
  82. height: 100%;
  83. overflow: hidden;
  84. position: relative;
  85. .RoundChart-echarts {
  86. position: absolute;
  87. bottom: 0;
  88. left: 0;
  89. right: 0;
  90. z-index: 2;
  91. width: 100%;
  92. height: 100%;
  93. }
  94. }
  95. </style>