index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div class="data_body">
  3. <div style="width: 100%; height: 100%">
  4. <div id="charts_canvas" class="echart" style="width: 100%; height: 100%"></div>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. props: {
  11. EworksNumArray: {
  12. type: Array,
  13. },
  14. },
  15. data() {
  16. return {
  17. chartObj: null,
  18. ooption: {
  19. data: [],
  20. },
  21. option: {
  22. tooltip: {
  23. trigger: "item",
  24. },
  25. grid: {
  26. top: "5%",
  27. left: "5%",
  28. right: "5%",
  29. bottom: "5%",
  30. containLabel: true,
  31. },
  32. series: [
  33. {
  34. type: "pie",
  35. radius: '50%',
  36. avoidLabelOverlap: true,
  37. emphasis: {
  38. label: {
  39. show: true,
  40. fontSize: 16,
  41. fontWeight: "bold",
  42. },
  43. },
  44. data: [
  45. // { value: 123, name: "一年级" },
  46. // { value: 200, name: "二年级" },
  47. // { value: 250, name: "三年级" },
  48. // { value: 450, name: "四年级" },
  49. // { value: 300, name: "五年级" },
  50. // { value: 100, name: "六年级" },
  51. ],
  52. },
  53. ],
  54. },
  55. };
  56. },
  57. methods: {
  58. setChart(option) {
  59. // 雷达图显示的标签
  60. let newPromise = new Promise((resolve) => {
  61. resolve();
  62. });
  63. //然后异步执行echarts的初始化函数
  64. newPromise.then(() => {
  65. const chartObj = this.$echarts.init(
  66. //劳动课程
  67. this.$el.querySelector("#charts_canvas")
  68. );
  69. this.option.series[0].data = this.ooption.data.filter(item => item.value !== 0);
  70. // 初始化雷达图
  71. this.chartObj = chartObj;
  72. this.chartObj.setOption(this.option);
  73. });
  74. },
  75. setArray(array) {
  76. this.ooption = {
  77. data: [],
  78. };
  79. for (var i = 0; i < array.length; i++) {
  80. this.ooption.data.push({ value: array[i].works, name: array[i].name });
  81. }
  82. if (!this.chartObj) {
  83. this.setChart(this.ooption);
  84. } else {
  85. this.option.series[0].data = this.ooption.data;
  86. this.chartObj.setOption(this.option);
  87. }
  88. this.$forceUpdate();
  89. },
  90. },
  91. watch: {
  92. EworksNumArray: {
  93. immediate: true,
  94. deep: true,
  95. handler(newValue, oldValue) {
  96. this.setArray(newValue);
  97. this.$forceUpdate();
  98. },
  99. },
  100. },
  101. mounted() {
  102. this.setArray(this.EworksNumArray);
  103. var _this = this;
  104. window.addEventListener("resize", () => {
  105. if (_this.chartObj) {
  106. _this.chartObj.resize();
  107. }
  108. });
  109. },
  110. };
  111. </script>
  112. <style scoped>
  113. .data_body {
  114. height: 100%;
  115. /* display: flex; */
  116. position: relative;
  117. border-radius: 5px;
  118. /* border: 1px solid #eee; */
  119. margin: 0 auto;
  120. box-sizing: border-box;
  121. padding: 0;
  122. width: 95%;
  123. background: #fff;
  124. }
  125. </style>