index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // nova-werun/pages/home/statistics/index.js
  2. import * as echarts from "../../../components/ec-canvas/echarts.min"
  3. const Parse = getApp().Parse;
  4. const company = getApp().globalData.company;
  5. function initChart(canvas, width, height, dpr) {
  6. const chart = echarts.init(canvas, null, {
  7. width: width,
  8. height: height,
  9. devicePixelRatio: dpr
  10. });
  11. canvas.setChart(chart);
  12. var option = {
  13. xAxis: {
  14. type: 'category',
  15. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  16. axisLabel: {
  17. formatter: function(value, index) {
  18. const dates = ['1号', '2号', '3号', '4号', '5号', '6号', '7号'];
  19. return index === 1 ? '今天' : value + '\n'+'\n' + dates[index];
  20. }
  21. }
  22. },
  23. yAxis: {
  24. type: 'value',
  25. max: 20000, // 设置Y轴最大值为20000
  26. axisLabel: {
  27. formatter: function(value) {
  28. if (value >= 10000) {
  29. return (value / 10000) + 'k'; // 格式化为20k
  30. }
  31. return value;
  32. }
  33. }
  34. },
  35. series: [
  36. {
  37. data: [
  38. 12000,
  39. {
  40. value: 12000,
  41. itemStyle: {
  42. color: '#a90000',
  43. }
  44. },
  45. 12000,
  46. 12000,
  47. 12000,
  48. 12000,
  49. 12000
  50. ],
  51. type: 'bar',
  52. barWidth:'10',
  53. itemStyle: {
  54. borderRadius:15,
  55. }
  56. }
  57. ],
  58. };
  59. chart.setOption(option);
  60. return chart;
  61. }
  62. Page({
  63. /**
  64. * 页面的初始数据
  65. */
  66. data: {
  67. //屏幕高度
  68. statusBarHeight: 0, // 状态栏高度
  69. screenHeight: 0, // 屏幕高度
  70. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  71. bottomNavHeight: 0, // 底部导航栏高度
  72. contentHeight: 0, // 可用内容高度
  73. contentHeight2: 0,
  74. contentpadding: 0, //顶部padding高度
  75. active: 0,
  76. //
  77. day: '7',
  78. target: '',
  79. sharList: [],
  80. //生涯数据
  81. stardate: '',
  82. daysDifference: "",
  83. totalSteps: 0,
  84. totalDistance: 0,
  85. totalBurnCalories: 0,
  86. totalsportDate: 0,
  87. ec: {
  88. onInit: initChart
  89. },
  90. percentage:'',
  91. percent:'',
  92. },
  93. /**
  94. * 生命周期函数--监听页面加载
  95. */
  96. onLoad: function (options) {
  97. // 计算
  98. const systemInfo = wx.getSystemInfoSync();
  99. const statusBarHeight = systemInfo.statusBarHeight || 0;
  100. const screenHeight = systemInfo.screenHeight || 0;
  101. const custom = wx.getMenuButtonBoundingClientRect();
  102. const customHeight = custom.height + 10 + 2 || 0;
  103. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  104. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  105. const contentHeight = (screenHeight - 50 - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  106. this.setData({
  107. statusBarHeight,
  108. screenHeight,
  109. customHeight,
  110. bottomNavHeight,
  111. contentpadding,
  112. contentHeight
  113. });
  114. this.gettarget()
  115. this.order()
  116. this.allorder()
  117. },
  118. /**
  119. * 生命周期函数--监听页面初次渲染完成
  120. */
  121. onReady: function () {
  122. },
  123. /**
  124. * 生命周期函数--监听页面显示
  125. */
  126. onShow: function () {
  127. },
  128. /**
  129. * 生命周期函数--监听页面隐藏
  130. */
  131. onHide: function () {
  132. },
  133. /**
  134. * 生命周期函数--监听页面卸载
  135. */
  136. onUnload: function () {
  137. },
  138. /**
  139. * 页面相关事件处理函数--监听用户下拉动作
  140. */
  141. onPullDownRefresh: function () {
  142. },
  143. /**
  144. * 页面上拉触底事件的处理函数
  145. */
  146. onReachBottom: function () {
  147. },
  148. /**
  149. * 用户点击右上角分享
  150. */
  151. onShareAppMessage: function () {
  152. },
  153. //底部tab栏修改
  154. onChange(event) {
  155. this.setData({
  156. active: event.detail
  157. });
  158. },
  159. //修改30天或者7天数据
  160. changeday() {
  161. if (this.data.day == '7') {
  162. this.setData({
  163. day: '30'
  164. })
  165. } else {
  166. this.setData({
  167. day: '7'
  168. })
  169. }
  170. },
  171. //获取目标步数
  172. async gettarget() {
  173. const currentUser = Parse.User.current();
  174. let userquery = new Parse.Query('_User');
  175. userquery.equalTo('company', company);
  176. userquery.equalTo('objectId', currentUser.id);
  177. userquery.notEqualTo('isDeleted', true)
  178. let user = await userquery.find();
  179. let num = user.map(item => item.toJSON());
  180. if (num[0].num) {
  181. this.setData({
  182. target: num[0].num
  183. })
  184. console.log('当前步数', this.data.target);
  185. }
  186. },
  187. //获取当天运动数据
  188. async order() {
  189. const currentUser = Parse.User.current();
  190. let ActivityDataquery = new Parse.Query('ActivityData');
  191. ActivityDataquery.equalTo('user', currentUser.id);
  192. ActivityDataquery.equalTo('company', company);
  193. ActivityDataquery.equalTo('type', 'today');
  194. ActivityDataquery.notEqualTo('isDeleted', true);
  195. // 获取今天的日期
  196. const today = new Date();
  197. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  198. const todayEnd = new Date(todayStart);
  199. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  200. // 在查询条件中添加对 createdAt 的限制
  201. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  202. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  203. ActivityDataquery.include('user');
  204. let r = await ActivityDataquery.find();
  205. let sharList = r.map(item => item.toJSON());
  206. this.setData({
  207. sharList
  208. });
  209. this.getBackgroundColor()
  210. console.log(this.data.sharList);
  211. },
  212. //获取生涯运动数据
  213. async allorder() {
  214. const currentUser = Parse.User.current();
  215. let ActivityDataquery = new Parse.Query('ActivityData');
  216. ActivityDataquery.equalTo('user', currentUser.id);
  217. ActivityDataquery.equalTo('company', company);
  218. ActivityDataquery.equalTo('type', 'today');
  219. ActivityDataquery.notEqualTo('isDeleted', true);
  220. let r = await ActivityDataquery.find();
  221. let allList = r.map(item => item.toJSON());
  222. // 根据 createdAt 日期从以前到现在排列
  223. allList.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
  224. // 获取最早的 createdAt 日期
  225. const earliestDate = new Date(allList[0].createdAt);
  226. // 格式化为 YYYY年MM月DD日
  227. const formattedDate = `${earliestDate.getFullYear()}年${String(earliestDate.getMonth() + 1).padStart(2, '0')}月${String(earliestDate.getDate()).padStart(2, '0')}日`;
  228. // 计算从最早日期到今天的天数
  229. const today = new Date();
  230. // 清零时间部分
  231. earliestDate.setHours(0, 0, 0, 0); // 将最早日期的时间部分清零
  232. today.setHours(0, 0, 0, 0); // 将今天的时间部分清零
  233. // 计算日期差值
  234. const timeDifference = today - earliestDate; // 时间差(毫秒)
  235. const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24)); // 转换为天数
  236. // 计算 steps、distance 和 burnCalories 的总和
  237. let totalSteps = 0;
  238. let totalDistance = 0;
  239. let totalBurnCalories = 0;
  240. let totalsportDate = 0;
  241. allList.forEach(item => {
  242. totalSteps += Number(item.steps) || 0; // 确保为数字类型,避免 NaN
  243. totalDistance += Number(item.distance) || 0; // 确保为数字类型,避免 NaN
  244. totalBurnCalories += Number(item.burnCalories) || 0; // 确保为数字类型,避免 NaN
  245. totalsportDate += Number(item.sportDate) || 0;
  246. });
  247. // 将总运动时间转换为天、小时、分钟
  248. const days = Math.floor(totalsportDate / (60 * 24)); // 转换为天数
  249. const hours = Math.floor((totalsportDate % (60 * 24)) / 60); // 剩余小时
  250. const minutes = totalsportDate % 60; // 剩余分钟
  251. // 设置 stardate 和 daysDifference
  252. this.setData({
  253. stardate: formattedDate,
  254. daysDifference: daysDifference,
  255. totalSteps,
  256. totalDistance,
  257. totalBurnCalories,
  258. totalsportDate: `${days}天 ${hours}小时 ${minutes}分钟` // 格式化为字符串
  259. });
  260. console.log(allList, this.data.totalsportDate);
  261. },
  262. //修改光圈
  263. getBackgroundColor() {
  264. const steps = this.data.sharList[0].steps || 0;
  265. let percent = (steps / this.data.target) * 100;
  266. if (percent > 100) {
  267. percent = 100;
  268. }
  269. this.setData({
  270. percent,
  271. percentage:`conic-gradient(from 0deg, #015EEA ${percent}%, white 0%)`,
  272. })
  273. console.log('百分比',this.data.percentage);
  274. }
  275. })