index.vue 13 KB

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