index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. monthArray: {
  12. type: Array,
  13. default: []
  14. },
  15. },
  16. data() {
  17. return {
  18. chartObj: null,
  19. ooption: {
  20. xdata: [],
  21. teacher: [],
  22. student: [],
  23. },
  24. option: {
  25. tooltip: {
  26. trigger: 'axis'
  27. },
  28. // legend: {
  29. // data: ['老师', '学生'],
  30. // right: '10'
  31. // },
  32. grid: {
  33. top: '30',
  34. left: '5%',
  35. right: '5%',
  36. bottom: '5%',
  37. containLabel: true
  38. },
  39. xAxis: {
  40. type: 'category',
  41. boundaryGap: true,
  42. // data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月']
  43. data: []
  44. },
  45. yAxis: {
  46. type: 'value'
  47. },
  48. series: [
  49. {
  50. name: '用户',
  51. type: 'line',
  52. // data: [120, 50, 101, 130, 110, 120]
  53. areaStyle: {
  54. color: 'rgb(220, 231, 251)'
  55. },
  56. data: [],
  57. itemStyle: {
  58. normal: {
  59. color: function (params) {
  60. return "#106bff";
  61. },
  62. },
  63. },
  64. },
  65. ]
  66. },
  67. };
  68. },
  69. methods: {
  70. setChart(option) {
  71. // 雷达图显示的标签
  72. let newPromise = new Promise((resolve) => {
  73. resolve();
  74. });
  75. //然后异步执行echarts的初始化函数
  76. newPromise.then(() => {
  77. const chartObj = this.$echarts.init(
  78. //劳动课程
  79. this.$el.querySelector("#charts_canvas")
  80. );
  81. // this.option.xAxis.data = option.xdata;
  82. // this.option.series[0].data = option.teacher;
  83. this.option.xAxis.data = ['3月', '4月', '5月', '6月', '7月', '8月', '9月'];
  84. this.option.series[0].data = [300, 500, 490, 510, 102, 201, 550];
  85. // 初始化雷达图
  86. this.chartObj = chartObj;
  87. this.chartObj.setOption(this.option);
  88. });
  89. },
  90. },
  91. watch: {
  92. monthArray: {
  93. immediate: true,
  94. deep: true,
  95. handler(newValue, oldValue) {
  96. this.ooption = {
  97. xdata: [],
  98. teacher: [],
  99. student: [],
  100. }
  101. let _array = newValue
  102. for (var i = 0; i < _array.length; i++) {
  103. this.ooption.xdata.push(_array[i].Month + '月')
  104. this.ooption.teacher.push(_array[i].teacher)
  105. this.ooption.student.push(_array[i].student)
  106. }
  107. if (!this.chartObj) {
  108. this.setChart(this.ooption);
  109. } else {
  110. this.option.xAxis.data = this.ooption.xdata;
  111. this.option.series[0].data = this.ooption.teacher;
  112. this.option.series[1].data = this.ooption.student;
  113. this.chartObj.setOption(this.option);
  114. }
  115. this.$forceUpdate();
  116. },
  117. },
  118. },
  119. mounted() {
  120. this.ooption = {
  121. xdata: [],
  122. teacher: [],
  123. student: [],
  124. }
  125. let _array = this.monthArray
  126. for (var i = 0; i < _array.length; i++) {
  127. this.ooption.xdata.push(_array[i].Month + '月')
  128. this.ooption.teacher.push(_array[i].teacher)
  129. this.ooption.student.push(_array[i].student)
  130. }
  131. this.setChart(this.ooption);
  132. var _this = this;
  133. window.addEventListener("resize", () => {
  134. if (_this.chartObj) {
  135. _this.chartObj.resize();
  136. }
  137. });
  138. },
  139. };
  140. </script>
  141. <style scoped>
  142. .data_body {
  143. height: 100%;
  144. /* display: flex; */
  145. position: relative;
  146. border-radius: 5px;
  147. /* border: 1px solid #eee; */
  148. margin: 0 auto;
  149. box-sizing: border-box;
  150. padding: 0;
  151. width: 95%;
  152. background: #fff;
  153. }
  154. </style>