index.js 9.5 KB

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