study.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. const app = getApp()
  2. import * as echarts from '../../../commpents/ec-canvas/echarts'
  3. Page({
  4. data: {
  5. frameStyle: { useTop: true, name: '出勤情况', leftArrow: true, useBar: false },
  6. // 学校
  7. zhSchool: '',
  8. school_id: '',
  9. schoolList: [],
  10. },
  11. // 返回
  12. back(e) {
  13. wx.navigateBack({ delta: 1 })
  14. },
  15. // 选择学校
  16. schoolChange: function (e) {
  17. const that = this;
  18. let data = that.data.schoolList[e.detail.value];
  19. if (data) {
  20. that.setData({ school_id: data.school_id });
  21. that.setData({ zhSchool: data.school_id_name });
  22. }
  23. that.watchLogin();
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. },
  30. // 监听用户是否登录
  31. watchLogin: async function () {
  32. const that = this;
  33. wx.getStorage({
  34. key: 'user',
  35. success: async res => {
  36. that.setData({
  37. ec: {
  38. onInit: that.initChart
  39. }
  40. })
  41. // 学校
  42. const school = await app.$get(`/rss`, { student_id: res.data.info.id });
  43. if (school.errcode == '0' && school.total > 0) {
  44. that.setData({ schoolList: school.data })
  45. }
  46. // 模拟请求
  47. var times = setTimeout(async function () {
  48. if (that.data.school_id) {
  49. const arr = await app.$get(`/statistics/studentLearning`, { school_id: that.data.school_id, student_id: res.data.info.id });
  50. if (arr.errcode == '0') {
  51. that.data.data1 = arr.data.signs;
  52. that.data.data2 = arr.data.minutes;
  53. }
  54. }
  55. clearTimeout(times);
  56. }, 1000)
  57. },
  58. fail: async res => {
  59. wx.redirectTo({ url: '/pages/index/index' })
  60. }
  61. })
  62. },
  63. // 折线图
  64. initChart(canvas, width, height, dpr) {
  65. let that = this;
  66. // 由于请求数据有延迟所以写一个时间函数,当数据存在的时候再执行绘制
  67. var times = setInterval(function () {
  68. var data1 = that.data.data1;
  69. var data2 = that.data.data2;
  70. if (data1 && data2) {
  71. clearInterval(times)
  72. const chart = echarts.init(canvas, null, {
  73. width: width,
  74. height: height,
  75. devicePixelRatio: dpr // new
  76. });
  77. canvas.setChart(chart);
  78. var option = {
  79. tooltip: {
  80. trigger: 'axis',
  81. axisPointer: {
  82. type: 'cross',
  83. label: {
  84. backgroundColor: '#6a7985'
  85. }
  86. }
  87. ,
  88. formatter: function (params) {
  89. let html = params[0].name + "<br>";
  90. for (let i = 0; i < params.length; i++) {
  91. html += '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:' + params[i].color + ';"></span>'
  92. if (params[i].seriesName == "签到数") {
  93. html += params[i].seriesName + ":" + params[i].value + "%<br>";
  94. }
  95. if (params[i].seriesName == "时长") {
  96. html += params[i].seriesName + ":" + params[i].value + "小时<br>";
  97. }
  98. }
  99. return html;
  100. }
  101. },
  102. legend: {
  103. textStyle: { color: '#4CA6A3' },
  104. x: 'right',
  105. data: [{ name: '签到数', icon: 'rect' }, { name: '时长', icon: 'rect' }]
  106. },
  107. grid: {
  108. left: '2%',
  109. right: '2%',
  110. containLabel: true
  111. },
  112. xAxis: [
  113. {
  114. type: 'category',
  115. boundaryGap: false,
  116. // 设置x轴线的属性
  117. axisLabel: {
  118. //x轴文字的配置
  119. show: true,
  120. interval: 0,//使x轴文字显示全
  121. },
  122. axisLine: {
  123. lineStyle: {
  124. color: '#4CA6A3', // 设置x轴字体颜色
  125. width: 2, // 设置x轴字体宽度
  126. }
  127. },
  128. data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
  129. }
  130. ],
  131. yAxis: [
  132. {
  133. type: 'value',
  134. name: '时长',
  135. position: 'left',
  136. alignTicks: true,
  137. axisLine: {
  138. show: true,
  139. lineStyle: {
  140. fontSize: 16,
  141. color: '#293C55'
  142. }
  143. },
  144. axisLabel: {
  145. formatter: '{value} 小时'
  146. }
  147. },
  148. {
  149. type: 'value',
  150. alignTicks: true,
  151. nameGap: 10,
  152. nameRotate: 0,
  153. nameTextStyle: {
  154. fontSize: 16,
  155. color: '#293C55'
  156. },
  157. splitLine: { show: false },//去除网格线
  158. min: 0,
  159. max: 100,
  160. interval: 10,
  161. // 设置轴线的属性
  162. axisLine: {
  163. show: true,
  164. lineStyle: {
  165. color: '#293C55',
  166. }
  167. }
  168. }
  169. ],
  170. series: [
  171. {
  172. name: '签到数',
  173. type: 'line',
  174. stack: '总量',
  175. areaStyle: {},
  176. itemStyle: {
  177. normal: {
  178. color: "#25DEDB",//折线点的颜色
  179. lineStyle: {
  180. color: "#25DEDB"//折线的颜色
  181. }
  182. }
  183. },
  184. areaStyle: { // 该属性设置可以使这下图区域颜色达到渐变的效果
  185. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  186. offset: 0,
  187. color: '#25DEDB'
  188. }, {
  189. offset: 1,
  190. color: '#ffe'
  191. }])
  192. },
  193. data: data1
  194. },
  195. {
  196. name: '时长',
  197. type: 'line',
  198. stack: '总量',
  199. areaStyle: {},
  200. itemStyle: {
  201. normal: {
  202. color: "#E96C44",//折线点的颜色
  203. lineStyle: {
  204. color: "#E96C44"//折线的颜色
  205. }
  206. }
  207. },
  208. areaStyle: { // 该属性设置可以使这下图区域颜色达到渐变的效果
  209. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
  210. offset: 0,
  211. color: '#E96C44'
  212. }, {
  213. offset: 1,
  214. color: '#ffe'
  215. }])
  216. },
  217. data: data2
  218. }
  219. ]
  220. };
  221. chart.setOption(option, true);
  222. return chart;
  223. }
  224. }, 100)
  225. },
  226. /**
  227. * 生命周期函数--监听页面初次渲染完成
  228. */
  229. onReady: function () { },
  230. /**
  231. * 生命周期函数--监听页面显示
  232. */
  233. onShow: function () {
  234. const that = this;
  235. // 监听用户是否登录
  236. that.watchLogin();
  237. },
  238. /**
  239. * 页面上拉触底事件的处理函数
  240. */
  241. /**
  242. * 生命周期函数--监听页面隐藏
  243. */
  244. onHide: function () {
  245. },
  246. /**
  247. * 生命周期函数--监听页面卸载
  248. */
  249. onUnload: function () {
  250. },
  251. /**
  252. * 页面相关事件处理函数--监听用户下拉动作
  253. */
  254. onPullDownRefresh: function () {
  255. },
  256. /**
  257. * 用户点击右上角分享
  258. */
  259. onShareAppMessage: function (res) {
  260. },
  261. })