Skip to content
Snippets Groups Projects
Commit e0e95304 authored by Park Tae hyeon's avatar Park Tae hyeon
Browse files

Fix: WeightChart

parent a178d2ef
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,7 @@ const options = { ...@@ -38,6 +38,7 @@ const options = {
}, },
grid: { grid: {
display: false, display: false,
}, },
ticks: { ticks: {
font: { font: {
...@@ -91,6 +92,21 @@ function WeightChart() { ...@@ -91,6 +92,21 @@ function WeightChart() {
useEffect(() => { useEffect(() => {
const fetchWeightHistory = async () => { const fetchWeightHistory = async () => {
try { try {
/*
const data = [
{ date: '2024-11-29', weight: 50 },
{ date: '2024-11-30', weight: null },
{ date: '2024-12-01', weight: null },
{ date: '2024-12-02', weight: null },
{ date: '2024-12-03', weight: null },
{ date: '2024-12-04', weight: 60 },
{ date: '2024-12-05', weight: null },
{ date: '2024-12-06', weight: null },
{ date: '2024-12-07', weight: null },
{ date: '2024-12-08', weight: 50 }
];
*/
const data = await getWeightHistory(); const data = await getWeightHistory();
if (!data || data.length === 0) { if (!data || data.length === 0) {
...@@ -98,12 +114,12 @@ function WeightChart() { ...@@ -98,12 +114,12 @@ function WeightChart() {
return; return;
} }
const weights = data.map(item => item.weight); let weights = data.map(item => item.weight);
// null 값 처리 후 최대/최소값 계산 // null 값 처리 후 최대/최소값 계산
const validWeights = weights.filter(w => w !== null); const validWeights = weights.filter(w => w !== null);
const minWeight = Math.min(...validWeights); const minWeight = Math.min(...validWeights) * 0.8;
const maxWeight = Math.max(...validWeights); const maxWeight = Math.max(...validWeights) * 1.2;
// 범위의 10% 계산 // 범위의 10% 계산
const range = maxWeight - minWeight; const range = maxWeight - minWeight;
...@@ -123,23 +139,31 @@ function WeightChart() { ...@@ -123,23 +139,31 @@ function WeightChart() {
})); }));
// 기존의 차트 데이터 설정 로직 // 기존의 차트 데이터 설정 로직
const labels = data.map(item => { let labels = data.map(item => {
const date = new Date(item.date); const date = new Date(item.date);
return `${date.getFullYear()}/${String(date.getMonth() + 1).padStart(2, '0')}/${String(date.getDate()).padStart(2, '0')}`; return `${date.getFullYear()}/${String(date.getMonth() + 1).padStart(2, '0')}/${String(date.getDate()).padStart(2, '0')}`;
}); });
let check = -1;
// null 값을 이후 날짜의 가장 가까운 weight 값으로 채우기 // null 값을 이후 날짜의 가장 가까운 weight 값으로 채우기
for (let i = 0; i < weights.length; i++) { for (let i = 0; i < weights.length; i++) {
if (weights[i] === null) { if (weights[i] === null) {
let nextValue = null; let nextValue = null;
for (let j = i + 1; j < weights.length; j++) { for (let j = i - 1; j >= 0; j--) {
if (weights[j] !== null) { if (weights[j] !== null) {
nextValue = weights[j]; nextValue = weights[j];
break; break;
} }
} }
weights[i] = nextValue; if (nextValue === null) check = i + 1;
else weights[i] = nextValue;
}
} }
// null로 기록된 이전 기록 제거
if (check != -1) {
labels = labels.slice(check);
weights = weights.slice(check);
} }
// 차트 데이터 설정 // 차트 데이터 설정
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment