fix #287 by using simple division

This commit is contained in:
Cindy Chang
2024-06-12 12:27:51 +08:00
parent 235504a3fb
commit 339657879a
3 changed files with 72 additions and 2 deletions

View File

@@ -1,4 +1,68 @@
import moment from 'moment';
const NUM_OF_PARTS_TO_SPLIT = 6;
/**
* 若想要有等差的Y軸刻度則必須確保index是乘以一個共通的被乘數
* 此被乘數就是 stepSize
* @param {number} maxTimeInSecond
* @returns {object} {resultStepSize, unitToUse}
*/
export const getStepSizeOfYTicks = (maxTimeInSecond) => {
const numOfParts = NUM_OF_PARTS_TO_SPLIT;
const {unitToUse, timeValue} = getTimeUnitAndValueToUse(maxTimeInSecond);
let resultStepSize;
resultStepSize = Math.floor(timeValue / numOfParts);
return {resultStepSize, unitToUse};
}
/**
* Convert second to possibly day or hour or mins.
* @param {number} secondToDecide
* @returns {object} {unitToUse, timeValue} where timeValue is measured in unitToUse
*/
const getTimeUnitAndValueToUse = (secondToDecide) => {
const day = 24 * 60 * 60;
const hour = 60 * 60;
const minutes = 60;
const dd = secondToDecide / day;
const hh = (secondToDecide % day) / hour;
const mm = (secondToDecide % hour) / minutes;
if (dd > 0 && dd > 1) {
return {
unitToUse: "d",
timeValue: secondToDecide / day,
};
} else if (hh > 0 && hh > 1) {
return {
unitToUse: "h",
timeValue: (secondToDecide % day) / hour,
};
} else if (mm > 0 && mm > 1) {
return {
unitToUse: "m",
timeValue: (secondToDecide % hour) / minutes,
};
} else {
return {
unitToUse: "s",
timeValue: secondToDecide,
}
}
};
/**
* For the display of PrimeVue chart.
* According to the max time of the data given,
* split the Y axis into equal part as ticks.
* @param {number} stepSize stepSize of Y axis
* @param {number} index index of current visiting tick on Y axis
* @param {string} unitToUse time unit to display; "dhms" usually
*/
export function getYTicksByIndex(stepSize, index, unitToUse){
return `${stepSize * index}${unitToUse}`;
};
/**
* 將秒數轉換成帶有時間單位的格式