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