BMI_history.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 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. delData(e,key){
  128. console.log('e: ',e);
  129. console.log('key: ',key);
  130. if(e['index'] === 0){
  131. let bmi_history = uni.getStorageSync('bmi_history');
  132. if (bmi_history.hasOwnProperty(key)) {
  133. delete bmi_history[key];
  134. uni.setStorageSync('bmi_history',bmi_history);
  135. }
  136. if (this.select_history.hasOwnProperty(key)) {
  137. delete this.select_history[key];
  138. }
  139. this.$forceUpdate();
  140. this.refresh = false;
  141. setTimeout(() => this.refresh = true,1);
  142. }
  143. },
  144. openDatetimePicker() {
  145. this.$refs.datetimePicker.open();
  146. },
  147. // filter(type, options) {
  148. // if (type === 'year') return filter_year(this.bmi_history);
  149. // console.log('options: ',options);
  150. // return options;
  151. // function filter_year(obj){
  152. // // 获取对象的所有键
  153. // const keys = Object.keys(obj);
  154. // // 获取第一个键并转换为 Date 对象
  155. // const firstKey = new Date(parseInt(keys[0]));
  156. // // 获取最后一个键并转换为 Date 对象
  157. // const lastKey = new Date(parseInt(keys[keys.length - 1]));
  158. // console.log('第一个键年份:', firstKey.getFullYear());
  159. // console.log('最后一个键年份:', lastKey.getFullYear());
  160. // return [firstKey.getFullYear().toString(),lastKey.getFullYear().toString()]
  161. // }
  162. // },
  163. confirm(e) {
  164. console.log('confirm', e);
  165. const date = new Date(parseInt(e.value));
  166. this.selectDate.year = date.getFullYear();
  167. this.selectDate.month = date.getMonth() + 1;
  168. // this.selectDate.day = date.getDate();
  169. this.select_history =this.$util.reversedObject(filterByDate(this.selectDate,this.bmi_history));
  170. this.$forceUpdate();
  171. this.refresh = false;
  172. setTimeout(() => this.refresh = true,1);
  173. function filterByDate(dateObj, timestampObj) {
  174. const { year, month } = dateObj;
  175. const result = {};
  176. for (const [timestamp, value] of Object.entries(timestampObj)) {
  177. const date = new Date(parseInt(timestamp));
  178. if ((year === null || date.getFullYear() === year) &&
  179. (month === null || date.getMonth() + 1 === month)) {
  180. const day = date.getDate().toString().padStart(2, '0');
  181. const hours = date.getHours().toString().padStart(2, '0');
  182. const minutes = date.getMinutes().toString().padStart(2, '0');
  183. result[timestamp] = { ...value, time: `${day}日 ${hours}时 ${minutes}分` };
  184. }
  185. }
  186. return result;
  187. }
  188. // function filterByDate(dateObj, timestampObj) {
  189. // const { year, month, day } = dateObj;
  190. // const result = {};
  191. // for (const [timestamp, value] of Object.entries(timestampObj)) {
  192. // const date = new Date(parseInt(timestamp));
  193. // if ((year === null || date.getFullYear() === year) &&
  194. // (month === null || date.getMonth() + 1 === month) &&
  195. // (day === null || date.getDate() === day)) {
  196. // result[timestamp] = value;
  197. // }
  198. // }
  199. // return result;
  200. // }
  201. },
  202. formatter(type, value) {
  203. if (type === 'year') {
  204. return `${value}年`
  205. }
  206. if (type === 'month') {
  207. return `${value}月`
  208. }
  209. if (type === 'day') {
  210. return `${value}日`
  211. }
  212. return value
  213. },
  214. selectItem(item) {
  215. this.showDropdown = false; // 关闭下拉选择器
  216. this.loadLineData(item);
  217. },
  218. loadLineData(type) {
  219. let res = this.handle_data(type);
  220. // let res = {
  221. // //x轴数据
  222. // xData: xData,
  223. // //y轴数据
  224. // yData: yData
  225. // }
  226. //这里option配置参考文档:https://echarts.apache.org/zh/option.html
  227. let option = {
  228. xAxis: {
  229. type: 'category',
  230. // x轴数据文字颜色
  231. axisLabel: {
  232. color: '#a7a7a7'
  233. },
  234. // x轴那天坐标轴线的颜色
  235. axisLine: {
  236. lineStyle: {
  237. color: '#f1f1f1',
  238. }
  239. },
  240. //x轴上面刻度线隐藏
  241. axisTick: {
  242. show: false,
  243. },
  244. //这里是x轴数据
  245. data: res.xData
  246. },
  247. //设置网格
  248. grid: {
  249. top: 40,
  250. bottom: 30,
  251. },
  252. //y轴设置
  253. yAxis: {
  254. type: 'value',
  255. //y轴标签文字颜色
  256. axisLabel: {
  257. color: '#a7a7a7'
  258. },
  259. // y轴分割线设置为虚线
  260. splitLine: {
  261. show: true,
  262. lineStyle: {
  263. type: 'dashed'
  264. }
  265. }
  266. },
  267. //设置提示为点击时
  268. tooltip: {
  269. trigger: 'axis',
  270. triggerOn: 'click',
  271. formatter: '{b} \n 数据: {c}'
  272. },
  273. //设置曲线的颜色
  274. color: ['#4e9d77'],
  275. series: [{
  276. //这里是数据
  277. data: res.yData,
  278. type: 'line',
  279. //设置为平滑,默认为折线
  280. smooth: true,
  281. }],
  282. //设置数据缩放,手指缩放
  283. dataZoom:{
  284. type:'inside',//inside移动端就是手指缩放,slider
  285. id:'123',
  286. }
  287. };
  288. this.$refs.lineChart.init(echarts, chart => {
  289. chart.setOption(option);
  290. });
  291. },
  292. handle_data(type = null){
  293. let xData;
  294. if(type === null) xData = this.formatTimestamps(this.bmi_history);
  295. else xData = this.formatTimestamps_typeB(this.bmi_history,type);
  296. let yData = this.extractBmis(this.bmi_history);
  297. console.log('xData: ',xData);
  298. console.log('yData: ',yData);
  299. let res = averageData(xData, yData)
  300. return {
  301. xData: res.newXData,
  302. yData: res.newYData
  303. }
  304. function averageData(xData, yData) {
  305. // 创建一个Map来存储每个时间点的总和和计数
  306. const dataMap = new Map();
  307. // 遍历xData和yData
  308. for (let i = 0; i < xData.length; i++) {
  309. const time = xData[i];
  310. const value = parseFloat(yData[i]);
  311. if (dataMap.has(time)) {
  312. // 如果时间点已经存在,更新总和和计数
  313. const [sum, count] = dataMap.get(time);
  314. dataMap.set(time, [sum + value, count + 1]);
  315. } else {
  316. // 如果时间点不存在,初始化总和和计数
  317. dataMap.set(time, [value, 1]);
  318. }
  319. }
  320. // 创建新的xData和yData
  321. const newXData = [];
  322. const newYData = [];
  323. // 遍历Map计算平均值
  324. for (const [time, [sum, count]] of dataMap) {
  325. newXData.push(time);
  326. newYData.push((sum / count).toFixed(2)); // 保留两位小数
  327. }
  328. return { newXData, newYData };
  329. }
  330. },
  331. extractBmis(obj) {
  332. const bmis = [];
  333. function traverse(object) {
  334. for (let key in object) {
  335. if (object.hasOwnProperty(key)) {
  336. const value = object[key];
  337. if (typeof value === 'object' && value !== null) {
  338. if (value.bmi !== undefined) {
  339. bmis.push(value.bmi);
  340. }
  341. traverse(value); // 递归遍历子对象
  342. }
  343. }
  344. }
  345. }
  346. traverse(obj);
  347. return bmis;
  348. },
  349. formatTimestamps(obj) {
  350. return this.formatTimestamps_typeB(obj, getTimeGranularity(obj));
  351. function getTimeGranularity(timestamps) {
  352. const dates = Object.keys(timestamps).map(ts => new Date(parseInt(ts)));
  353. const allSameYear = dates.every(date => date.getFullYear() === dates[0].getFullYear());
  354. if (!allSameYear) return 'year';
  355. const allSameMonth = dates.every(date => date.getMonth() === dates[0].getMonth());
  356. if (!allSameMonth) return 'month';
  357. const allSameDay = dates.every(date => date.getDate() === dates[0].getDate());
  358. if (!allSameDay) return 'day';
  359. }
  360. },
  361. formatTimestamps_typeB(obj, type) {
  362. if(type === 'year')this.type = '年';
  363. if(type === 'month')this.type = '月';
  364. if(type === 'day')this.type = '日';
  365. if(type === 'hour')this.type = '时';
  366. const timestamps = Object.keys(obj).map(ts => new Date(parseInt(ts)));
  367. return timestamps.map(ts => {
  368. switch (type) {
  369. case 'year':
  370. return ts.getFullYear().toString();
  371. case 'month':
  372. return (ts.getMonth() + 1).toString();
  373. case 'day':
  374. return (ts.getMonth() + 1) + '/' + ts.getDate().toString().padStart(2, '0');
  375. // return ts.getDate().toString();
  376. case 'hour':
  377. return ts.getHours() + ':' + ts.getMinutes().toString().padStart(2, '0');
  378. default:
  379. throw new Error('Invalid granularity');
  380. }
  381. });
  382. }
  383. // formatTimestamps_typeA(obj, granularity) {
  384. // const timestamps = Object.keys(obj).map(ts => new Date(parseInt(ts)));
  385. // return timestamps.map(ts => {
  386. // let formatted = '';
  387. // if (granularity === 'year') {
  388. // formatted += ts.getFullYear() + '/';
  389. // }
  390. // if (granularity === 'year' || granularity === 'month') {
  391. // formatted += (ts.getMonth() + 1) + '/';
  392. // }
  393. // if (granularity === 'year' || granularity === 'month' || granularity === 'day') {
  394. // formatted += ts.getDate() + ' ';
  395. // }
  396. // if (granularity === 'year' || granularity === 'month' || granularity === 'day' || granularity === 'hour') {
  397. // formatted += ts.getHours() + ':' + ts.getMinutes();
  398. // }
  399. // return formatted;
  400. // });
  401. // }
  402. // generateArithmeticSequence(min, max) {
  403. // // 计算调整后的最大值和最小值
  404. // const adjustedMin = min - 10;
  405. // const adjustedMax = max + 10;
  406. // // 计算等差数列的公差
  407. // const difference = adjustedMax - adjustedMin;
  408. // const count = 10;
  409. // const step = difference / (count - 1);
  410. // // 生成等差数列数组
  411. // const sequence = [];
  412. // for (let i = 0; i < count; i++) {
  413. // sequence.push(adjustedMin + i * step);
  414. // }
  415. // return sequence;
  416. // },
  417. // findMaxBmi(obj) {
  418. // let maxBmi = -Infinity;
  419. // function traverse(object) {
  420. // for (let key in object) {
  421. // if (object.hasOwnProperty(key)) {
  422. // const value = object[key];
  423. // if (typeof value === 'object' && value !== null) {
  424. // if (value.bmi !== undefined) {
  425. // maxBmi = Math.max(maxBmi, parseFloat(value.bmi));
  426. // }
  427. // traverse(value); // 递归遍历子对象
  428. // }
  429. // }
  430. // }
  431. // }
  432. // traverse(obj);
  433. // return maxBmi;
  434. // },
  435. // findMinBmi(obj) {
  436. // let minBmi = Infinity;
  437. // function traverse(object) {
  438. // for (let key in object) {
  439. // if (object.hasOwnProperty(key)) {
  440. // const value = object[key];
  441. // if (typeof value === 'object' && value !== null) {
  442. // if (value.bmi !== undefined) {
  443. // minBmi = Math.min(minBmi, parseFloat(value.bmi));
  444. // }
  445. // traverse(value); // 递归遍历子对象
  446. // }
  447. // }
  448. // }
  449. // }
  450. // traverse(obj);
  451. // return minBmi;
  452. // }
  453. }
  454. }
  455. </script>
  456. <style>
  457. .dropdown-item {
  458. padding: 3px;
  459. color: #5e5e5e;
  460. border-radius: 10px;
  461. cursor: pointer;
  462. }
  463. .dropdown-item:hover {
  464. background-color: #e0e0e0;
  465. }
  466. .dropdown-container {
  467. padding: 3px;
  468. font-size: 18px;
  469. position: relative;
  470. top: 3px; /* 根据实际需求调整距离 */
  471. background-color: #f3f5f4;
  472. border-radius: 10px;
  473. // box-shadow: 0 2px 10px rgba(0, 0, 0, 0.8);
  474. z-index: 100; /* 确保选择器在其他内容之上 */
  475. }
  476. </style>
  477. <style lang="scss">
  478. .uv-page {
  479. padding: 0;
  480. }
  481. .uv-demo-block__title {
  482. padding: 10px 0 2px 15px;
  483. }
  484. .swipe-action {
  485. &__content {
  486. padding: 25rpx 0;
  487. &__text {
  488. font-size: 15px;
  489. color: #222;
  490. padding-left: 30rpx;
  491. }
  492. }
  493. }
  494. </style>
  495. <style lang="scss" scoped>
  496. .content {
  497. width: 100%;
  498. min-height: 100vh;
  499. background-color: #f1f3f2;
  500. padding: 20rpx 0rpx 100rpx;
  501. }
  502. .t-sm{
  503. font-size: 22rpx;
  504. color: #6e6e6e;
  505. padding-left: 10rpx;
  506. }
  507. .chart-con {
  508. width: 100%;
  509. box-sizing: border-box;
  510. padding: 0rpx 28rpx;
  511. .chart-wrap {
  512. width: 100%;
  513. box-sizing: border-box;
  514. background-color: #ffffff;
  515. padding: 32rpx 0rpx;
  516. border-radius: 20rpx;
  517. .title {
  518. box-sizing: border-box;
  519. width: 100%;
  520. padding: 0rpx 28rpx;
  521. display: flex;
  522. flex-direction: row;
  523. justify-content: flex-start;
  524. align-items: center;
  525. }
  526. .ver-line {
  527. height: 30rpx;
  528. width: 8rpx;
  529. border-radius: 10rpx;
  530. background-color: #4e9d77;
  531. }
  532. .title-desc {
  533. font-size: 30rpx;
  534. color: #222222;
  535. margin-left: 22rpx;
  536. font-weight: bold;
  537. }
  538. .line-chart-con {
  539. width: 100%;
  540. box-sizing: border-box;
  541. padding: 0rpx 28rpx;
  542. .fun-tabs {
  543. margin-top: 42rpx;
  544. display: flex;
  545. flex-direction: row;
  546. justify-content: space-between;
  547. align-self: center;
  548. width: 100%;
  549. box-sizing: border-box;
  550. .tab-item {
  551. width: 200rpx;
  552. height: 120rpx;
  553. border-radius: 10rpx;
  554. padding-left: 20rpx;
  555. background: #ffffff;
  556. border: 1rpx solid #ececec;
  557. display: flex;
  558. flex-direction: column;
  559. justify-content: center;
  560. align-items: flex-start;
  561. box-sizing: border-box;
  562. .item-name {
  563. color: #6e6e6e;
  564. font-size: 20rpx;
  565. }
  566. .item-val {
  567. color: #222222;
  568. font-size: 24rpx;
  569. font-weight: bold;
  570. margin-top: 20rpx;
  571. }
  572. }
  573. .selected {
  574. background: #edf5f1 !important;
  575. border: 1rpx solid #4e9d77 !important;
  576. .item-name {
  577. color: #4e9d77 !important;
  578. }
  579. .item-val {
  580. color: #4e9d77 !important;
  581. }
  582. }
  583. }
  584. .line-chart {
  585. margin-top: 30rpx;
  586. height: 380rpx;
  587. }
  588. }
  589. }
  590. }
  591. .gap {
  592. margin-top: 30rpx;
  593. }
  594. </style>