index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. courseNumberArray: {
  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. series: [
  26. {
  27. type: 'pie',
  28. radius: ['35%', '70%'],
  29. avoidLabelOverlap: false,
  30. emphasis: {
  31. label: {
  32. show: true,
  33. fontSize: 16,
  34. fontWeight: 'bold'
  35. }
  36. },
  37. data: [
  38. // { value: 1048, name: '一年级' },
  39. // { value: 735, name: '二年级' },
  40. // { value: 580, name: '三年级' },
  41. // { value: 484, name: '四年级' },
  42. // { value: 484, name: '五年级' },
  43. // { value: 300, name: '六年级' }
  44. ]
  45. }
  46. ]
  47. },
  48. };
  49. },
  50. methods: {
  51. setChart(option) {
  52. // 雷达图显示的标签
  53. let newPromise = new Promise((resolve) => {
  54. resolve();
  55. });
  56. //然后异步执行echarts的初始化函数
  57. newPromise.then(() => {
  58. const chartObj = this.$echarts.init(
  59. //劳动课程
  60. this.$el.querySelector("#charts_canvas")
  61. );
  62. this.option.series[0].data = this.ooption.data;
  63. chartObj.off('click')
  64. let _this = this
  65. chartObj.on('click', function (param) {
  66. //param参数包含的内容有:
  67. //param.name:X轴的值
  68. //param.data:Y轴的值
  69. //param.value:Y轴的值
  70. //param.type:点击事件均为click
  71. //param.seriesName:legend的名称
  72. //param.seriesIndex:系列序号(series中当前图形是第几个图形第几个)
  73. //param.dataIndex:数值序列(X轴上当前点是第几个点)
  74. //alert(param.seriesName); //legend的名称
  75. console.log(param); //X轴的值
  76. _this.$emit('openCourse',param.dataIndex)
  77. });
  78. // 初始化雷达图
  79. this.chartObj = chartObj;
  80. this.chartObj.setOption(this.option);
  81. });
  82. },
  83. setArray(array) {
  84. this.ooption = {
  85. data: [],
  86. }
  87. for (var i = 0; i < array.length; i++) {
  88. this.ooption.data.push({ value: array[i].course, name: array[i].name})
  89. }
  90. if (!this.chartObj) {
  91. this.setChart(this.ooption);
  92. } else {
  93. this.option.series[0].data = this.ooption.data;
  94. this.chartObj.setOption(this.option);
  95. }
  96. this.$forceUpdate();
  97. },
  98. },
  99. watch: {
  100. courseNumberArray: {
  101. immediate: true,
  102. deep: true,
  103. handler(newValue, oldValue) {
  104. this.setArray(newValue)
  105. this.$forceUpdate();
  106. },
  107. },
  108. },
  109. mounted() {
  110. this.setArray(this.courseNumberArray)
  111. var _this = this;
  112. window.addEventListener("resize", () => {
  113. if (_this.chartObj) {
  114. _this.chartObj.resize();
  115. }
  116. });
  117. },
  118. };
  119. </script>
  120. <style scoped>
  121. .data_body {
  122. height: 100%;
  123. /* display: flex; */
  124. position: relative;
  125. border-radius: 5px;
  126. /* border: 1px solid #eee; */
  127. margin: 0 auto;
  128. box-sizing: border-box;
  129. padding: 0;
  130. width: 95%;
  131. background: #fff;
  132. }
  133. </style>