index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <div class="data_body">
  3. <div style="width: 100%; height: 100%" :style="{ minWidth: ooption.hours.length * 75 + 'px' }">
  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. confine: true,
  27. formatter: function (params) {
  28. // console.log(params);
  29. return params.marker + params.name + ' ' + params.data[2];//params.seriesName + '<br>' + params.
  30. }
  31. },
  32. grid: {
  33. top: '5%',
  34. left: 0,
  35. bottom: '5%',
  36. right: '5%',
  37. containLabel: true
  38. },
  39. xAxis: {
  40. type: 'category',
  41. data: [],
  42. boundaryGap: false,
  43. splitLine: {
  44. show: true
  45. },
  46. axisLine: {
  47. show: false
  48. }
  49. },
  50. yAxis: {
  51. type: 'category',
  52. data: [],
  53. axisLine: {
  54. show: false
  55. }
  56. },
  57. series: [
  58. {
  59. name: 'Punch Card',
  60. type: 'scatter',
  61. symbolSize: function (val) {
  62. return val[2] * 3;
  63. },
  64. data: [],
  65. animationDelay: function (idx) {
  66. return idx * 5;
  67. },
  68. itemStyle: {
  69. color: function(params) {
  70. // 根据行索引设置不同的颜色
  71. var row = params.value[1];
  72. var colorList = ['hsl(170, 80%, 75%)', 'hsl(300, 99%, 70%)', 'hsl(139, 93%, 60%)', 'hsl(56, 73%, 69%)', 'hsl(352, 98%, 69%)', 'hsl(288, 76%, 66%)', 'hsl(167, 89%, 60%)', 'hsl(83, 81%, 74%)', 'hsl(359, 84%, 66%)', 'hsl(208, 96%, 66%)', 'hsl(259, 82%, 62%)', 'hsl(224, 95%, 64%)', 'hsl(119, 89%, 71%)', 'hsl(35, 75%, 70%)', 'hsl(48, 73%, 76%)', 'hsl(10, 73%, 69%)', 'hsl(112, 88%, 71%)', 'hsl(90, 87%, 61%)', 'hsl(37, 83%, 66%)', 'hsl(192, 86%, 72%)'];
  73. return colorList[row % colorList.length];
  74. }
  75. }
  76. }
  77. ]
  78. // title: [],
  79. // singleAxis: [],
  80. // series: []
  81. },
  82. };
  83. },
  84. methods: {
  85. setChart(option) {
  86. // 雷达图显示的标签
  87. let newPromise = new Promise((resolve) => {
  88. resolve();
  89. });
  90. //然后异步执行echarts的初始化函数
  91. newPromise.then(() => {
  92. const chartObj = this.$echarts.init(
  93. //劳动课程
  94. this.$el.querySelector("#charts_canvas")
  95. );
  96. const hours = option.hours
  97. // [
  98. // // '语文', '数学', '英语', '科学', '体育', '音乐', '美术',
  99. // // '劳动', '其他',
  100. // ];
  101. // prettier-ignore
  102. const days = option.days
  103. // [
  104. // // '一年级', '二年级', '三年级', '四年级', '五年级', '六年级'
  105. // ];
  106. // prettier-ignore
  107. const data = option.data
  108. // [
  109. // // [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],
  110. // // [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],
  111. // // [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],
  112. // // [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],
  113. // // [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],
  114. // // [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],
  115. // ];
  116. const title = [];
  117. const singleAxis = [];
  118. const series = [];
  119. // days.forEach(function (day, idx) {
  120. // title.push({
  121. // textBaseline: 'middle',
  122. // top: ((idx + 0.5) * 90) / days.length + '%',
  123. // text: day,
  124. // textStyle: {
  125. // fontSize: 12,
  126. // },
  127. // });
  128. // singleAxis.push({
  129. // left: 70,
  130. // type: 'category',
  131. // boundaryGap: false,
  132. // data: hours,
  133. // top: (idx * 90) / days.length + 5 + '%',
  134. // height: 90 / days.length - 10 + '%'
  135. // });
  136. // series.push({
  137. // singleAxisIndex: idx,
  138. // coordinateSystem: 'singleAxis',
  139. // type: 'scatter',
  140. // data: [],
  141. // symbolSize: function (dataItem) {
  142. // return dataItem[1] * 3;
  143. // }
  144. // });
  145. // });
  146. // data.forEach(function (dataItem) {
  147. // series[dataItem[0]].data.push([dataItem[1], dataItem[2]]);
  148. // });
  149. this.option.xAxis.data = hours
  150. this.option.yAxis.data = days
  151. this.option.series[0].data = data.map(function (item) {
  152. return [item[1], item[0], item[2]];
  153. })
  154. // this.option.title = title
  155. // this.option.singleAxis = singleAxis
  156. // this.option.series = series
  157. // 初始化雷达图
  158. this.chartObj = chartObj;
  159. this.chartObj.setOption(this.option);
  160. });
  161. },
  162. setJson(array) {
  163. if (array != undefined && array.length > 0) {
  164. this.ooption = {
  165. hours: [],
  166. days: [],
  167. data: [],
  168. }
  169. let _grade = []
  170. let _subject = []
  171. let data = []
  172. array.forEach(function (item, idx) {
  173. _grade.push(item.name)
  174. })
  175. array[0].subject.forEach(function (item, idx) {
  176. _subject.push(item.name)
  177. })
  178. array.forEach(function (item, idx) {
  179. item.subject.forEach(function (item2, idx2) {
  180. data.push([idx, idx2, item2.course])
  181. })
  182. })
  183. this.ooption.hours = _subject
  184. this.ooption.days = _grade
  185. this.ooption.data = data
  186. if (!this.chartObj) {
  187. this.setChart(this.ooption);
  188. } else {
  189. const hours = this.ooption.hours;
  190. // prettier-ignore
  191. const days = this.ooption.days;
  192. // prettier-ignore
  193. const data = this.ooption.data;
  194. const title = [];
  195. const singleAxis = [];
  196. const series = [];
  197. // days.forEach(function (day, idx) {
  198. // title.push({
  199. // textBaseline: 'middle',
  200. // top: ((idx + 0.5) * 90) / days.length + '%',
  201. // text: day,
  202. // textStyle: {
  203. // fontSize: 12,
  204. // },
  205. // });
  206. // singleAxis.push({
  207. // left: 70,
  208. // type: 'category',
  209. // boundaryGap: false,
  210. // data: hours,
  211. // top: (idx * 90) / days.length + 5 + '%',
  212. // height: 90 / days.length - 10 + '%'
  213. // });
  214. // series.push({
  215. // singleAxisIndex: idx,
  216. // coordinateSystem: 'singleAxis',
  217. // type: 'scatter',
  218. // data: [],
  219. // symbolSize: function (dataItem) {
  220. // return dataItem[1] * 3;
  221. // }
  222. // });
  223. // });
  224. // data.forEach(function (dataItem) {
  225. // series[dataItem[0]].data.push([dataItem[1], dataItem[2]]);
  226. // });
  227. this.option.xAxis.data = hours
  228. this.option.yAxis.data = days
  229. this.option.series[0].data = data.map(function (item) {
  230. return [item[1], item[0], item[2]];
  231. })
  232. // this.option.title = title
  233. // this.option.singleAxis = singleAxis
  234. // this.option.series = series
  235. this.chartObj.setOption(this.option);
  236. }
  237. }
  238. }
  239. },
  240. watch: {
  241. courseArray: {
  242. immediate: true,
  243. deep: true,
  244. handler(newValue, oldValue) {
  245. // newValue = newValue.filter(item => {
  246. // return ['一年级','二年级','三年级','四年级','五年级','六年级','七年级'].indexOf(item.name) !== -1
  247. // })
  248. this.setJson(newValue)
  249. this.$forceUpdate();
  250. },
  251. },
  252. },
  253. mounted() {
  254. // this.courseArray = this.courseArray.filter(item => {
  255. // return ['一年级','二年级','三年级','四年级','五年级','六年级','七年级'].indexOf(item.name) !== -1
  256. // })
  257. this.setJson(this.courseArray)
  258. var _this = this;
  259. window.addEventListener("resize", () => {
  260. if (_this.chartObj) {
  261. _this.chartObj.resize();
  262. }
  263. });
  264. },
  265. };
  266. </script>
  267. <style scoped>
  268. .data_body {
  269. height: 100%;
  270. position: relative;
  271. border-radius: 5px;
  272. margin: 0 auto;
  273. box-sizing: border-box;
  274. padding: 0;
  275. width: 95%;
  276. background: #fff;
  277. overflow: auto;
  278. }
  279. </style>