-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_data.js
More file actions
143 lines (117 loc) · 4.85 KB
/
Copy pathload_data.js
File metadata and controls
143 lines (117 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function getLastDays(days) {
const dates = [];
const mostRecentDate = new Date();
for (let i = 0; i < days; i++) {
const date = new Date(mostRecentDate);
date.setDate(mostRecentDate.getDate() - i);
const dateString = date.toISOString().split('T')[0];
dates.push(dateString);
}
return dates;
}
async function loadCsvFile(filename) {
try {
console.log(`📥 Fetching: ${filename}.csv`);
const response = await fetch(`https://raw.githubusercontent.com/D3rhami/milli-gold-capture/master/database/${filename}.csv`);
if (!response.ok) {
console.warn(`⚠️ File ${filename}.csv not found`);
return [];
}
const csvData = await response.text();
const lines = csvData.split('\n');
const data = [];
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
const commaIndex = line.indexOf(',');
if (commaIndex === -1) continue;
const priceStr = line.substring(0, commaIndex);
const dateStr = line.substring(commaIndex + 1);
const date = new Date(dateStr).getTime();
const price = parseFloat(priceStr);
if (!isNaN(date) && !isNaN(price)) {
data.push([date, price]);
}
}
console.log(`✅ Loaded: ${filename}.csv (${data.length} records)`);
return data;
} catch (error) {
console.error(`❌ Error loading ${filename}.csv:`, error);
return [];
}
}
function showLoadingIndicator() {
const chartContainer = document.querySelector("#chart");
if (chartContainer) {
chartContainer.innerHTML = `
<div style="display: flex; justify-content: center; align-items: center; height: 385px; font-family: 'Vazirmatn', sans-serif;">
<div style="text-align: center;">
<div style="border: 4px solid #f3f3f3; border-top: 4px solid #FFD700; border-radius: 50%; width: 40px; height: 40px; animation: spin 1s linear infinite; margin: 0 auto 20px;"></div>
<div style="color: #666; font-size: 16px;">در حال بارگذاری دادهها...</div>
</div>
</div>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
`;
}
}
async function loadDataForRange(range) {
console.log(`🔄 Loading data for range: ${range}`);
showLoadingIndicator();
let filesToLoad = [];
switch (range) {
case '1h':
console.log('📊 1 Hour selected - fetching today only');
filesToLoad = getLastDays(1);
break;
case '1d':
console.log('📊 1 Day selected - fetching today + yesterday');
filesToLoad = getLastDays(2);
break;
case '1w':
console.log('📊 1 Week selected - fetching 8 days (today + 7 days before)');
filesToLoad = getLastDays(8);
break;
case '1m':
console.log('📊 1 Month selected - fetching 30 days');
filesToLoad = getLastDays(30);
break;
default:
console.log('📊 Default - fetching today + yesterday');
filesToLoad = getLastDays(2);
}
console.log(`📋 Files to load: [${filesToLoad.join(', ')}]`);
const allData = [];
let loadedFiles = 0;
let foundFiles = 0;
for (const filename of filesToLoad) {
const data = await loadCsvFile(filename);
if (data.length > 0) {
for (let i = 0; i < data.length; i++) {
allData.push(data[i]);
}
foundFiles++;
}
loadedFiles++;
console.log(`📈 Progress: ${loadedFiles}/${filesToLoad.length} files loaded (${foundFiles} found)`);
}
allData.sort((a, b) => a[0] - b[0]);
if (allData.length === 0) {
console.log('⚠️ No data found for any of the requested files');
return [];
}
console.log(`🎯 Total data points loaded: ${allData.length} from ${foundFiles} available files`);
console.log(`⏱️ Data range: ${new Date(allData[0]?.[0]).toLocaleString('fa-IR')} to ${new Date(allData[allData.length - 1]?.[0]).toLocaleString('fa-IR')}`);
if (foundFiles < filesToLoad.length) {
console.log(`ℹ️ Showing available data: ${foundFiles}/${filesToLoad.length} files found - displaying what we have`);
}
return allData;
}
async function loadInitialData() {
console.log('🏠 Initial page load - loading today + yesterday for optimal performance');
return await loadDataForRange('1d');
}