From e2d420b4bd921b30ddab0bb47af251f4b40e0259 Mon Sep 17 00:00:00 2001 From: Cindy Chang Date: Wed, 5 Jun 2024 09:04:23 +0800 Subject: [PATCH] add unused getYAxisTickMark(); this is written by ChatGPT --- src/module/timeLabel.js | 62 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/src/module/timeLabel.js b/src/module/timeLabel.js index ff25589..5047ef8 100644 --- a/src/module/timeLabel.js +++ b/src/module/timeLabel.js @@ -10,24 +10,68 @@ export function getTimeLabel(second, fixedNumber = 0) { const day = 24 * 60 * 60; const hour = 60 * 60; const minutes = 60; + + // 取餘數的操作會把 second 限制在一天之內的範圍(即從0到86399之間), + // 意思是過了多少天後剩下多少秒。 const dd = Math.floor(second / day); const hh = Math.floor((second % day) / hour); const mm = Math.floor((second % hour) / minutes); if(dd > 0){ - return (second / day).toFixed(fixedNumber) + " days" + return (second / day).toFixed(fixedNumber) + " days"; } else if(hh > 0){ - return ((second % day) / hour).toFixed(fixedNumber) + " hrs" + return ((second % day) / hour).toFixed(fixedNumber) + " hrs"; } else if(mm > 0){ - return ((second % hour) / minutes).toFixed(fixedNumber) + " mins" + return ((second % hour) / minutes).toFixed(fixedNumber) + " mins"; } if(second == 0){ - return second + " sec" + return second + " sec"; } - return second + " sec" + return second + " sec"; } + +/** + * Converts seconds into a formatted string with time units. + * 將秒數轉換成帶有時間單位的格式 + * + * @param {number} second - Total seconds + * 總秒數 + * @param {number} fixedNumber - Number of decimal places + * 小數點後幾位 + * @returns {string} - The formatted time string (e.g., 1 day, 6.8 hrs) + * 轉換完的格式(ex: 1 day, 6.8 hrs) + */ +export function getYAxisTickMark(second, fixedNumber = 0) { + const day = 24 * 60 * 60; + const hour = 60 * 60; + const minutes = 60; + + // Directly convert seconds to the largest unit to ensure equal interval values + // 直接轉換秒數到最大單位,保證數值等差 + + // 1. 首先,我們判斷秒數 second 是否大於或等於一天的秒數 day(一天有86400秒)。 + // 如果是,就把秒數除以一天的秒數,這樣就得到幾天,然後把這個數字格式化, + // 保留 fixedNumber 位小數,最後加上 " days"。 + // 2. 如果秒數小於一天但大於或等於一小時的秒數 hour(一小時有3600秒), + // 就把秒數除以一小時的秒數,這樣就得到幾小時,然後把這個數字格式化, + // 保留 fixedNumber 位小數,最後加上 " hrs"。 + // 3. 如果秒數小於一小時但大於或等於一分鐘的秒數 minutes(一分鐘有60秒), + // 就把秒數除以一分鐘的秒數,這樣就得到幾分鐘,然後把這個數字格式化,保留 fixedNumber 位小數,最後加上 " mins"。 + // 4. 如果以上條件都不滿足,說明秒數小於60秒,就直接返回秒數, + // 格式化後保留 fixedNumber 位小數,最後加上 " sec"。 + if (second >= day) { + return (second / day).toFixed(fixedNumber) + " days"; + } else if (second >= hour) { + return (second / hour).toFixed(fixedNumber) + " hrs"; + } else if (second >= minutes) { + return (second / minutes).toFixed(fixedNumber) + " mins"; + } else { + return second.toFixed(fixedNumber) + " sec"; + } +} + /** * 將秒數轉換成帶有縮寫時間單位的格式 * @param {number} second 總秒數 @@ -67,12 +111,17 @@ export function followTimeLabel(second, max, fixedNumber = 0) { const day = 24 * 60 * 60; const hour = 60 * 60; const minutes = 60; + console.log("max / day", max / day); + console.log("max % day", max % day); + console.log('(max % day) / hour', (max % day) / hour); + console.log('max % hour', max % hour); + console.log('(max % hour) / minutes', (max % hour) / minutes); const dd = Math.floor(max / day); const hh = Math.floor((max % day) / hour); const mm = Math.floor((max % hour) / minutes); let maxUnit = ''; let result; - +console.log("所以dd, hh, mm", dd, hh, mm) maxUnit = dd > 0 ? 'd' : hh > 0 ? 'h' : mm > 0 ? 'm' : 's'; switch (maxUnit) { case 'd': @@ -100,6 +149,7 @@ export function followTimeLabel(second, max, fixedNumber = 0) { result = second.toFixed(fixedNumber) + 's'; break; } + console.log("followTimeLabel() result", max, result) return result; } /**