index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. yearArray: {
  12. type: Array,
  13. },
  14. },
  15. data() {
  16. return {
  17. chartObj: null,
  18. ooption: {
  19. xdata: [],
  20. sdata: [],
  21. },
  22. option: {
  23. title: {
  24. text: '在线时长',
  25. textStyle:{
  26. fontSize: 12,
  27. },
  28. padding: [10, 0, 0, 10]
  29. },
  30. tooltip: {
  31. position: 'top'
  32. },
  33. grid: {
  34. top: '50',
  35. left: '5%',
  36. right: '5%',
  37. bottom: '5%',
  38. containLabel: true
  39. },
  40. xAxis: {
  41. type: 'category',
  42. // data: [
  43. // '1月', '2月', '3月', '4月', '5月', '6月', '7月',
  44. // '8月', '9月', '10月', '11月', '12月'
  45. // ],
  46. data: [],
  47. splitArea: {
  48. show: true
  49. }
  50. },
  51. yAxis: {
  52. type: 'category',
  53. data: [
  54. '星期天', '星期一', '星期二', '星期三',
  55. '星期四', '星期五', '星期六',
  56. ],
  57. splitArea: {
  58. show: true
  59. },
  60. },
  61. visualMap: {
  62. min: 0,
  63. max: 10,
  64. calculable: true,
  65. orient: 'horizontal',
  66. right: '10',
  67. top: '0%'
  68. },
  69. series: [
  70. {
  71. name: '',
  72. type: 'heatmap',
  73. data: [],
  74. label: {
  75. show: true
  76. },
  77. emphasis: {
  78. itemStyle: {
  79. shadowBlur: 10,
  80. shadowColor: 'rgba(0, 0, 0, 0.5)'
  81. }
  82. }
  83. }
  84. ]
  85. },
  86. };
  87. },
  88. methods: {
  89. setChart(option) {
  90. // 雷达图显示的标签
  91. let newPromise = new Promise((resolve) => {
  92. resolve();
  93. });
  94. //然后异步执行echarts的初始化函数
  95. newPromise.then(() => {
  96. const chartObj = this.$echarts.init(
  97. //劳动课程
  98. this.$el.querySelector("#charts_canvas")
  99. );
  100. this.option.xAxis.data = option.xdata;
  101. this.option.series[0].data = option.sdata;
  102. this.option.visualMap.max = option.max ? option.max : 0;
  103. // 初始化雷达图
  104. this.chartObj = chartObj;
  105. this.chartObj.setOption(this.option);
  106. });
  107. },
  108. },
  109. watch: {
  110. yearArray: {
  111. immediate: true,
  112. deep: true,
  113. handler(newValue, oldValue) {
  114. this.ooption = {
  115. xdata: [],
  116. sdata: [],
  117. max:0
  118. }
  119. let _array = newValue
  120. let max = []
  121. for (var i = 0; i < _array.length; i++) {
  122. this.ooption.xdata.push(_array[i].Month + '月')
  123. this.ooption.sdata.push([i, 0, _array[i].sun])
  124. this.ooption.sdata.push([i, 1, _array[i].mon])
  125. this.ooption.sdata.push([i, 2, _array[i].tue])
  126. this.ooption.sdata.push([i, 3, _array[i].wed])
  127. this.ooption.sdata.push([i, 4, _array[i].thur])
  128. this.ooption.sdata.push([i, 5, _array[i].fri])
  129. this.ooption.sdata.push([i, 6, _array[i].sat])
  130. // let _data = [_array[i].Month-1,] //[月份,星期,值]
  131. max.push(_array[i].sun)
  132. max.push(_array[i].mon)
  133. max.push(_array[i].tue)
  134. max.push(_array[i].wed)
  135. max.push(_array[i].thur)
  136. max.push(_array[i].fri)
  137. max.push(_array[i].sat)
  138. }
  139. this.ooption.max = max.sort(function(a,b){
  140. return b-a;
  141. })[0];
  142. if (!this.chartObj) {
  143. this.setChart(this.ooption);
  144. } else {
  145. this.option.xAxis.data = this.ooption.xdata;
  146. this.option.series[0].data = this.ooption.sdata;
  147. this.option.visualMap.max = this.ooption.max ? this.ooption.max : 0;
  148. this.chartObj.setOption(this.option);
  149. }
  150. this.$forceUpdate();
  151. },
  152. },
  153. },
  154. mounted() {
  155. this.ooption = {
  156. xdata: [],
  157. sdata: [],
  158. max:0
  159. }
  160. let _array = this.yearArray
  161. let max = []
  162. for (var i = 0; i < _array.length; i++) {
  163. this.ooption.xdata.push(_array[i].Month + '月')
  164. this.ooption.sdata.push([i, 0, _array[i].sun])
  165. this.ooption.sdata.push([i, 1, _array[i].mon])
  166. this.ooption.sdata.push([i, 2, _array[i].tue])
  167. this.ooption.sdata.push([i, 3, _array[i].wed])
  168. this.ooption.sdata.push([i, 4, _array[i].thur])
  169. this.ooption.sdata.push([i, 5, _array[i].fri])
  170. this.ooption.sdata.push([i, 6, _array[i].sat])
  171. // let _data = [_array[i].Month-1,] //[月份,星期,值]
  172. max.push(_array[i].sun)
  173. max.push(_array[i].mon)
  174. max.push(_array[i].tue)
  175. max.push(_array[i].wed)
  176. max.push(_array[i].thur)
  177. max.push(_array[i].fri)
  178. max.push(_array[i].sat)
  179. }
  180. this.ooption.max = max.sort(function(a,b){
  181. return b-a;
  182. })[0];
  183. this.setChart(this.ooption);
  184. var _this = this;
  185. window.addEventListener("resize", () => {
  186. if (_this.chartObj) {
  187. _this.chartObj.resize();
  188. }
  189. });
  190. },
  191. };
  192. </script>
  193. <style scoped>
  194. .data_body {
  195. height: 100%;
  196. position: relative;
  197. border-radius: 5px;
  198. margin: 0 auto;
  199. box-sizing: border-box;
  200. padding: 0;
  201. width: 95%;
  202. background: #fff;
  203. }
  204. </style>