MediaChart.vue 2.8 KB

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