add unused getYAxisTickMark(); this is written by ChatGPT

This commit is contained in:
Cindy Chang
2024-06-05 09:04:23 +08:00
parent 5418a6e7c0
commit e2d420b4bd

View File

@@ -10,24 +10,68 @@ export function getTimeLabel(second, fixedNumber = 0) {
const day = 24 * 60 * 60; const day = 24 * 60 * 60;
const hour = 60 * 60; const hour = 60 * 60;
const minutes = 60; const minutes = 60;
// 取餘數的操作會把 second 限制在一天之內的範圍即從0到86399之間
// 意思是過了多少天後剩下多少秒。
const dd = Math.floor(second / day); const dd = Math.floor(second / day);
const hh = Math.floor((second % day) / hour); const hh = Math.floor((second % day) / hour);
const mm = Math.floor((second % hour) / minutes); const mm = Math.floor((second % hour) / minutes);
if(dd > 0){ if(dd > 0){
return (second / day).toFixed(fixedNumber) + " days" return (second / day).toFixed(fixedNumber) + " days";
} }
else if(hh > 0){ else if(hh > 0){
return ((second % day) / hour).toFixed(fixedNumber) + " hrs" return ((second % day) / hour).toFixed(fixedNumber) + " hrs";
} }
else if(mm > 0){ else if(mm > 0){
return ((second % hour) / minutes).toFixed(fixedNumber) + " mins" return ((second % hour) / minutes).toFixed(fixedNumber) + " mins";
} }
if(second == 0){ 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 總秒數 * @param {number} second 總秒數
@@ -67,12 +111,17 @@ export function followTimeLabel(second, max, fixedNumber = 0) {
const day = 24 * 60 * 60; const day = 24 * 60 * 60;
const hour = 60 * 60; const hour = 60 * 60;
const minutes = 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 dd = Math.floor(max / day);
const hh = Math.floor((max % day) / hour); const hh = Math.floor((max % day) / hour);
const mm = Math.floor((max % hour) / minutes); const mm = Math.floor((max % hour) / minutes);
let maxUnit = ''; let maxUnit = '';
let result; let result;
console.log("所以dd, hh, mm", dd, hh, mm)
maxUnit = dd > 0 ? 'd' : hh > 0 ? 'h' : mm > 0 ? 'm' : 's'; maxUnit = dd > 0 ? 'd' : hh > 0 ? 'h' : mm > 0 ? 'm' : 's';
switch (maxUnit) { switch (maxUnit) {
case 'd': case 'd':
@@ -100,6 +149,7 @@ export function followTimeLabel(second, max, fixedNumber = 0) {
result = second.toFixed(fixedNumber) + 's'; result = second.toFixed(fixedNumber) + 's';
break; break;
} }
console.log("followTimeLabel() result", max, result)
return result; return result;
} }
/** /**