index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. loginArray: {
  12. type: Array
  13. },
  14. },
  15. data() {
  16. return {
  17. chartObj: null,
  18. ooption: {
  19. xdata: [],
  20. type: [],
  21. },
  22. option: {
  23. tooltip: {
  24. trigger: "item",
  25. formatter: "{b} {c}(小时)",
  26. },
  27. grid: {
  28. top: '5%',
  29. left: '5%',
  30. right: '5%',
  31. bottom: '5%',
  32. containLabel: true
  33. },
  34. xAxis: {
  35. type: "category",
  36. data: ["老师", "学生"],
  37. },
  38. yAxis: {
  39. type: "value",
  40. data: [],
  41. },
  42. series: [
  43. {
  44. data: [],
  45. barWidth: 30,
  46. type: "bar",
  47. },
  48. ],
  49. },
  50. };
  51. },
  52. methods: {
  53. setChart(array) {
  54. // 雷达图显示的标签
  55. let newPromise = new Promise((resolve) => {
  56. resolve();
  57. });
  58. //然后异步执行echarts的初始化函数
  59. newPromise.then(() => {
  60. const chartObj = this.$echarts.init(
  61. this.$el.querySelector("#charts_canvas")
  62. );
  63. this.option.series[0].data = array;
  64. // 初始化雷达图
  65. this.chartObj = chartObj;
  66. this.chartObj.setOption(this.option);
  67. });
  68. },
  69. setJson(array) {
  70. this.ooption = {
  71. xdata: [],
  72. sdata: [],
  73. max: 0
  74. }
  75. if (!this.chartObj) {
  76. this.setChart(array);
  77. } else {
  78. this.option.series[0].data = array;
  79. this.chartObj.setOption(this.option);
  80. }
  81. }
  82. },
  83. watch: {
  84. loginArray: {
  85. immediate: true,
  86. deep: true,
  87. handler(newValue, oldValue) {
  88. this.setJson(newValue)
  89. this.$forceUpdate();
  90. },
  91. },
  92. },
  93. mounted() {
  94. this.setJson(this.loginArray)
  95. var _this = this;
  96. window.addEventListener("resize", () => {
  97. if (_this.chartObj) {
  98. _this.chartObj.resize();
  99. }
  100. });
  101. },
  102. };
  103. </script>
  104. <style scoped>
  105. .data_body {
  106. height: 100%;
  107. /* display: flex; */
  108. position: relative;
  109. border-radius: 5px;
  110. /* border: 1px solid #eee; */
  111. margin: 0 auto;
  112. box-sizing: border-box;
  113. padding: 0;
  114. width: 95%;
  115. background: #fff;
  116. }
  117. </style>