BorderRadius.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div class="hac-echarts-box">
  3. <div ref="echart" class="hac-echarts" :style="`width : ${chart?.clientWidth}px`"/>
  4. </div>
  5. </template>
  6. <script setup>
  7. import * as echarts from 'echarts'
  8. import {useAppStore} from "~src/store";
  9. import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
  10. const props = defineProps({
  11. ratio: {
  12. type: [Number,String],
  13. default: 0
  14. }
  15. })
  16. //初始变量
  17. let chart = null;
  18. const echart = ref(null)
  19. const useAppState = useAppStore()
  20. const AppColor = ref(useAppState.getColor);
  21. watch(() => [
  22. props.ratio
  23. ], ([ratio]) => {
  24. setOptions(ratio, 100 - ratio)
  25. })
  26. //初始化图表
  27. const initChart = () => {
  28. chart = echarts.init(echart.value)
  29. setOptions(props.ratio, 100 - props.ratio)
  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 = (val1,val2) => {
  42. nextTick(() => {
  43. chart.setOption({
  44. tooltip: {
  45. trigger: "item",
  46. formatter: "{d}%"
  47. },
  48. legend: {
  49. type: 'scroll',
  50. orient: 'vertical',
  51. left: '-7',
  52. },
  53. series: [
  54. {
  55. name: 'Access From',
  56. type: 'pie',
  57. radius: ['80%', '50%'],
  58. center: ["76%", "50%"],
  59. avoidLabelOverlap: false,
  60. itemStyle: {
  61. borderRadius: 4,
  62. borderColor: '#fff',
  63. borderWidth: 1
  64. },
  65. label: {
  66. show: false,
  67. position: 'center'
  68. },
  69. emphasis: {
  70. label: {
  71. show: true,
  72. }
  73. },
  74. labelLine: {
  75. show: false
  76. },
  77. data: [
  78. { value: 1048, name: '奉建路' },
  79. { value: 735, name: '西环线' },
  80. { value: 580, name: '陈油路' },
  81. { value: 484, name: '宝北路' }
  82. ]
  83. }
  84. ],
  85. })
  86. })
  87. }
  88. //渲染完成
  89. onMounted(() => {
  90. nextTick(() => {
  91. initChart()
  92. windowResize()
  93. })
  94. })
  95. //被卸载
  96. onUnmounted(() => {
  97. window.removeEventListener("resize",resizeEvent);
  98. chart.dispose()
  99. chart = null
  100. })
  101. const onResize = () => {
  102. nextTick(() => {
  103. chart.resize();
  104. })
  105. }
  106. // 暴露出去
  107. defineExpose({
  108. onResize
  109. })
  110. </script>
  111. <style lang="scss" scoped>
  112. .hac-echarts-box {
  113. display: block;
  114. height: 100%;
  115. overflow: hidden;
  116. position: relative;
  117. .hac-echarts {
  118. position: absolute;
  119. bottom: 0;
  120. left: 0;
  121. right: 0;
  122. z-index: 2;
  123. width: 100%;
  124. height: 100%;
  125. }
  126. }
  127. </style>