index.vue 5.5 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. axisLabel: {
  51. formatter: function (value) {
  52. console.log(value);
  53. var ret = "";//拼接加\n返回的类目项
  54. var maxLength = 2;//每项显示文字个数
  55. var valLength = value.length;//X轴类目项的文字个数
  56. var rowN = Math.ceil(valLength / maxLength); //类目项需要换行的行数
  57. if (rowN > 1)//如果类目项的文字大于5,
  58. {
  59. for (var i = 0; i < rowN; i++) {
  60. var temp = "";//每次截取的字符串
  61. var start = i * maxLength;//开始截取的位置
  62. var end = start + maxLength;//结束截取的位置
  63. //这里也可以加一个是否是最后一行的判断,但是不加也没有影响,那就不加吧
  64. temp = value.substring(start, end) + "\n";
  65. ret += temp; //凭借最终的字符串
  66. }
  67. return ret;
  68. }
  69. else {
  70. return value;
  71. }
  72. }
  73. }
  74. },
  75. yAxis: {
  76. type: 'category',
  77. data: [
  78. '星期天', '星期一', '星期二', '星期三',
  79. '星期四', '星期五', '星期六',
  80. ],
  81. splitArea: {
  82. show: true
  83. },
  84. },
  85. visualMap: {
  86. min: 0,
  87. max: 10,
  88. calculable: true,
  89. orient: 'horizontal',
  90. inRange: {
  91. color: ['#fff', '#477cd7']
  92. },
  93. right: '10',
  94. top: '0%'
  95. },
  96. series: [
  97. {
  98. name: '',
  99. type: 'heatmap',
  100. data: [],
  101. label: {
  102. show: true
  103. },
  104. emphasis: {
  105. itemStyle: {
  106. shadowBlur: 10,
  107. shadowColor: 'rgba(0, 0, 0, 0.5)'
  108. }
  109. }
  110. }
  111. ]
  112. },
  113. };
  114. },
  115. methods: {
  116. setChart(option) {
  117. // 雷达图显示的标签
  118. let newPromise = new Promise((resolve) => {
  119. resolve();
  120. });
  121. //然后异步执行echarts的初始化函数
  122. newPromise.then(() => {
  123. const chartObj = this.$echarts.init(
  124. //劳动课程
  125. this.$el.querySelector("#charts_canvas")
  126. );
  127. this.option.xAxis.data = option.xdata;
  128. this.option.series[0].data = option.sdata;
  129. this.option.visualMap.max = option.max ? option.max : 0;
  130. // 初始化雷达图
  131. this.chartObj = chartObj;
  132. this.chartObj.setOption(this.option);
  133. });
  134. },
  135. setJson(array) {
  136. this.ooption = {
  137. xdata: [],
  138. sdata: [],
  139. max: 0
  140. }
  141. let _array = array
  142. let max = []
  143. for (var i = 0; i < _array.length; i++) {
  144. this.ooption.xdata.push(_array[i].Month + '月')
  145. this.ooption.sdata.push([i, 0, _array[i].sun])
  146. this.ooption.sdata.push([i, 1, _array[i].mon])
  147. this.ooption.sdata.push([i, 2, _array[i].tue])
  148. this.ooption.sdata.push([i, 3, _array[i].wed])
  149. this.ooption.sdata.push([i, 4, _array[i].thur])
  150. this.ooption.sdata.push([i, 5, _array[i].fri])
  151. this.ooption.sdata.push([i, 6, _array[i].sat])
  152. // let _data = [_array[i].Month-1,] //[月份,星期,值]
  153. max.push(_array[i].sun)
  154. max.push(_array[i].mon)
  155. max.push(_array[i].tue)
  156. max.push(_array[i].wed)
  157. max.push(_array[i].thur)
  158. max.push(_array[i].fri)
  159. max.push(_array[i].sat)
  160. }
  161. this.ooption.max = max.sort(function (a, b) {
  162. return b - a;
  163. })[0];
  164. if (!this.chartObj) {
  165. this.setChart(this.ooption);
  166. } else {
  167. this.option.xAxis.data = this.ooption.xdata;
  168. this.option.series[0].data = this.ooption.sdata;
  169. this.option.visualMap.max = this.ooption.max ? this.ooption.max : 0;
  170. this.chartObj.setOption(this.option);
  171. }
  172. }
  173. },
  174. watch: {
  175. yearArray: {
  176. immediate: true,
  177. deep: true,
  178. handler(newValue, oldValue) {
  179. this.setJson(newValue)
  180. this.$forceUpdate();
  181. },
  182. },
  183. },
  184. mounted() {
  185. this.setJson(this.yearArray)
  186. window.addEventListener("resize", () => {
  187. if (_this.chartObj) {
  188. _this.chartObj.resize();
  189. }
  190. });
  191. },
  192. };
  193. </script>
  194. <style scoped>
  195. .data_body {
  196. height: 100%;
  197. position: relative;
  198. border-radius: 5px;
  199. margin: 0 auto;
  200. box-sizing: border-box;
  201. padding: 0;
  202. width: 95%;
  203. background: #fff;
  204. }
  205. </style>