Skip to content

Commit f6c82b0

Browse files
committed
refactor : WaterfallChart Constant 처리 개선
1 parent e8e8457 commit f6c82b0

File tree

2 files changed

+90
-88
lines changed

2 files changed

+90
-88
lines changed

src/chart/WaterfallChart.tsx

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,7 @@
1-
import { ChartData, ChartOptions } from "chart.js";
21
import ChartDataLabels from "chartjs-plugin-datalabels";
32
import React from "react";
43
import { Bar } from "react-chartjs-2";
5-
import { formatNumberWithCommas } from "../utils/numberUtils";
6-
7-
const data: ChartData<"bar"> = {
8-
labels: [
9-
"보험",
10-
"금리",
11-
"신용",
12-
"시장",
13-
"운영",
14-
"비금융",
15-
"차이",
16-
"요구자본",
17-
],
18-
datasets: [
19-
{
20-
label: "Waterfall",
21-
data: [
22-
[0, 6489],
23-
[6489, 6489 + 11188],
24-
[6489 + 11188, 6489 + 11188 + 24523],
25-
[6489 + 11188 + 24523, 6489 + 11188 + 24523 + 4086],
26-
[6489 + 11188 + 24523 + 4086, 6489 + 11188 + 24523 + 4086 + 1435],
27-
[
28-
6489 + 11188 + 24523 + 4086 + 1435,
29-
6489 + 11188 + 24523 + 4086 + 1435 + 1406,
30-
],
31-
[
32-
6489 + 11188 + 24523 + 4086 + 1435 + 1406,
33-
6489 + 11188 + 24523 + 4086 + 1435 + 1406 - 9752,
34-
],
35-
[6489 + 11188 + 24523 + 4086 + 1435 + 1406 - 9752 - 39375, 39375],
36-
],
37-
backgroundColor: function (context: { dataset: any; dataIndex: number }) {
38-
const value = context.dataset.data[context.dataIndex];
39-
40-
// 마지막 차트는 보라색, 나머지는 양수는 녹색, 음수는 빨간색
41-
if (context.dataIndex === context.dataset.data.length - 1) {
42-
return "#B798C5"; // 보라색
43-
}
44-
45-
return value[1] >= value[0] ? "#B8EFEF" : "#FBADB0"; // 양수는 녹색, 음수는 빨간색
46-
},
47-
// borderColor: "#000000",
48-
// borderWidth: 1,
49-
},
50-
],
51-
};
52-
53-
const options: ChartOptions<"bar"> = {
54-
plugins: {
55-
legend: {
56-
display: false, // 범례 숨김
57-
},
58-
title: {
59-
display: true,
60-
text: "기준월 요구자본 세부",
61-
},
62-
datalabels: {
63-
color: "black", // 막대 안에서 읽기 쉽게 글자색을 흰색으로 설정
64-
anchor: "center", // 글자가 막대 안에서 중앙에 배치되도록 설정
65-
align: "center", // 가운데 정렬
66-
formatter: function (value: number[], context: any) {
67-
return (value[1] - value[0]).toLocaleString("ko-KR");
68-
},
69-
},
70-
},
71-
scales: {
72-
x: {
73-
stacked: true, // 누적 설정
74-
ticks: {
75-
maxRotation: 0, // X축 레이블을 기울이지 않음
76-
minRotation: 0, // X축 레이블 최소 회전 설정
77-
autoSkip: true, // 레이블이 겹칠 경우 자동으로 생략
78-
},
79-
},
80-
y: {
81-
beginAtZero: true,
82-
ticks: {
83-
stepSize: 25000,
84-
callback: function (value) {
85-
const numValue = value as number;
86-
return formatNumberWithCommas(numValue) + "억";
87-
},
88-
},
89-
},
90-
},
91-
};
4+
import { data, options } from "../data/WaterfallChartConstant";
925

936
const WaterfallChart = ({ chartRef }: { chartRef: React.RefObject<any> }) => {
947
return (
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { ChartData, ChartOptions } from "chart.js";
2+
import { formatNumberWithCommas } from "../utils/numberUtils";
3+
4+
export const data: ChartData<"bar"> = {
5+
labels: [
6+
"Insurance",
7+
"Interest Rate",
8+
"Credit",
9+
"Market",
10+
"Operations",
11+
"Non-Financial",
12+
"Difference",
13+
"Required Capital",
14+
],
15+
datasets: [
16+
{
17+
label: "Waterfall",
18+
data: [
19+
[0, 6489],
20+
[6489, 6489 + 11188],
21+
[6489 + 11188, 6489 + 11188 + 24523],
22+
[6489 + 11188 + 24523, 6489 + 11188 + 24523 + 4086],
23+
[6489 + 11188 + 24523 + 4086, 6489 + 11188 + 24523 + 4086 + 1435],
24+
[
25+
6489 + 11188 + 24523 + 4086 + 1435,
26+
6489 + 11188 + 24523 + 4086 + 1435 + 1406,
27+
],
28+
[
29+
6489 + 11188 + 24523 + 4086 + 1435 + 1406,
30+
6489 + 11188 + 24523 + 4086 + 1435 + 1406 - 9752,
31+
],
32+
[6489 + 11188 + 24523 + 4086 + 1435 + 1406 - 9752 - 39375, 39375],
33+
],
34+
backgroundColor: function (context: { dataset: any; dataIndex: number }) {
35+
const value = context.dataset.data[context.dataIndex];
36+
if (context.dataIndex === context.dataset.data.length - 1) {
37+
return "#B798C5"; // 마지막 항목은 보라색
38+
}
39+
return value[1] >= value[0] ? "#B8EFEF" : "#FBADB0"; // 양수는 파란색, 음수는 빨간색
40+
},
41+
42+
// borderColor: "#000000",
43+
// borderWidth: 1,
44+
},
45+
],
46+
};
47+
48+
export const options: ChartOptions<"bar"> = {
49+
plugins: {
50+
legend: {
51+
display: false, // 범례 숨김
52+
},
53+
title: {
54+
display: true,
55+
text: "Detailed Required Capital by Base Month",
56+
},
57+
datalabels: {
58+
color: "black", // 막대 안에서 읽기 쉽게 글자색을 흰색으로 설정
59+
anchor: "center", // 글자가 막대 안에서 중앙에 배치되도록 설정
60+
align: "center", // 가운데 정렬
61+
formatter: function (value: number[], context: any) {
62+
return (value[1] - value[0]).toLocaleString("en-US");
63+
},
64+
},
65+
},
66+
scales: {
67+
x: {
68+
stacked: true, // 누적 설정
69+
ticks: {
70+
maxRotation: 0, // X축 레이블을 기울이지 않음
71+
minRotation: 0, // X축 레이블 최소 회전 설정
72+
autoSkip: true, // 레이블이 겹칠 경우 자동으로 생략
73+
font: {
74+
size: 6,
75+
},
76+
},
77+
},
78+
y: {
79+
beginAtZero: true,
80+
ticks: {
81+
stepSize: 25000,
82+
callback: function (value) {
83+
const numValue = value as number;
84+
return `${formatNumberWithCommas(numValue)}M`;
85+
},
86+
},
87+
},
88+
},
89+
};

0 commit comments

Comments
 (0)