pieChart.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <div shadow="never" class="homeBoxCard" v-loading="loading">
  3. <div class="height300" id="pieChart" ref="pieChart" />
  4. </div>
  5. </template>
  6. <script>
  7. import { defineComponent, onMounted, ref ,watch} from 'vue'
  8. // import { useStore } from 'vuex'
  9. import * as echarts from 'echarts'
  10. // var total_datas = 0
  11. // for (var i = 0; i < echartData.length; i++) {
  12. // total_datas += Number(echartData[i].value)
  13. // console.log(typeof echartData[i].value)
  14. // }
  15. export default defineComponent({
  16. name: 'PieChart',
  17. props: {
  18. getTableData: Object,
  19. },
  20. setup(props) {
  21. // 读取数据 func
  22. const loading = ref(true)
  23. const getData = async () => {
  24. loading.value = false
  25. }
  26. function echarts2() {
  27. let myChart = echarts.init(document.getElementById('pieChart'))
  28. // 绘制图表
  29. myChart.setOption({
  30. title: [
  31. {
  32. text: 1,
  33. subtext: '共计回路',
  34. textStyle: {
  35. fontSize: 24,
  36. color: 'black',
  37. },
  38. subtextStyle: {
  39. fontSize: 16,
  40. color: 'black',
  41. },
  42. textAlign: 'center',
  43. left: '49%',
  44. top: '40%',
  45. },
  46. ],
  47. tooltip: {
  48. trigger: 'item',
  49. formatter: function (parms) {
  50. var str =
  51. '</br>' +
  52. parms.marker +
  53. '' +
  54. parms.data.legendname +
  55. '</br>'
  56. // +
  57. // '数量:' +
  58. // parms.data.value +
  59. // '</br>' +
  60. // '占比:' +
  61. // parms.percent +
  62. // '%'
  63. return str
  64. },
  65. },
  66. legend: {
  67. type: 'scroll',
  68. orient: 'horizontal',
  69. bottom: '0%',
  70. align: 'left',
  71. // top:'middle',
  72. textStyle: {
  73. color: '#8C8C8C',
  74. },
  75. height: 250,
  76. },
  77. series: [
  78. {
  79. // name: '标题',
  80. type: 'pie',
  81. center: ['50%', '50%'],
  82. radius: ['37%', '55%'],
  83. clockwise: false, //饼图的扇区是否是顺时针排布
  84. avoidLabelOverlap: false,
  85. label: {
  86. show: false,
  87. },
  88. labelLine: {
  89. show: false,
  90. },
  91. data: [
  92. {
  93. value: props.getTableData.score>=80?1:0,
  94. legendname: '优秀',
  95. name: '优秀',
  96. itemStyle: { color: '#40ABFE' },
  97. },
  98. {
  99. value: props.getTableData.score>=60?1:0,
  100. legendname: '合格',
  101. name: '合格',
  102. itemStyle: { color: '#EEAA3F' },
  103. },
  104. {
  105. value: props.getTableData.score<60?1:0,
  106. legendname: '不合格',
  107. name: '不合格',
  108. itemStyle: { color: '#FF2222' },
  109. },
  110. ],
  111. },
  112. ],
  113. })
  114. window.addEventListener('resize', () => {
  115. myChart.resize()
  116. })
  117. }
  118. const writeValue = (val) => {
  119. val
  120. getData()
  121. echarts2()
  122. }
  123. //监听变化
  124. watch(
  125. () => props.getTableData,
  126. (newVal, oldVal, clear) => {
  127. // 执行异步任务,并得到关闭异步任务的 id
  128. // console.log(newVal)
  129. let id = writeValue(newVal, oldVal)
  130. // 如果 watch 监听被重复执行了,则会先清除上次未完成的异步任务
  131. clear(() => clearTimeout(id))
  132. },
  133. { lazy: true }
  134. )
  135. onMounted(() => {
  136. getData()
  137. echarts2()
  138. })
  139. return {
  140. loading,
  141. echarts2,
  142. }
  143. },
  144. })
  145. </script>
  146. <style lang="scss" scoped>
  147. .homeBoxCard {
  148. margin-bottom: 24px;
  149. ::v-deep(.el-card__header) {
  150. padding-left: 12px;
  151. padding-right: 12px;
  152. }
  153. ::v-deep(.el-card__body) {
  154. padding: 12px;
  155. font-size: 14px;
  156. line-height: 1.5715;
  157. }
  158. ::v-deep(.el-divider) {
  159. margin: 8px 0;
  160. }
  161. .num {
  162. font-size: 30px;
  163. color: #515a6e;
  164. }
  165. .height300 {
  166. height: 300px;
  167. }
  168. }
  169. </style>