260312_01_ENH: API_DECIMAL_POINT, VIEW_DECIMAL_POINT 적용, 스택 차트 색상 변경, 그룹 스택 차트 하단에 연도 레이블 출력, Total 페이지 설명문 수정
This commit is contained in:
@@ -7,11 +7,12 @@ import {
|
||||
} from "@/types/lg";
|
||||
import {ChartData} from "@/components/lge-dashboard";
|
||||
import {
|
||||
API_DECIMAL_POINT,
|
||||
BANNER_GROUP,
|
||||
BANNER_KEYS,
|
||||
CATEGORY_PAGE_FIELDS,
|
||||
GAP_WORDING,
|
||||
PRODUCT_KEYS
|
||||
PRODUCT_KEYS, VIEW_DECIMAL_POINT
|
||||
} from "@/const/lgConst";
|
||||
import {formatNumber} from "@/lib/utils";
|
||||
import {nextApi} from "@/lib/http-client";
|
||||
@@ -393,16 +394,27 @@ export const buildCategoryFilter = (
|
||||
return Object.keys(out).length ? out : undefined;
|
||||
}
|
||||
|
||||
const fixedDecimalPoint = (n: number): string => {
|
||||
return n.toFixed(API_DECIMAL_POINT);
|
||||
};
|
||||
|
||||
export const roundNumber = (num: number|null) => {
|
||||
if(num == null) return null;
|
||||
|
||||
const factor = 10 ** VIEW_DECIMAL_POINT;
|
||||
return Math.round(num * factor) / factor;
|
||||
};
|
||||
|
||||
export const calcGapProductRow = (after: ProductRow, before: ProductRow): ProductRow => {
|
||||
const out: ProductRow = {
|
||||
year: after?.year ?? "",
|
||||
rowLabel: "gap",
|
||||
total: String(Number(after?.total ?? "0") - Number(before?.total ?? "0")),
|
||||
total: fixedDecimalPoint(Number(after?.total ?? "0") - Number(before?.total ?? "0")),
|
||||
};
|
||||
for (const k of PRODUCT_KEYS) {
|
||||
const a = after? after[k] ?? "0" : "0";
|
||||
const b = before? before[k] ?? "0" : "0";
|
||||
out[k] = String(Number(a) - Number(b));
|
||||
out[k] = fixedDecimalPoint(Number(a) - Number(b));
|
||||
}
|
||||
return out;
|
||||
};
|
||||
@@ -411,24 +423,22 @@ export const calcGapBannerRow = (after: BannerRow, before: BannerRow): BannerRow
|
||||
const out: BannerRow = {
|
||||
year: after?.year ?? "",
|
||||
rowLabel: "gap",
|
||||
total: String(Number(after?.total ?? "0") - Number(before?.total ?? "0")),
|
||||
total: fixedDecimalPoint(Number(after?.total ?? "0") - Number(before?.total ?? "0")),
|
||||
};
|
||||
for (const k of BANNER_KEYS) {
|
||||
const a = after? after[k] ?? "0" : "0";
|
||||
const b = before? before[k] ?? "0" : "0";
|
||||
out[k] = String(Number(a) - Number(b));
|
||||
out[k] = fixedDecimalPoint(Number(a) - Number(b));
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const round1 = (n: number) => Math.round(n * 10) / 10;
|
||||
|
||||
const calcCagr = (after: number, before: number, years:number): string => {
|
||||
const calcCagr = (after: number, before: number, years:number): number => {
|
||||
const power = Math.pow(after / before, 1 / years);
|
||||
const cagr = power - 1;
|
||||
const cagr100 = cagr * 100;
|
||||
|
||||
return String(round1(cagr100));
|
||||
return cagr100;
|
||||
};
|
||||
|
||||
export const calcCagrRow = (after: ProductRow, before: ProductRow): ProductRow => {
|
||||
@@ -441,32 +451,32 @@ export const calcCagrRow = (after: ProductRow, before: ProductRow): ProductRow =
|
||||
|
||||
// total은 참고용으로만: (26/24) CAGR %
|
||||
out.total =
|
||||
Number(before?.total ?? "0") > 0 ? calcCagr(Number(after?.total ?? "0"), Number(before?.total ?? "0"), years) : "0";
|
||||
fixedDecimalPoint((before?.total ?? "0") > "0" ? calcCagr(Number(after?.total ?? "0"), Number(before?.total ?? "0"), years) : 0);
|
||||
|
||||
for (const k of PRODUCT_KEYS) {
|
||||
const vBefore = before? before[k] ?? "0" : "0";
|
||||
const vAfter = after? after[k] ?? "0" : "0";
|
||||
out[k] = Number(vBefore) > 0 ? calcCagr(Number(vAfter), Number(vBefore), years) : "0";
|
||||
out[k] = fixedDecimalPoint(Number(vBefore) > 0 ? calcCagr(Number(vAfter), Number(vBefore), years) : 0);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
|
||||
export const makeTotalBasePct = (t:number|string, f:number|string):number => {
|
||||
return Number(t) > 0 ? Math.round((Number(f) / Number(t)) * 100) : 0;
|
||||
return Number(t) > 0 ? (Number(f) * 100) / Number(t) : 0;
|
||||
}
|
||||
|
||||
export const calcTotalBaseRow = (label:string, totalRow: ProductRow, targetRow: ProductRow): ProductRow => {
|
||||
const out: ProductRow = {
|
||||
year: targetRow?.year ?? "",
|
||||
rowLabel: `전체 ${label} 중 자사제품 비율`,
|
||||
total: String(makeTotalBasePct(totalRow.total, targetRow.total))
|
||||
total: fixedDecimalPoint(makeTotalBasePct(totalRow.total, targetRow.total))
|
||||
};
|
||||
|
||||
for (const k of PRODUCT_KEYS) {
|
||||
const t = totalRow? totalRow[k] ?? "0" : "0";
|
||||
const f = targetRow? targetRow[k] ?? "0" : "0";
|
||||
out[k] = String(makeTotalBasePct(t, f));
|
||||
out[k] = fixedDecimalPoint(makeTotalBasePct(t, f));
|
||||
}
|
||||
return out;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user