index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. courseArray: {
  12. type: Array,
  13. },
  14. },
  15. data() {
  16. return {
  17. chartObj: null,
  18. ooption: {
  19. hours: [],
  20. days: [],
  21. data: [],
  22. },
  23. option: {
  24. tooltip: {
  25. position: 'top',
  26. formatter: function (params) {
  27. // console.log(params);
  28. return params.marker + params.name + ' ' + params.data[1];//params.seriesName + '<br>' + params.
  29. }
  30. },
  31. title: [],
  32. singleAxis: [],
  33. series: []
  34. },
  35. };
  36. },
  37. methods: {
  38. setChart(option) {
  39. // 雷达图显示的标签
  40. let newPromise = new Promise((resolve) => {
  41. resolve();
  42. });
  43. //然后异步执行echarts的初始化函数
  44. newPromise.then(() => {
  45. const chartObj = this.$echarts.init(
  46. //劳动课程
  47. this.$el.querySelector("#charts_canvas")
  48. );
  49. const hours = option.hours
  50. // [
  51. // // '语文', '数学', '英语', '科学', '体育', '音乐', '美术',
  52. // // '劳动', '其他',
  53. // ];
  54. // prettier-ignore
  55. const days = option.days
  56. // [
  57. // // '一年级', '二年级', '三年级', '四年级', '五年级', '六年级'
  58. // ];
  59. // prettier-ignore
  60. const data = option.data
  61. // [
  62. // // [0, 0, 2], [0, 1, 1], [0, 2, 3], [0, 3, 0], [0, 4, 5], [0, 5, 5], [0, 6, 7], [0, 7, 8], [0, 8, 1],
  63. // // [1, 0, 5], [1, 1, 1], [1, 2, 2], [1, 3, 0], [1, 4, 5], [1, 5, 7], [1, 6, 7], [1, 7, 8], [1, 8, 6],
  64. // // [2, 0, 5], [2, 1, 2], [2, 2, 0], [2, 3, 2], [2, 4, 1], [2, 5, 5], [2, 6, 4], [2, 7, 4], [2, 8, 1],
  65. // // [3, 0, 1], [3, 1, 1], [3, 2, 1], [3, 3, 0], [3, 4, 5], [3, 5, 2], [3, 6, 7], [3, 7, 8], [3, 8, 5],
  66. // // [4, 0, 5], [4, 1, 3], [4, 2, 0], [4, 3, 3], [4, 4, 4], [4, 5, 2], [4, 6, 3], [4, 7, 5], [4, 8, 1],
  67. // // [5, 0, 5], [5, 1, 1], [5, 2, 0], [5, 3, 0], [5, 4, 5], [5, 5, 5], [5, 6, 7], [5, 7, 8], [5, 8, 3],
  68. // ];
  69. const title = [];
  70. const singleAxis = [];
  71. const series = [];
  72. days.forEach(function (day, idx) {
  73. title.push({
  74. textBaseline: 'middle',
  75. top: ((idx + 0.5) * 90) / days.length + '%',
  76. text: day,
  77. textStyle: {
  78. fontSize: 12,
  79. },
  80. });
  81. singleAxis.push({
  82. left: 70,
  83. type: 'category',
  84. boundaryGap: false,
  85. data: hours,
  86. top: (idx * 90) / days.length + 5 + '%',
  87. height: 90 / days.length - 10 + '%'
  88. });
  89. series.push({
  90. singleAxisIndex: idx,
  91. coordinateSystem: 'singleAxis',
  92. type: 'scatter',
  93. data: [],
  94. symbolSize: function (dataItem) {
  95. return dataItem[1] * 3;
  96. }
  97. });
  98. });
  99. data.forEach(function (dataItem) {
  100. series[dataItem[0]].data.push([dataItem[1], dataItem[2]]);
  101. });
  102. chartObj.off('click')
  103. let _this = this
  104. chartObj.on('click', function (param) {
  105. //param参数包含的内容有:
  106. //param.name:X轴的值
  107. //param.data:Y轴的值
  108. //param.value:Y轴的值
  109. //param.type:点击事件均为click
  110. //param.seriesName:legend的名称
  111. //param.seriesIndex:系列序号(series中当前图形是第几个图形第几个)
  112. //param.dataIndex:数值序列(X轴上当前点是第几个点)
  113. //alert(param.seriesName); //legend的名称
  114. console.log(param); //X轴的值
  115. _this.$emit('openCourse',param.componentIndex,param.data[0])
  116. });
  117. this.option.title = title
  118. this.option.singleAxis = singleAxis
  119. this.option.series = series
  120. // 初始化雷达图
  121. this.chartObj = chartObj;
  122. this.chartObj.setOption(this.option);
  123. });
  124. },
  125. setJson(array) {
  126. if (array != undefined && array.length > 0) {
  127. this.ooption = {
  128. hours: [],
  129. days: [],
  130. data: [],
  131. }
  132. let _grade = []
  133. let _subject = []
  134. let data = []
  135. array.forEach(function (item, idx) {
  136. _grade.push(item.name)
  137. })
  138. array[0].subject.forEach(function (item, idx) {
  139. _subject.push(item.name)
  140. })
  141. array.forEach(function (item, idx) {
  142. item.subject.forEach(function (item2, idx2) {
  143. data.push([idx, idx2, item2.course])
  144. })
  145. })
  146. this.ooption.hours = _subject
  147. this.ooption.days = _grade
  148. this.ooption.data = data
  149. if (!this.chartObj) {
  150. this.setChart(this.ooption);
  151. } else {
  152. const hours = this.ooption.hours;
  153. // prettier-ignore
  154. const days = this.ooption.days;
  155. // prettier-ignore
  156. const data = this.ooption.data;
  157. const title = [];
  158. const singleAxis = [];
  159. const series = [];
  160. days.forEach(function (day, idx) {
  161. title.push({
  162. textBaseline: 'middle',
  163. top: ((idx + 0.5) * 90) / days.length + '%',
  164. text: day,
  165. textStyle: {
  166. fontSize: 12,
  167. },
  168. });
  169. singleAxis.push({
  170. left: 70,
  171. type: 'category',
  172. boundaryGap: false,
  173. data: hours,
  174. top: (idx * 90) / days.length + 5 + '%',
  175. height: 90 / days.length - 10 + '%'
  176. });
  177. series.push({
  178. singleAxisIndex: idx,
  179. coordinateSystem: 'singleAxis',
  180. type: 'scatter',
  181. data: [],
  182. symbolSize: function (dataItem) {
  183. return dataItem[1] * 3;
  184. }
  185. });
  186. });
  187. data.forEach(function (dataItem) {
  188. series[dataItem[0]].data.push([dataItem[1], dataItem[2]]);
  189. });
  190. this.option.title = title
  191. this.option.singleAxis = singleAxis
  192. this.option.series = series
  193. this.chartObj.setOption(this.option);
  194. }
  195. }
  196. }
  197. },
  198. watch: {
  199. courseArray: {
  200. immediate: true,
  201. deep: true,
  202. handler(newValue, oldValue) {
  203. this.setJson(newValue)
  204. this.$forceUpdate();
  205. },
  206. },
  207. },
  208. mounted() {
  209. this.setJson(this.courseArray)
  210. var _this = this;
  211. window.addEventListener("resize", () => {
  212. if (_this.chartObj) {
  213. _this.chartObj.resize();
  214. }
  215. });
  216. },
  217. };
  218. </script>
  219. <style scoped>
  220. .data_body {
  221. height: 100%;
  222. position: relative;
  223. border-radius: 5px;
  224. margin: 0 auto;
  225. box-sizing: border-box;
  226. padding: 0;
  227. width: 95%;
  228. background: #fff;
  229. }
  230. </style>