sleep.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <template>
  2. <view class="bobox" :style="boxstyle" v-if="!isEmpty">
  3. <view class="box-title">
  4. <view class="box-title-left">睡眠趋势</view>
  5. </view>
  6. <view class="loading" v-show="loading"><sleepLoading /></view>
  7. <view class="echartbox-header" v-if="this.sleepValue && this.sleepValue.length > 0">
  8. <view v-if="touchValue.startTime==touchValue.endTime">{{touchValue.startTime||'--'}}</view>
  9. <view v-else>{{touchValue.startTime.substring(5,16)}} 至 {{touchValue.endTime.substring(5,16)}}</view>
  10. <view class="echartbox-header-time">
  11. <text>{{touchValue.sleepStateText}}</text>
  12. <text class="echartbox-header-num">{{touchValue.hours||0}}</text>
  13. <text>小时</text>
  14. <text class="echartbox-header-num">{{touchValue.minutes||0}}</text>
  15. <text>分钟</text>
  16. <!-- <image src="@/static/images/pages_watch/icons/prompt_icon.png"></image> -->
  17. </view>
  18. </view>
  19. <view class="echartbox">
  20. <template v-if="this.sleepValue && this.sleepValue.length > 0">
  21. <view style="min-height: 446rpx;" v-show="type==1">
  22. <qiun-data-charts type="column" :opts="opts" :chartData="chartData" tooltipFormat="sleepTooltip" @getIndex="clickCharts" />
  23. </view>
  24. <view style="min-height: 446rpx;" v-show="type==0">
  25. <sleepCharts ref="sleepCharts" :type="2" :sleepData="sleepData" :sleepValue="sleepValue" @handleItem="handleItem" />
  26. </view>
  27. <view class="legend border-line" v-show="!loading">
  28. <view class="legend-item">
  29. <view class="legend-dot" style="background-color: #8C37E6;"></view><text>深睡</text>
  30. </view>
  31. <view class="legend-item">
  32. <view class="legend-dot" style="background-color: #D138CF;"></view><text>浅睡</text>
  33. </view>
  34. <view class="legend-item">
  35. <view class="legend-dot" style="background-color: #F88082;"></view><text>快速眼动</text>
  36. </view>
  37. <view class="legend-item">
  38. <view class="legend-dot" style="background-color: #FDBD27;"></view><text>清醒</text>
  39. </view>
  40. </view>
  41. </template>
  42. <template v-else>
  43. <myEmpty />
  44. </template>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import dayjs from 'dayjs';
  50. import sleepLoading from "@/pages_watch/components/sleepCharts/sleepLoading.vue"
  51. import sleepCharts from "@/pages_watch/components/sleepCharts/sleepCharts.vue";
  52. import myEmpty from "@/pages_watch/components/myEmpty/myEmpty.vue";
  53. export default {
  54. name: "sleep",
  55. props: {
  56. loading: {
  57. type: Boolean,
  58. default: false
  59. },
  60. boxstyle: {
  61. type: Object,
  62. default: () => {}
  63. }
  64. },
  65. components: {
  66. sleepCharts,
  67. myEmpty,
  68. sleepLoading
  69. },
  70. data() {
  71. return {
  72. isEmpty: false,
  73. type: 0, // 0日查询,1周查询
  74. sleepData: {},
  75. touchValue: {
  76. startTime: "",
  77. endTime: "",
  78. type: "",
  79. sleepStateText: "",
  80. hours: "",
  81. minutes: ""
  82. },
  83. sleepValue:[],
  84. chartData: {},
  85. opts: {
  86. color: ['#8c37e6','#d138cf','#f88082','#fdbd27'],
  87. padding: [0,0,0,0], // 左侧留够周几文字宽度
  88. enableScroll: false,
  89. legend: { show: false },
  90. dataLabel: false, // 柱内标签已关
  91. xAxis: {},
  92. yAxis: {
  93. disabled: true,
  94. disableGrid: true,
  95. data: [{
  96. min: 0,
  97. max: 378
  98. }]
  99. },
  100. extra: {
  101. column: {
  102. type: 'stack',
  103. width: 20,
  104. barBorderRadius: [10,10,0,0],
  105. labelPosition: 'none',
  106. linearType: 'custom'
  107. }
  108. }
  109. },
  110. dateArray: [],
  111. watchSleepDataVoList: []
  112. }
  113. },
  114. methods: {
  115. setChartData(data,type,param) {
  116. this.type = type || 0
  117. this.sleepValue = data.sleepSection || []
  118. // console.log(JSON.stringify(this.sleepValue))
  119. if(this.sleepValue && this.sleepValue.length > 0) {
  120. if(this.type == 1) {
  121. this.getServerData(param,data)
  122. } else {
  123. this.$nextTick(()=>{
  124. this.$refs.sleepCharts.initData()
  125. })
  126. }
  127. } else {
  128. this.isEmpty = true
  129. }
  130. },
  131. handleItem(item) {
  132. let diff = Math.abs(new Date(item.startTime).getTime() - new Date(item.endTime).getTime());
  133. this.touchValue = {
  134. ...item,
  135. hours: Math.floor(diff / (1000 * 60 * 60)),
  136. minutes: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
  137. }
  138. },
  139. getServerData(param,data) {
  140. const start = dayjs(param.startTime, 'YYYY/MM/DD HH:mm:ss');
  141. const end = dayjs(param.endTime, 'YYYY/MM/DD HH:mm:ss');
  142. this.watchSleepDataVoList = data.watchSleepDataVoList
  143. const map = new Map(data.watchSleepDataVoList.map(item => [item.date, item]));
  144. // 3. 生成 7 天日期数组
  145. const dates = Array.from({ length: end.diff(start, 'day') + 1 }, (_, i) =>
  146. start.add(i, 'day').format('YYYY-MM-DD')
  147. );
  148. this.dateArray = dates
  149. console.log(dates)
  150. // 4. 按维度抽 4 个数组
  151. const deepSleepArr = dates.map(d => map.get(d)?.deepSleep ?? 0);
  152. const eyemoveSleepArr = dates.map(d => map.get(d)?.eyemoveSleep ?? 0);
  153. const lightSleepArr = dates.map(d => map.get(d)?.lightSleep ?? 0);
  154. const weakSleepArr = dates.map(d => map.get(d)?.weakSleep ?? 0);
  155. console.log("结果==",deepSleepArr,eyemoveSleepArr,lightSleepArr,weakSleepArr)
  156. const allZero = [
  157. ...deepSleepArr,
  158. ...eyemoveSleepArr,
  159. ...lightSleepArr,
  160. ...weakSleepArr
  161. ].every(val => val === 0);
  162. // 每分钟占多少像素(总高 20 px)
  163. // const TOTAL_H = 20; // 固定总高
  164. // const maxMin = 378;
  165. const maxTotal = Math.max(
  166. ...data.watchSleepDataVoList.map(({ deepSleep, lightSleep, weakSleep, eyemoveSleep }) =>
  167. (deepSleep ?? 0) + lightSleep + weakSleep + eyemoveSleep
  168. )
  169. );
  170. this.opts.yAxis.data[0].max = maxTotal
  171. console.log(maxTotal)
  172. let res = {
  173. categories: ['周一','周二','周三','周四','周五','周六','周日'],
  174. series: [
  175. {
  176. name: "深睡",
  177. data:deepSleepArr,
  178. },
  179. {
  180. name: "浅睡",
  181. data:lightSleepArr,
  182. },
  183. {
  184. name: "快速动眼",
  185. data:eyemoveSleepArr,
  186. },
  187. {
  188. name: "清醒",
  189. data:weakSleepArr,
  190. }
  191. ]
  192. };
  193. this.chartData = JSON.parse(JSON.stringify(res));
  194. const touchValue = {
  195. startTime: param.startTime,
  196. endTime: param.endTime,
  197. type: '',
  198. sleepStateText: '总睡眠',
  199. hours: "",
  200. minutes: ""
  201. }
  202. const all = (data.deepSleep||0) + (data.lightSleep||0) + (data.weakSleep||0 )+ (data.eyemoveSleep||0)
  203. console.log(all)
  204. touchValue.hours = Math.floor(all / 60)
  205. touchValue.minutes = all % 60
  206. this.touchValue = touchValue
  207. if(allZero) {
  208. this.sleepValue = []
  209. }
  210. },
  211. clickCharts(e) {
  212. console.log(e)
  213. const currentIndex = e.currentIndex.index
  214. const date=this.dateArray[currentIndex]
  215. const res = this.watchSleepDataVoList.find(item=>item.date == date) || {}
  216. console.log("查找",res,date)
  217. const touchValue = {
  218. startTime: res.startTime || date,
  219. endTime: res.endTime || date,
  220. type: '',
  221. sleepStateText: '总睡眠',
  222. hours: "",
  223. minutes: ""
  224. }
  225. const all = (res.deepSleep||0) + (res.lightSleep||0) + (res.weakSleep||0 )+ (res.eyemoveSleep||0)
  226. touchValue.hours = Math.floor(all / 60)
  227. touchValue.minutes = all % 60
  228. this.touchValue = touchValue
  229. }
  230. }
  231. }
  232. </script>
  233. <style lang="scss" scoped>
  234. @mixin u-flex($flexD, $alignI, $justifyC) {
  235. display: flex;
  236. flex-direction: $flexD;
  237. align-items: $alignI;
  238. justify-content: $justifyC;
  239. }
  240. .loading {
  241. height: 90%;
  242. width: 100%;
  243. display: flex;
  244. align-items: center;
  245. justify-content: center;
  246. position: absolute;
  247. top: 42rpx;
  248. left: 0;
  249. background-color: #fff;
  250. z-index: 99;
  251. }
  252. .bobox {
  253. background: #fff;
  254. position: relative;
  255. }
  256. .box-title {
  257. font-family: PingFang SC, PingFang SC;
  258. font-weight: 400;
  259. font-size: 24rpx;
  260. color: #757575;
  261. @include u-flex(row, center, space-between);
  262. &-left {
  263. font-weight: 500;
  264. font-size: 36rpx;
  265. color: #222222;
  266. line-height: 42rpx;
  267. }
  268. &-right {
  269. @include u-flex(row, center, flex-start);
  270. image {
  271. height: 48rpx;
  272. width: 48rpx;
  273. }
  274. }
  275. }
  276. .echartbox {
  277. // height: 694rpx;
  278. width: 100%;
  279. overflow: hidden;
  280. &-header {
  281. font-family: PingFang SC, PingFang SC;
  282. font-weight: 400;
  283. font-size: 24rpx;
  284. color: #757575;
  285. text-align: center;
  286. &-time {
  287. font-weight: 400;
  288. font-size: 28rpx;
  289. color: #333333;
  290. }
  291. &-num {
  292. margin: 0 8rpx;
  293. font-family: DIN, DIN;
  294. font-weight: 500;
  295. font-size: 56rpx;
  296. color: #333333;
  297. }
  298. image {
  299. width: 32rpx;
  300. height: 32rpx;
  301. margin-left: 4rpx;
  302. }
  303. }
  304. }
  305. .legend {
  306. padding: 46rpx 0;
  307. box-sizing: border-box;
  308. @include u-flex(row, center, space-between);
  309. font-family: PingFang SC, PingFang SC;
  310. font-weight: 400;
  311. font-size: 24rpx;
  312. color: #757575;
  313. &-item {
  314. flex: 1;
  315. text-align: center;
  316. @include u-flex(row, center, center);
  317. }
  318. &-dot {
  319. width: 12rpx;
  320. height: 12rpx;
  321. border-radius: 50%;
  322. margin: 12rpx;
  323. }
  324. }
  325. </style>