index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. //屏幕高度
  11. statusBarHeight: 0, // 状态栏高度
  12. screenHeight: 0, // 屏幕高度
  13. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  14. bottomNavHeight: 0, // 底部导航栏高度
  15. contentHeight: 0, // 可用内容高度
  16. contentHeight2: 0,
  17. contentpadding: 0, //顶部padding高度
  18. active: 0,
  19. //
  20. day: '7',
  21. target: '',
  22. sharList: [],
  23. //生涯数据
  24. stardate: '',
  25. daysDifference: "",
  26. totalSteps: 0,
  27. totalDistance: 0,
  28. totalBurnCalories: 0,
  29. totalsportDate: 0,
  30. ec: {
  31. onInit: null
  32. },
  33. resultList: [],
  34. stepsData: [], //统计图柱状
  35. xAxis: [], //x轴
  36. percentage: '',
  37. percent: '',
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: async function (options) {
  43. // 计算
  44. const systemInfo = wx.getSystemInfoSync();
  45. const statusBarHeight = systemInfo.statusBarHeight || 0;
  46. const screenHeight = systemInfo.screenHeight || 0;
  47. const custom = wx.getMenuButtonBoundingClientRect();
  48. const customHeight = custom.height + 10 + 2 || 0;
  49. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  50. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  51. const contentHeight = (screenHeight - 50 - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  52. this.setData({
  53. statusBarHeight,
  54. screenHeight,
  55. customHeight,
  56. bottomNavHeight,
  57. contentpadding,
  58. contentHeight
  59. });
  60. await this.gettarget()
  61. await this.order()
  62. this.allorder()
  63. await this.getweekday()
  64. this.echartsComponnet = this.selectComponent('#mychart')
  65. await this.initChart()
  66. },
  67. //图表
  68. // 将 initChart 定义在 Page 上下文中
  69. initChart() {
  70. if (this.chart) {
  71. this.chart.clear();
  72. }
  73. this.echartsComponnet.init((canvas, width, height, dpr) => {
  74. // 初始化图表
  75. this.chart = echarts.init(canvas, null, {
  76. width: width,
  77. height: height,
  78. devicePixelRatio: dpr // 清晰度 使canvas的图表更加清晰
  79. });
  80. this.chart.setOption(this.getOption())
  81. return this.chart;
  82. });
  83. },
  84. getOption() {
  85. var option = {
  86. xAxis: {
  87. type: 'category',
  88. data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  89. axisLabel: {
  90. formatter: (value, index) => this.formatter(value, index)
  91. }
  92. },
  93. yAxis: {
  94. type: 'value',
  95. max: 20000, // 设置Y轴最大值为20000
  96. axisLabel: {
  97. formatter: function (value) {
  98. if (value >= 10000) {
  99. return (value / 1000) + 'k'; // 格式化为20k
  100. }
  101. return value;
  102. }
  103. }
  104. },
  105. series: [{
  106. // data: [
  107. // 12000,
  108. // {
  109. // value: 12000,
  110. // itemStyle: {
  111. // color: '#a90000',
  112. // }
  113. // },
  114. // 12000,
  115. // 12000,
  116. // 12000,
  117. // 12000,
  118. // 12000
  119. // ],
  120. data: this.data.stepsData,
  121. type: 'bar',
  122. barWidth: '10',
  123. itemStyle: {
  124. borderRadius: 15,
  125. }
  126. }],
  127. };
  128. return option; // 返回 option 对象
  129. },
  130. // 返回x轴底下的字
  131. formatter(value, index) {
  132. let daylist = this.data.resultList.map(item => {
  133. const day = new Date(item.createdAt); // 获取当前日期
  134. const dayOfMonth = day.getDate(); // 获取今天是几号
  135. return dayOfMonth
  136. })
  137. daylist.reverse()
  138. const dayindex = daylist.length-1
  139. if (daylist.length < 7) {
  140. for (let i = daylist.length; i < 7; i++) {
  141. const lastDay = daylist[daylist.length - 1];
  142. daylist.push(lastDay + (i - daylist.length + 1)); // 补充后续日期
  143. }
  144. }
  145. const dates = daylist.map(item=>{
  146. return `${item}号`
  147. })
  148. console.log('dates',dates);
  149. return index === dayindex ? '今天' : value + '\n' + '\n' + dates[index];
  150. },
  151. /**
  152. * 生命周期函数--监听页面初次渲染完成
  153. */
  154. onReady: function () {
  155. },
  156. /**
  157. * 生命周期函数--监听页面显示
  158. */
  159. onShow: function () {
  160. },
  161. /**
  162. * 生命周期函数--监听页面隐藏
  163. */
  164. onHide: function () {
  165. },
  166. /**
  167. * 生命周期函数--监听页面卸载
  168. */
  169. onUnload: function () {
  170. },
  171. /**
  172. * 页面相关事件处理函数--监听用户下拉动作
  173. */
  174. onPullDownRefresh: function () {
  175. },
  176. /**
  177. * 页面上拉触底事件的处理函数
  178. */
  179. onReachBottom: function () {
  180. },
  181. /**
  182. * 用户点击右上角分享
  183. */
  184. onShareAppMessage: function () {
  185. },
  186. //底部tab栏修改
  187. onChange(event) {
  188. this.setData({
  189. active: event.detail
  190. });
  191. this.echartsComponnet = this.selectComponent('#mychart')
  192. this.initChart()
  193. },
  194. //修改30天或者7天数据
  195. changeday() {
  196. if (this.data.day == '7') {
  197. this.setData({
  198. day: '30'
  199. })
  200. } else {
  201. this.setData({
  202. day: '7'
  203. })
  204. }
  205. },
  206. //获取目标步数
  207. async gettarget() {
  208. const currentUser = Parse.User.current();
  209. let userquery = new Parse.Query('_User');
  210. userquery.equalTo('company', company);
  211. userquery.equalTo('objectId', currentUser.id);
  212. userquery.notEqualTo('isDeleted', true)
  213. let user = await userquery.find();
  214. let num = user.map(item => item.toJSON());
  215. if (num[0].num) {
  216. this.setData({
  217. target: num[0].num
  218. })
  219. console.log('当前步数', this.data.target);
  220. }
  221. },
  222. //获取当天运动数据
  223. async order() {
  224. const currentUser = Parse.User.current();
  225. let ActivityDataquery = new Parse.Query('ActivityData');
  226. ActivityDataquery.equalTo('user', currentUser.id);
  227. ActivityDataquery.equalTo('company', company);
  228. ActivityDataquery.equalTo('type', 'today');
  229. ActivityDataquery.notEqualTo('isDeleted', true);
  230. // 获取今天的日期
  231. const today = new Date();
  232. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  233. const todayEnd = new Date(todayStart);
  234. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  235. // 在查询条件中添加对 createdAt 的限制
  236. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  237. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  238. ActivityDataquery.include('user');
  239. let r = await ActivityDataquery.find();
  240. let sharList = r.map(item => item.toJSON());
  241. this.setData({
  242. sharList
  243. });
  244. this.getBackgroundColor()
  245. console.log(this.data.sharList);
  246. },
  247. //获取生涯运动数据
  248. async allorder() {
  249. const currentUser = Parse.User.current();
  250. let ActivityDataquery = new Parse.Query('ActivityData');
  251. ActivityDataquery.equalTo('user', currentUser.id);
  252. ActivityDataquery.equalTo('company', company);
  253. ActivityDataquery.equalTo('type', 'today');
  254. ActivityDataquery.notEqualTo('isDeleted', true);
  255. let r = await ActivityDataquery.find();
  256. let allList = r.map(item => item.toJSON());
  257. // 根据 createdAt 日期从以前到现在排列
  258. allList.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
  259. // 获取最早的 createdAt 日期
  260. const earliestDate = new Date(allList[0].createdAt);
  261. // 格式化为 YYYY年MM月DD日
  262. const formattedDate = `${earliestDate.getFullYear()}年${String(earliestDate.getMonth() + 1).padStart(2, '0')}月${String(earliestDate.getDate()).padStart(2, '0')}日`;
  263. // 计算从最早日期到今天的天数
  264. const today = new Date();
  265. // 清零时间部分
  266. earliestDate.setHours(0, 0, 0, 0); // 将最早日期的时间部分清零
  267. today.setHours(0, 0, 0, 0); // 将今天的时间部分清零
  268. // 计算日期差值
  269. const timeDifference = today - earliestDate; // 时间差(毫秒)
  270. const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24)); // 转换为天数
  271. // 计算 steps、distance 和 burnCalories 的总和
  272. let totalSteps = 0;
  273. let totalDistance = 0;
  274. let totalBurnCalories = 0;
  275. let totalsportDate = 0;
  276. allList.forEach(item => {
  277. totalSteps += Number(item.steps) || 0; // 确保为数字类型,避免 NaN
  278. totalDistance += Number(item.distance) || 0; // 确保为数字类型,避免 NaN
  279. totalBurnCalories += Number(item.burnCalories) || 0; // 确保为数字类型,避免 NaN
  280. totalsportDate += Number(item.sportDate) || 0;
  281. });
  282. // 将总运动时间转换为天、小时、分钟
  283. const days = Math.floor(totalsportDate / (60 * 24)); // 转换为天数
  284. const hours = Math.floor((totalsportDate % (60 * 24)) / 60); // 剩余小时
  285. const minutes = totalsportDate % 60; // 剩余分钟
  286. // 设置 stardate 和 daysDifference
  287. this.setData({
  288. stardate: formattedDate,
  289. daysDifference: daysDifference,
  290. totalSteps,
  291. totalDistance,
  292. totalBurnCalories,
  293. totalsportDate: `${days}天 ${hours}小时 ${minutes}分钟` // 格式化为字符串
  294. });
  295. console.log(allList, this.data.totalsportDate);
  296. },
  297. //修改光圈
  298. getBackgroundColor() {
  299. const steps = this.data.sharList[0].steps || 0;
  300. let percent = (steps / this.data.target) * 100;
  301. console.log('目标', percent);
  302. if (percent > 100) {
  303. percent = 100;
  304. }
  305. // 保留两位小数
  306. percent = parseFloat(percent.toFixed(2));
  307. this.setData({
  308. percent,
  309. percentage: `conic-gradient(from 0deg, #015EEA ${percent}%, white 0%)`,
  310. })
  311. console.log('百分比', this.data.percentage);
  312. },
  313. //获取本周记录
  314. async getweekday() {
  315. const currentUser = Parse.User.current();
  316. let ActivityDataquery = new Parse.Query('ActivityData');
  317. ActivityDataquery.equalTo('user', currentUser.id);
  318. ActivityDataquery.equalTo('company', company);
  319. ActivityDataquery.equalTo('type', 'today');
  320. ActivityDataquery.notEqualTo('isDeleted', true);
  321. let r = await ActivityDataquery.find();
  322. let allList = r.map(item => item.toJSON());
  323. // 按时间降序排序(从现在到以前)
  324. allList.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
  325. // 获取今天是星期几(0 = 周日, 1 = 周一, ..., 6 = 周六)
  326. const today = new Date();
  327. const dayOfWeek = today.getDay(); // 获取当前星期几
  328. // 根据星期几决定取多少条数据
  329. let numItemsToTake;
  330. if (dayOfWeek === 0) {
  331. numItemsToTake = 7; // 周日取7条数据
  332. } else {
  333. numItemsToTake = dayOfWeek; // 周一至周六取对应条数
  334. }
  335. // 取出对应数量的数据
  336. const resultList = allList.slice(0, numItemsToTake);
  337. const stepsData = resultList.map((item, index) => {
  338. if (index === 0) {
  339. // 如果是第一条数据,设置特殊样式
  340. return {
  341. value: item.steps,
  342. itemStyle: {
  343. color: '#a90000',
  344. }
  345. };
  346. } else {
  347. // 其他数据直接返回 steps
  348. return item.steps;
  349. }
  350. });
  351. this.setData({
  352. resultList,
  353. stepsData
  354. })
  355. console.log('resultList', resultList);
  356. },
  357. })