index.vue 3.2 KB

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