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'; 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}`;
};
/** /**
* 將秒數轉換成帶有時間單位的格式 * 將秒數轉換成帶有時間單位的格式

View File

@@ -158,6 +158,8 @@ import { setLineChartData } from '@/module/setChartData.js';
import { simpleTimeLabel, followTimeLabel, dateLabel, import { simpleTimeLabel, followTimeLabel, dateLabel,
setTimeStringFormatBaseOnTimeDifference, setTimeStringFormatBaseOnTimeDifference,
mapTimestampToAxisTicksByFormat, mapTimestampToAxisTicksByFormat,
getStepSizeOfYTicks,
getYTicksByIndex,
} from '@/module/timeLabel.js'; } from '@/module/timeLabel.js';
import i18next from '@/i18n/i18n'; import i18next from '@/i18n/i18n';
@@ -549,8 +551,9 @@ export default {
}; };
primeVueSetOption.scales.x.min = minX; primeVueSetOption.scales.x.min = minX;
primeVueSetOption.scales.x.max = maxX; primeVueSetOption.scales.x.max = maxX;
const {resultStepSize: YTickStepSize, unitToUse} = getStepSizeOfYTicks(maxY); // Stepsize only needs to be calculated once
primeVueSetOption.scales.y.ticks.callback = function (value, index, ticks) { primeVueSetOption.scales.y.ticks.callback = function (value, index, ticks) {
return getFollowTimeLabel(value, maxY, 1) return getYTicksByIndex(YTickStepSize, index, unitToUse);
} }
break; break;
case 'count': case 'count':

View File

@@ -146,6 +146,8 @@ import { setLineChartData } from '@/module/setChartData.js';
import { simpleTimeLabel, followTimeLabel, dateLabel, import { simpleTimeLabel, followTimeLabel, dateLabel,
setTimeStringFormatBaseOnTimeDifference, setTimeStringFormatBaseOnTimeDifference,
mapTimestampToAxisTicksByFormat, mapTimestampToAxisTicksByFormat,
getStepSizeOfYTicks,
getYTicksByIndex,
} from '@/module/timeLabel.js'; } from '@/module/timeLabel.js';
const knownScaleLineChartOptions = { const knownScaleLineChartOptions = {
@@ -394,8 +396,9 @@ export default {
}; };
primeVueSetOption.scales.x.min = minX; primeVueSetOption.scales.x.min = minX;
primeVueSetOption.scales.x.max = maxX; primeVueSetOption.scales.x.max = maxX;
const {resultStepSize: YTickStepSize, unitToUse} = getStepSizeOfYTicks(maxY); // Stepsize only needs to be calculated once
primeVueSetOption.scales.y.ticks.callback = function (value, index, ticks) { primeVueSetOption.scales.y.ticks.callback = function (value, index, ticks) {
return getFollowTimeLabel(value, maxY, 1) return getYTicksByIndex(YTickStepSize, index, unitToUse);
}; };
break; break;
case 'count': case 'count':