CSNET's picture
ช่วยเพิ่มกราฟข้อมูลเหมือนแอพ Whoop ในรูปให้ที
95f9fb4 verified
Raw History Blame Contribute Delete
18 kB
// Behavior Database
const behaviorDatabase = {
"การนอนและสุขภาพรอบวัน": [
"แว่นกันแสงสีฟ้า",
"เครื่องเสียง",
"อ่านหนังสือบนเตียง",
"นอนร่วมกับผู้อื่น",
"แมวอยู่ในห้องนอน",
"สุนัขอยู่ในห้องนอน",
"หน้ากากปิดตา",
"นอนในห้องมืด",
"แสงแดดตอนเช้า",
"รับประทานอาหารดึก"
],
"การฟื้นตัว": [
"นวดบำบัด",
"อาบน้ำเย็น",
"การยืดกล้ามเนื้อ",
"ซาวน่า",
"ห้องไอน้ำ",
"ฝังเข็ม",
"ถ้วยฝาครอบ",
"การ meditate",
"การฝึกหายใจ",
"อาบน้ำอุ่น"
],
"โภชนาการ": [
"การลดน้ำหนักแบบ Intermittent Fasting",
"อาหาร Paleo",
"อาหารพืช",
"อาหาร Keto",
"อาหาร Mediterranean",
"คาเฟอีน",
"การดื่มแอลกอฮอล์",
"ไม่ดื่มแอลกอฮอล์",
"การติดตามการบริโภคน้ำ"
],
"ยาและอาหารเสริม": [
"อาหารเสริมแมกนีเซียม",
"เมลาโทนิน",
"วิตามินรวม",
"วิตามิน D",
"โปรไบโอติกส์",
"CBD",
"ยาแก้ปวด",
"ครีเอทีน",
"ผงโปรตีน"
],
"ไลฟ์สไตล์และสุขภาพจิต": [
"ความวิตกกังวล",
"ความเครียด",
"เวลาหน้าจอ",
"เดินเที่ยวในธรรมชาติ",
"การ detox จากดิจิทัล",
"การตั้งสติ",
"โยคะ",
"การฝึกหายใจ"
]
};
// Initialize Feather Icons
feather.replace();
// Chart Configurations
Chart.defaults.color = '#9ca3af';
Chart.defaults.borderColor = '#374151';
// Heart Rate Zones Chart
const heartRateZonesCtx = document.getElementById('heartRateZonesChart');
if (heartRateZonesCtx) {
const heartRateZonesChart = new Chart(heartRateZonesCtx, {
type: 'bar',
data: {
labels: ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'],
datasets: [
{
label: 'พัก',
data: [45, 48, 52, 65, 58, 62, 55, 50],
backgroundColor: 'rgba(59, 130, 246, 0.8)',
borderColor: '#3b82f6',
borderWidth: 0
},
{
label: 'เบา',
data: [10, 8, 15, 25, 20, 18, 12, 8],
backgroundColor: 'rgba(34, 197, 94, 0.8)',
borderColor: '#22c55e',
borderWidth: 0
},
{
label: 'ปานกลาง',
data: [0, 0, 5, 15, 12, 10, 8, 2],
backgroundColor: 'rgba(250, 204, 21, 0.8)',
borderColor: '#facc15',
borderWidth: 0
},
{
label: 'หนัก',
data: [0, 0, 0, 8, 5, 4, 3, 0],
backgroundColor: 'rgba(251, 146, 60, 0.8)',
borderColor: '#fb923c',
borderWidth: 0
},
{
label: 'สูงสุด',
data: [0, 0, 0, 2, 1, 1, 0, 0],
backgroundColor: 'rgba(239, 68, 68, 0.8)',
borderColor: '#ef4444',
borderWidth: 0
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
label: function(context) {
return context.dataset.label + ': ' + context.parsed.y + ' นาที';
}
}
}
},
scales: {
x: {
stacked: true,
grid: {
display: false
}
},
y: {
stacked: true,
grid: {
color: 'rgba(55, 65, 81, 0.3)'
},
ticks: {
callback: function(value) {
return value + ' นาที';
}
}
}
}
}
});
}
// Strain Accumulation Chart
const strainAccumulationCtx = document.getElementById('strainAccumulationChart');
if (strainAccumulationCtx) {
const strainAccumulationChart = new Chart(strainAccumulationCtx, {
type: 'line',
data: {
labels: ['06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00'],
datasets: [{
label: 'Strain Score',
data: [0.2, 1.5, 3.2, 5.8, 8.4, 10.2, 11.8, 12.5, 12.8],
borderColor: '#fb923c',
backgroundColor: 'rgba(251, 146, 60, 0.1)',
fill: true,
tension: 0.4,
pointRadius: 4,
pointHoverRadius: 6,
borderWidth: 3,
pointBackgroundColor: '#fb923c',
pointBorderColor: '#0f172a'
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
label: function(context) {
return 'Strain: ' + context.parsed.y;
}
}
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
beginAtZero: true,
grid: {
color: 'rgba(55, 65, 81, 0.3)'
}
}
}
}
});
}
// Sleep Cycle Chart
const sleepCtx = document.getElementById('sleepChart');
if (sleepCtx) {
const sleepChart = new Chart(sleepCtx, {
type: 'line',
data: {
labels: ['22:00', '23:00', '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00'],
datasets: [{
label: 'Sleep Stages',
data: [2, 1, 3, 3, 3, 2, 2, 1, 0],
borderColor: '#60a5fa',
backgroundColor: 'rgba(96, 165, 250, 0.1)',
fill: true,
tension: 0.4,
pointRadius: 0,
pointHoverRadius: 6,
borderWidth: 2,
segment: {
borderColor: ctx => {
const value = ctx.p0.parsed.y;
if (value === 0) return '#fbbf24'; // Wake
if (value === 1) return '#93c5fd'; // Light
if (value === 2) return '#6366f1'; // Deep
if (value === 3) return '#a855f7'; // REM
return '#60a5fa';
},
backgroundColor: ctx => {
const value = ctx.p0.parsed.y;
if (value === 0) return 'rgba(251, 191, 36, 0.1)';
if (value === 1) return 'rgba(147, 197, 253, 0.1)';
if (value === 2) return 'rgba(99, 102, 241, 0.1)';
if (value === 3) return 'rgba(168, 85, 247, 0.1)';
return 'rgba(96, 165, 250, 0.1)';
}
}
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
label: function(context) {
const stages = ['ตื่น', 'หลับตื้น', 'หลับลึก', 'REM'];
return stages[context.parsed.y] || '';
}
}
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
min: 0,
max: 3,
ticks: {
stepSize: 1,
callback: function(value) {
const stages = ['ตื่น', 'หลับตื้น', 'หลับลึก', 'REM'];
return stages[value] || '';
}
}
}
}
}
});
}
// HRV Trend Chart
const hrvCtx = document.getElementById('hrvChart');
if (hrvCtx) {
const hrvChart = new Chart(hrvCtx, {
type: 'line',
data: {
labels: ['จ.', 'อ.', 'พ.', 'พฤ.', 'ศ.', 'ส.', 'อา.'],
datasets: [{
label: 'HRV (ms)',
data: [45, 48, 52, 50, 55, 52, 58],
borderColor: '#34d399',
backgroundColor: 'rgba(52, 211, 153, 0.1)',
fill: true,
tension: 0.4,
pointRadius: 4,
pointHoverRadius: 6,
borderWidth: 3,
pointBackgroundColor: '#34d399',
pointBorderColor: '#0f172a'
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
label: function(context) {
return `HRV: ${context.parsed.y} ms`;
}
}
}
},
scales: {
x: {
grid: {
display: false
}
},
y: {
beginAtZero: false,
grid: {
color: 'rgba(55, 65, 81, 0.3)'
}
}
}
}
});
}
// Simulate Real-time Data Updates
function simulateDataUpdate() {
const recoveryScore = document.querySelector('.metric-card:nth-child(1) .text-3xl');
const strainScore = document.querySelector('.metric-card:nth-child(2) .text-3xl');
const sleepScore = document.querySelector('.metric-card:nth-child(3) .text-3xl');
// Add subtle pulse animation to indicate live data
setInterval(() => {
document.querySelectorAll('.metric-card').forEach(card => {
card.classList.add('animate-pulse-glow');
setTimeout(() => card.classList.remove('animate-pulse-glow'), 2000);
});
}, 10000);
}
// Initialize animations on page load
document.addEventListener('DOMContentLoaded', function() {
// Fade in sections
const sections = document.querySelectorAll('section');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
});
sections.forEach(section => {
section.style.opacity = '0';
section.style.transform = 'translateY(20px)';
section.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
observer.observe(section);
});
simulateDataUpdate();
// Initialize behavior tracker on journal page
if (document.getElementById('behaviorTrackerContainer')) {
initializeBehaviorTracker();
}
});
// Handle navigation active state
document.addEventListener('DOMContentLoaded', function() {
const currentPage = window.location.pathname.split('/').pop() || 'index.html';
const navEvent = new CustomEvent('nav-update', { detail: { page: currentPage } });
document.dispatchEvent(navEvent);
});
// Behavior Tracking Functions
function initializeBehaviorTracker() {
const container = document.getElementById('behaviorTrackerContainer');
if (!container) return;
// Load saved behaviors
const savedBehaviors = JSON.parse(localStorage.getItem('selectedBehaviors') || '[]');
// Generate HTML for each category
Object.entries(behaviorDatabase).forEach(([category, behaviors]) => {
const categoryDiv = document.createElement('div');
categoryDiv.className = 'mb-6';
const categoryHeader = document.createElement('div');
categoryHeader.className = 'flex items-center justify-between mb-3 cursor-pointer';
categoryHeader.innerHTML = `
<h3 class="text-lg font-semibold text-gray-200">${category}</h3>
<i data-feather="chevron-down" class="w-5 h-5 text-gray-400 transition-transform"></i>
`;
const behaviorsGrid = document.createElement('div');
behaviorsGrid.className = 'grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-3 max-h-0 overflow-hidden transition-all duration-300';
behaviors.forEach(behavior => {
const isSelected = savedBehaviors.includes(behavior);
const button = document.createElement('button');
button.className = `behavior-btn p-3 rounded-lg border text-sm transition-all ${
isSelected
? 'bg-indigo-500/20 border-indigo-400 text-indigo-400'
: 'bg-gray-900/50 border-gray-600 hover:border-gray-500 text-gray-300'
}`;
button.textContent = behavior;
button.dataset.behavior = behavior;
button.addEventListener('click', function() {
toggleBehavior(behavior, this);
});
behaviorsGrid.appendChild(button);
});
// Toggle category visibility
categoryHeader.addEventListener('click', function() {
const icon = this.querySelector('i');
if (behaviorsGrid.classList.contains('max-h-0')) {
behaviorsGrid.classList.remove('max-h-0');
behaviorsGrid.classList.add('max-h-96');
icon.style.transform = 'rotate(180deg)';
} else {
behaviorsGrid.classList.add('max-h-0');
behaviorsGrid.classList.remove('max-h-96');
icon.style.transform = 'rotate(0deg)';
}
});
categoryDiv.appendChild(categoryHeader);
categoryDiv.appendChild(behaviorsGrid);
container.appendChild(categoryDiv);
});
feather.replace();
}
function toggleBehavior(behavior, button) {
let savedBehaviors = JSON.parse(localStorage.getItem('selectedBehaviors') || '[]');
if (savedBehaviors.includes(behavior)) {
// Remove behavior
savedBehaviors = savedBehaviors.filter(b => b !== behavior);
button.classList.remove('bg-indigo-500/20', 'border-indigo-400', 'text-indigo-400');
button.classList.add('bg-gray-900/50', 'border-gray-600', 'hover:border-gray-500', 'text-gray-300');
} else {
// Add behavior
savedBehaviors.push(behavior);
button.classList.remove('bg-gray-900/50', 'border-gray-600', 'hover:border-gray-500', 'text-gray-300');
button.classList.add('bg-indigo-500/20', 'border-indigo-400', 'text-indigo-400');
}
localStorage.setItem('selectedBehaviors', JSON.stringify(savedBehaviors));
updateBehaviorStats(savedBehaviors);
}
function updateBehaviorStats(selectedBehaviors) {
const statsContainer = document.getElementById('behaviorStats');
if (!statsContainer) return;
const total = selectedBehaviors.length;
const categories = {};
Object.entries(behaviorDatabase).forEach(([category, behaviors]) => {
const count = behaviors.filter(b => selectedBehaviors.includes(b)).length;
if (count > 0) {
categories[category] = count;
}
});
let statsHTML = `<div class="text-sm text-gray-400">พฤติกรรมที่เลือกวันนี้: <span class="text-indigo-400 font-semibold">${total} รายการ</span></div>`;
if (total > 0) {
statsHTML += '<div class="mt-2 flex flex-wrap gap-2">';
Object.entries(categories).forEach(([category, count]) => {
statsHTML += `<span class="px-2 py-1 bg-gray-700 rounded-full text-xs">${category}: ${count}</span>`;
});
statsHTML += '</div>';
}
statsContainer.innerHTML = statsHTML;
}