ArrRoundChart.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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: Object,
  12. default: () => ({})
  13. },
  14. config: {
  15. type: Object,
  16. default: () => ({})
  17. },
  18. })
  19. //初始变量
  20. let chart = null;
  21. const echart = ref(null)
  22. const datas = ref(props.datas)
  23. //监听
  24. watch(() => [
  25. props.datas
  26. ], ([data]) => {
  27. datas.value = data
  28. setDatas(data)
  29. })
  30. //初始化图表
  31. const initChart = () => {
  32. chart = echarts.init(echart.value)
  33. setDatas(props.datas)
  34. }
  35. //设置数据
  36. const setDatas = (data) => {
  37. let AxisData = [];
  38. const {name, key} = props.config;
  39. for (let i = 0; i < name.length; i++) {
  40. AxisData.push({
  41. name: name[i],
  42. value: data[key[i]]
  43. })
  44. }
  45. setOptions(AxisData)
  46. }
  47. //监听浏览器窗口变化
  48. const windowResize = () => {
  49. window.addEventListener("resize", resizeEvent);
  50. }
  51. const resizeEvent = () => {
  52. window.requestAnimationFrame(() => {
  53. chart.resize();
  54. })
  55. }
  56. //设置图表
  57. const setOptions = (AxisData) => {
  58. const {color, label} = props.config;
  59. chart.setOption({
  60. color: color,
  61. tooltip: {
  62. trigger: 'item'
  63. },
  64. series: [
  65. {
  66. name: label,
  67. type: 'pie',
  68. radius: ['50%', '80%'],
  69. center: ["50%", "50%"],
  70. avoidLabelOverlap: false,
  71. itemStyle: {
  72. borderRadius: 5,
  73. borderColor: '#fff',
  74. borderWidth: 2
  75. },
  76. emphasis: {
  77. label: {
  78. show: true,
  79. fontSize: '20',
  80. fontWeight: 'bold'
  81. }
  82. },
  83. data: AxisData,
  84. label:{
  85. formatter: '{b}\ {c}',
  86. }
  87. }
  88. ]
  89. })
  90. }
  91. //渲染完成
  92. onMounted(() => {
  93. nextTick(() => {
  94. initChart()
  95. windowResize()
  96. })
  97. })
  98. //被卸载
  99. onUnmounted(() => {
  100. window.removeEventListener("resize",resizeEvent);
  101. chart.dispose()
  102. chart = null
  103. })
  104. const onResize = () => {
  105. nextTick(() => {
  106. chart.resize();
  107. })
  108. }
  109. // 暴露出去
  110. defineExpose({
  111. onResize
  112. })
  113. </script>
  114. <style lang="scss" scoped>
  115. .hc-bar-chart-box {
  116. height: 100%;
  117. overflow: hidden;
  118. position: relative;
  119. .hc-bar-chart-echarts {
  120. position: absolute;
  121. bottom: 0;
  122. left: 0;
  123. right: 0;
  124. z-index: 2;
  125. width: 100%;
  126. height: 100%;
  127. }
  128. }
  129. </style>