index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. setChart(option) {
  126. // 雷达图显示的标签
  127. let newPromise = new Promise((resolve) => {
  128. resolve();
  129. });
  130. //然后异步执行echarts的初始化函数
  131. newPromise.then(() => {
  132. const chartObj = this.$echarts.init(
  133. //劳动课程
  134. this.$el.querySelector("#charts_canvas")
  135. );
  136. const hours =
  137. [
  138. '1班', '2班', '3班', '4班', '5班', '6班'
  139. ];
  140. // prettier-ignore
  141. const days =
  142. [
  143. '一年级', '二年级', '三年级', '四年级', '五年级', '六年级'
  144. ];
  145. // prettier-ignore
  146. const data =
  147. [
  148. [0, 0, 34], [0, 1, 35], [0, 2, 40], [0, 3, 33], [0, 4, 35], [0, 5, 35],
  149. [1, 0, 34], [1, 1, 30], [1, 2, 35], [1, 3, 41], [1, 4, 42], [1, 5, 37],
  150. [2, 0, 45], [2, 1, 35], [2, 2, 44], [2, 3, 45], [2, 4, 41], [2, 5, 35],
  151. [3, 0, 41], [3, 1, 44], [3, 2, 43], [3, 3, 39], [3, 4, 42], [3, 5, 42],
  152. [4, 0, 34], [4, 1, 36], [4, 2, 40], [4, 3, 0], [4, 4, 0], [4, 5, 0],
  153. [5, 0, 44], [5, 1, 44], [5, 2, 34], [5, 3, 0], [5, 4, 0], [5, 5, 0],
  154. ];
  155. const title = [];
  156. const singleAxis = [];
  157. const series = [];
  158. days.forEach(function (day, idx) {
  159. title.push({
  160. textBaseline: 'middle',
  161. top: ((idx + 0.5) * 90) / days.length + '%',
  162. text: day,
  163. textStyle: {
  164. fontSize: 12,
  165. },
  166. });
  167. singleAxis.push({
  168. left: 70,
  169. type: 'category',
  170. boundaryGap: false,
  171. data: hours,
  172. top: (idx * 90) / days.length + 5 + '%',
  173. height: 90 / days.length - 10 + '%'
  174. });
  175. series.push({
  176. singleAxisIndex: idx,
  177. coordinateSystem: 'singleAxis',
  178. type: 'scatter',
  179. data: [],
  180. symbolSize: function (dataItem) {
  181. return dataItem[1];
  182. }
  183. });
  184. });
  185. data.forEach(function (dataItem) {
  186. series[dataItem[0]].data.push([dataItem[1], dataItem[2]]);
  187. });
  188. this.option.title = title
  189. this.option.singleAxis = singleAxis
  190. this.option.series = series
  191. // 初始化雷达图
  192. this.chartObj = chartObj;
  193. this.chartObj.setOption(this.option);
  194. });
  195. },
  196. setJson(array) {
  197. this.setChart(this.ooption);
  198. if (array != undefined && array.length > 0) {
  199. this.ooption = {
  200. hours: [],
  201. days: [],
  202. data: [],
  203. }
  204. let _grade = []
  205. let _subject = []
  206. let data = []
  207. array.forEach(function (item, idx) {
  208. _grade.push(item.name)
  209. })
  210. array[0].subject.forEach(function (item, idx) {
  211. _subject.push(item.name)
  212. })
  213. array.forEach(function (item, idx) {
  214. item.subject.forEach(function (item2, idx2) {
  215. data.push([idx, idx2, item2.course])
  216. })
  217. })
  218. this.ooption.hours = _subject
  219. this.ooption.days = _grade
  220. this.ooption.data = data
  221. if (!this.chartObj) {
  222. this.setChart(this.ooption);
  223. } else {
  224. const hours = this.ooption.hours;
  225. // prettier-ignore
  226. const days = this.ooption.days;
  227. // prettier-ignore
  228. const data = this.ooption.data;
  229. const title = [];
  230. const singleAxis = [];
  231. const series = [];
  232. days.forEach(function (day, idx) {
  233. title.push({
  234. textBaseline: 'middle',
  235. top: ((idx + 0.5) * 90) / days.length + '%',
  236. text: day,
  237. textStyle: {
  238. fontSize: 12,
  239. },
  240. });
  241. singleAxis.push({
  242. left: 70,
  243. type: 'category',
  244. boundaryGap: false,
  245. data: hours,
  246. top: (idx * 90) / days.length + 5 + '%',
  247. height: 90 / days.length - 10 + '%'
  248. });
  249. series.push({
  250. singleAxisIndex: idx,
  251. coordinateSystem: 'singleAxis',
  252. type: 'scatter',
  253. data: [],
  254. symbolSize: function (dataItem) {
  255. return dataItem[1] * 3;
  256. }
  257. });
  258. });
  259. data.forEach(function (dataItem) {
  260. series[dataItem[0]].data.push([dataItem[1], dataItem[2]]);
  261. });
  262. this.option.title = title
  263. this.option.singleAxis = singleAxis
  264. this.option.series = series
  265. this.chartObj.setOption(this.option);
  266. }
  267. }
  268. }
  269. },
  270. watch: {
  271. courseArray: {
  272. immediate: true,
  273. deep: true,
  274. handler(newValue, oldValue) {
  275. // newValue = newValue.filter(item => {
  276. // return ['一年级','二年级','三年级','四年级','五年级','六年级','七年级'].indexOf(item.name) !== -1
  277. // })
  278. this.setJson(newValue)
  279. this.$forceUpdate();
  280. },
  281. },
  282. },
  283. mounted() {
  284. // this.courseArray = this.courseArray.filter(item => {
  285. // return ['一年级','二年级','三年级','四年级','五年级','六年级','七年级'].indexOf(item.name) !== -1
  286. // })
  287. this.setJson(this.courseArray)
  288. var _this = this;
  289. window.addEventListener("resize", () => {
  290. if (_this.chartObj) {
  291. _this.chartObj.resize();
  292. }
  293. });
  294. },
  295. };
  296. </script>
  297. <style scoped>
  298. .data_body {
  299. height: 100%;
  300. position: relative;
  301. border-radius: 5px;
  302. margin: 0 auto;
  303. box-sizing: border-box;
  304. padding: 0;
  305. width: 95%;
  306. background: #fff;
  307. }
  308. </style>