BMI_history.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <template>
  2. <view v-if="bmi_history" class="content">
  3. <!-- 折线-开始 -->
  4. <view class="chart-con gap">
  5. <view class="chart-wrap">
  6. <view class="title" style="height: 18px;">
  7. <view class="ver-line"></view>
  8. <view @click="showDropdown = !showDropdown" class="title-desc">
  9. <text>平均值 </text>
  10. <text class="t-sm">{{type}}<text class="cuIcon-triangledownfill"></text></text>
  11. <!-- 下拉选择器 -->
  12. </view>
  13. <view v-show="showDropdown" class="dropdown-container flex justify-between">
  14. <view class="dropdown-item" @click="selectItem('year')">年</view>
  15. <view class="dropdown-item" @click="selectItem('month')">月</view>
  16. <view class="dropdown-item" @click="selectItem('day')">日</view>
  17. <!-- <view class="dropdown-item" @click="selectItem('hour')">时</view> -->
  18. </view>
  19. </view>
  20. <view class="line-chart-con">
  21. <l-echart id="123" class="line-chart" ref="lineChart"></l-echart>
  22. </view>
  23. <!-- <text class="margin-left t-sm">注:数据只保留一年</text> -->
  24. </view>
  25. </view>
  26. <!-- 折线-结束 -->
  27. <view class="chart-con gap">
  28. <view class="chart-wrap">
  29. <view class="title">
  30. <view class="ver-line"></view>
  31. <view @click="openDatetimePicker" class="title-desc">
  32. <text>详细记录</text>
  33. <text class="t-sm">
  34. {{selectDate.year}} 年 {{selectDate.month}} 月 <!-- {{selectDate.day}} 日 -->
  35. <text class="cuIcon-triangledownfill"></text>
  36. </text>
  37. </view>
  38. </view>
  39. <uv-swipe-action v-if="refresh">
  40. <uv-swipe-action-item @click="delData($event,key)" v-for="(value, key) in select_history" :key="key" :options="options">
  41. <view @longpress="check_delDatag({'index':0},key)" class="swipe-action uv-border-top uv-border-bottom">
  42. <view class="swipe-action__content">
  43. <view class="margin-left margin-right">
  44. <view class="margin-bottom-xs flex justify-between">
  45. <view class="flex">
  46. <text class="margin-right" style="font-size: 24px;font-weight: 600;">{{value.bmi}}</text>
  47. <div style="display: flex;font-weight: 600;align-items: center;justify-content: center;">
  48. <span style="color: #57bff6;" v-if="value.bmi < 18.5">偏瘦</span>
  49. <span style="color: #13d080;" v-else-if="value.bmi >= 18.5 && value.bmi < 24.0">正常</span>
  50. <span style="color: #ffb951;" v-else-if="value.bmi >= 24.0 && value.bmi < 28.0">偏重</span>
  51. <span style="color: #ff7433;" v-else-if="value.bmi >= 28.0">肥胖</span>
  52. </div>
  53. </view>
  54. <text style="color:#999;display: flex;align-items: center;justify-content: center;">{{value.time}}</text>
  55. </view>
  56. <view style="color:#666;" class="flex justify-start">
  57. <text class="margin-right">身高:{{value.height}}cm</text>
  58. <text>体重:{{value.weight}}kg</text>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <!-- <uv-divider v-if="key != lastKey" style="margin:0 ;padding: 0;height: 1px;"></uv-divider> -->
  64. <view v-if="key != lastKey" style="background-color: #ccc;bottom: 0;height: 1px;margin:0 10px;"></view>
  65. </uv-swipe-action-item>
  66. </uv-swipe-action>
  67. <view v-else style="height: 3000px;"></view>
  68. </view>
  69. <uv-datetime-picker :formatter="formatter" :minDate="firstkey" :maxDate="lastKey" ref="datetimePicker" v-model="dateValue" mode="year-month" @confirm="confirm"></uv-datetime-picker>
  70. </view>
  71. </view>
  72. <view v-else class="no_data"></view>
  73. </template>
  74. <script>
  75. import * as echarts from '@/uni_modules/lime-echart/static/echarts.min.js';
  76. export default {
  77. onPullDownRefresh() {
  78. let bmi_history = uni.getStorageSync('bmi_history');
  79. this.bmi_history = null;
  80. if(bmi_history && Object.keys(bmi_history).length != 0) this.bmi_history = bmi_history;
  81. else uni.showToast({duration:1500,icon:"error",title: '暂无数据 !'});
  82. this.$forceUpdate();
  83. setTimeout(() => {uni.stopPullDownRefresh();}, 1000);
  84. },
  85. onLoad() {
  86. let bmi_history = uni.getStorageSync('bmi_history');
  87. if(bmi_history && Object.keys(bmi_history).length != 0) this.bmi_history = bmi_history;
  88. else uni.showToast({duration:1500,icon:"error",title: '暂无数据 !'});
  89. },
  90. mounted() {
  91. if(this.bmi_history){
  92. this.loadLineData("month");
  93. }
  94. // 获取对象的所有键
  95. const keys = Object.keys(this.bmi_history);
  96. // 获取最后一个键
  97. const lastKey = keys[keys.length - 1];
  98. this.lastKey = lastKey;
  99. this.firstkey = keys[0];
  100. this.confirm({'value': lastKey});
  101. },
  102. data() {
  103. return {
  104. selectDate: {
  105. year: null,
  106. month: null,
  107. day: null
  108. },
  109. dateValue: Number(new Date()),
  110. month: null,
  111. bmi_history: null,
  112. select_history: null,
  113. showDropdown: false,
  114. type: null,
  115. options: [{
  116. text: '删除',
  117. style: {
  118. backgroundColor: '#f56c6c'
  119. }
  120. }],
  121. firstkey: null,
  122. lastKey: null,
  123. refresh: true
  124. }
  125. },
  126. methods: {
  127. check_delDatag(e,key){
  128. uni.showModal({
  129. title: '删除此纪录',
  130. content: '是否要删除此纪录?',
  131. success: (res) => {
  132. if (res.confirm)
  133. this.delData(e,key);
  134. }
  135. })
  136. },
  137. delData(e,key){
  138. console.log('e: ',e);
  139. console.log('key: ',key);
  140. if(e['index'] === 0){
  141. let bmi_history = uni.getStorageSync('bmi_history');
  142. if (bmi_history.hasOwnProperty(key)) {
  143. delete bmi_history[key];
  144. uni.setStorageSync('bmi_history',bmi_history);
  145. }
  146. if (this.select_history.hasOwnProperty(key)) {
  147. delete this.select_history[key];
  148. }
  149. this.$forceUpdate();
  150. this.refresh = false;
  151. setTimeout(() => this.refresh = true,1);
  152. }
  153. },
  154. openDatetimePicker() {
  155. this.$refs.datetimePicker.open();
  156. },
  157. confirm(e) {
  158. console.log('confirm', e);
  159. const date = new Date(parseInt(e.value));
  160. this.selectDate.year = date.getFullYear();
  161. this.selectDate.month = date.getMonth() + 1;
  162. // this.selectDate.day = date.getDate();
  163. this.select_history =this.$util.reversedObject(filterByDate(this.selectDate,this.bmi_history));
  164. this.$forceUpdate();
  165. this.refresh = false;
  166. setTimeout(() => this.refresh = true,1);
  167. function filterByDate(dateObj, timestampObj) {
  168. const { year, month } = dateObj;
  169. const result = {};
  170. for (const [timestamp, value] of Object.entries(timestampObj)) {
  171. const date = new Date(parseInt(timestamp));
  172. if ((year === null || date.getFullYear() === year) &&
  173. (month === null || date.getMonth() + 1 === month)) {
  174. const day = date.getDate().toString().padStart(2, '0');
  175. const hours = date.getHours().toString().padStart(2, '0');
  176. const minutes = date.getMinutes().toString().padStart(2, '0');
  177. result[timestamp] = { ...value, time: `${day}日 ${hours}时 ${minutes}分` };
  178. }
  179. }
  180. return result;
  181. }
  182. },
  183. formatter(type, value) {
  184. if (type === 'year') {
  185. return `${value}年`
  186. }
  187. if (type === 'month') {
  188. return `${value}月`
  189. }
  190. if (type === 'day') {
  191. return `${value}日`
  192. }
  193. return value
  194. },
  195. selectItem(item) {
  196. this.showDropdown = false; // 关闭下拉选择器
  197. this.loadLineData(item);
  198. },
  199. loadLineData(type) {
  200. let res = this.handle_data(type);
  201. let option = {
  202. xAxis: {
  203. type: 'category',
  204. // x轴数据文字颜色
  205. axisLabel: {
  206. color: '#a7a7a7'
  207. },
  208. // x轴那天坐标轴线的颜色
  209. axisLine: {
  210. lineStyle: {
  211. color: '#f1f1f1',
  212. }
  213. },
  214. //x轴上面刻度线隐藏
  215. axisTick: {
  216. show: false,
  217. },
  218. //这里是x轴数据
  219. data: res.xData
  220. },
  221. //设置网格
  222. grid: {
  223. top: 40,
  224. bottom: 30,
  225. },
  226. //y轴设置
  227. yAxis: {
  228. type: 'value',
  229. //y轴标签文字颜色
  230. axisLabel: {
  231. color: '#a7a7a7'
  232. },
  233. // y轴分割线设置为虚线
  234. splitLine: {
  235. show: true,
  236. lineStyle: {
  237. type: 'dashed'
  238. }
  239. }
  240. },
  241. //设置提示为点击时
  242. tooltip: {
  243. trigger: 'axis',
  244. triggerOn: 'click',
  245. formatter: '{b} \n 数据: {c}'
  246. },
  247. //设置曲线的颜色
  248. color: ['#4e9d77'],
  249. series: [{
  250. //这里是数据
  251. data: res.yData,
  252. type: 'line',
  253. //设置为平滑,默认为折线
  254. smooth: true,
  255. }],
  256. //设置数据缩放,手指缩放
  257. dataZoom:{
  258. type:'inside',//inside移动端就是手指缩放,slider
  259. id:'123',
  260. }
  261. };
  262. this.$refs.lineChart.init(echarts, chart => {
  263. chart.setOption(option);
  264. });
  265. },
  266. handle_data(type = null){
  267. let xData;
  268. if(type === null) xData = this.formatTimestamps(this.bmi_history);
  269. else xData = this.formatTimestamps_typeB(this.bmi_history,type);
  270. let yData = this.extractBmis(this.bmi_history);
  271. console.log('xData: ',xData);
  272. console.log('yData: ',yData);
  273. let res = averageData(xData, yData)
  274. return {
  275. xData: res.newXData,
  276. yData: res.newYData
  277. }
  278. function averageData(xData, yData) {
  279. // 创建一个Map来存储每个时间点的总和和计数
  280. const dataMap = new Map();
  281. // 遍历xData和yData
  282. for (let i = 0; i < xData.length; i++) {
  283. const time = xData[i];
  284. const value = parseFloat(yData[i]);
  285. if (dataMap.has(time)) {
  286. // 如果时间点已经存在,更新总和和计数
  287. const [sum, count] = dataMap.get(time);
  288. dataMap.set(time, [sum + value, count + 1]);
  289. } else {
  290. // 如果时间点不存在,初始化总和和计数
  291. dataMap.set(time, [value, 1]);
  292. }
  293. }
  294. // 创建新的xData和yData
  295. const newXData = [];
  296. const newYData = [];
  297. // 遍历Map计算平均值
  298. for (const [time, [sum, count]] of dataMap) {
  299. newXData.push(time);
  300. newYData.push((sum / count).toFixed(2)); // 保留两位小数
  301. }
  302. return { newXData, newYData };
  303. }
  304. },
  305. extractBmis(obj) {
  306. const bmis = [];
  307. function traverse(object) {
  308. for (let key in object) {
  309. if (object.hasOwnProperty(key)) {
  310. const value = object[key];
  311. if (typeof value === 'object' && value !== null) {
  312. if (value.bmi !== undefined) {
  313. bmis.push(value.bmi);
  314. }
  315. traverse(value); // 递归遍历子对象
  316. }
  317. }
  318. }
  319. }
  320. traverse(obj);
  321. return bmis;
  322. },
  323. formatTimestamps(obj) {
  324. return this.formatTimestamps_typeB(obj, getTimeGranularity(obj));
  325. function getTimeGranularity(timestamps) {
  326. const dates = Object.keys(timestamps).map(ts => new Date(parseInt(ts)));
  327. const allSameYear = dates.every(date => date.getFullYear() === dates[0].getFullYear());
  328. if (!allSameYear) return 'year';
  329. const allSameMonth = dates.every(date => date.getMonth() === dates[0].getMonth());
  330. if (!allSameMonth) return 'month';
  331. const allSameDay = dates.every(date => date.getDate() === dates[0].getDate());
  332. if (!allSameDay) return 'day';
  333. }
  334. },
  335. formatTimestamps_typeB(obj, type) {
  336. if(type === 'year')this.type = '年';
  337. if(type === 'month')this.type = '月';
  338. if(type === 'day')this.type = '日';
  339. if(type === 'hour')this.type = '时';
  340. const timestamps = Object.keys(obj).map(ts => new Date(parseInt(ts)));
  341. return timestamps.map(ts => {
  342. switch (type) {
  343. case 'year':
  344. return ts.getFullYear().toString();
  345. case 'month':
  346. return (ts.getMonth() + 1).toString();
  347. case 'day':
  348. return (ts.getMonth() + 1) + '/' + ts.getDate().toString().padStart(2, '0');
  349. // return ts.getDate().toString();
  350. case 'hour':
  351. return ts.getHours() + ':' + ts.getMinutes().toString().padStart(2, '0');
  352. default:
  353. throw new Error('Invalid granularity');
  354. }
  355. });
  356. }
  357. }
  358. }
  359. </script>
  360. <style>
  361. .dropdown-item {
  362. padding: 3px;
  363. color: #5e5e5e;
  364. border-radius: 10px;
  365. cursor: pointer;
  366. }
  367. .dropdown-item:hover {
  368. background-color: #e0e0e0;
  369. }
  370. .dropdown-container {
  371. padding: 3px;
  372. font-size: 18px;
  373. position: relative;
  374. top: 3px; /* 根据实际需求调整距离 */
  375. background-color: #f3f5f4;
  376. border-radius: 10px;
  377. // box-shadow: 0 2px 10px rgba(0, 0, 0, 0.8);
  378. z-index: 100; /* 确保选择器在其他内容之上 */
  379. }
  380. </style>
  381. <style lang="scss">
  382. .uv-page {
  383. padding: 0;
  384. }
  385. .uv-demo-block__title {
  386. padding: 10px 0 2px 15px;
  387. }
  388. .swipe-action {
  389. &__content {
  390. padding: 25rpx 0;
  391. &__text {
  392. font-size: 15px;
  393. color: #222;
  394. padding-left: 30rpx;
  395. }
  396. }
  397. }
  398. </style>
  399. <style lang="scss" scoped>
  400. .content {
  401. width: 100%;
  402. min-height: 100vh;
  403. background-color: #f1f3f2;
  404. padding: 20rpx 0rpx 100rpx;
  405. }
  406. .t-sm{
  407. font-size: 22rpx;
  408. color: #6e6e6e;
  409. padding-left: 10rpx;
  410. }
  411. .chart-con {
  412. width: 100%;
  413. box-sizing: border-box;
  414. padding: 0rpx 28rpx;
  415. .chart-wrap {
  416. width: 100%;
  417. box-sizing: border-box;
  418. background-color: #ffffff;
  419. padding: 32rpx 0rpx;
  420. border-radius: 20rpx;
  421. .title {
  422. box-sizing: border-box;
  423. width: 100%;
  424. padding: 0rpx 28rpx;
  425. display: flex;
  426. flex-direction: row;
  427. justify-content: flex-start;
  428. align-items: center;
  429. }
  430. .ver-line {
  431. height: 30rpx;
  432. width: 8rpx;
  433. border-radius: 10rpx;
  434. background-color: #4e9d77;
  435. }
  436. .title-desc {
  437. font-size: 30rpx;
  438. color: #222222;
  439. margin-left: 22rpx;
  440. font-weight: bold;
  441. }
  442. .line-chart-con {
  443. width: 100%;
  444. box-sizing: border-box;
  445. padding: 0rpx 28rpx;
  446. .fun-tabs {
  447. margin-top: 42rpx;
  448. display: flex;
  449. flex-direction: row;
  450. justify-content: space-between;
  451. align-self: center;
  452. width: 100%;
  453. box-sizing: border-box;
  454. .tab-item {
  455. width: 200rpx;
  456. height: 120rpx;
  457. border-radius: 10rpx;
  458. padding-left: 20rpx;
  459. background: #ffffff;
  460. border: 1rpx solid #ececec;
  461. display: flex;
  462. flex-direction: column;
  463. justify-content: center;
  464. align-items: flex-start;
  465. box-sizing: border-box;
  466. .item-name {
  467. color: #6e6e6e;
  468. font-size: 20rpx;
  469. }
  470. .item-val {
  471. color: #222222;
  472. font-size: 24rpx;
  473. font-weight: bold;
  474. margin-top: 20rpx;
  475. }
  476. }
  477. .selected {
  478. background: #edf5f1 !important;
  479. border: 1rpx solid #4e9d77 !important;
  480. .item-name {
  481. color: #4e9d77 !important;
  482. }
  483. .item-val {
  484. color: #4e9d77 !important;
  485. }
  486. }
  487. }
  488. .line-chart {
  489. margin-top: 30rpx;
  490. height: 380rpx;
  491. }
  492. }
  493. }
  494. }
  495. .gap {
  496. margin-top: 30rpx;
  497. }
  498. </style>