-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
426 lines (379 loc) · 12.8 KB
/
script.js
File metadata and controls
426 lines (379 loc) · 12.8 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Data storage with mock data
let runs = JSON.parse(localStorage.getItem("runs") || "[]");
// Add mock data if no runs exist
// if (runs.length === 0) {
// const mockRuns = generateMockData();
// runs = mockRuns;
// localStorage.setItem("runs", JSON.stringify(runs));
// }
// Generate 4 weeks of mock running data
function generateMockData() {
const mockData = [];
const runTypes = ["easy", "tempo", "long"];
const surfaces = ["road", "trail"];
const today = new Date();
// Generate runs for the past 28 days
for (let daysAgo = 27; daysAgo >= 0; daysAgo--) {
// Skip some days randomly (rest days)
if (Math.random() > 0.75) continue;
const runDate = new Date(today);
runDate.setDate(today.getDate() - daysAgo);
runDate.setHours(Math.floor(Math.random() * 12) + 6); // Morning runs between 6-18
// Determine run type based on day pattern
let runType;
const dayOfWeek = runDate.getDay();
if (dayOfWeek === 0) {
// Sunday - long run
runType = "long";
} else if (dayOfWeek === 2 || dayOfWeek === 4) {
// Tuesday/Thursday - tempo
runType = Math.random() > 0.5 ? "tempo" : "easy";
} else {
runType = "easy";
}
// Set distance based on run type
let distance;
if (runType === "long") {
distance = 10 + Math.random() * 6; // 10-16 miles
} else if (runType === "tempo") {
distance = 5 + Math.random() * 3; // 5-8 miles
} else {
distance = 3 + Math.random() * 4; // 3-7 miles
}
// Set pace based on run type
let paceMinutes, paceSeconds;
if (runType === "tempo") {
paceMinutes = 7 + Math.floor(Math.random() * 1);
paceSeconds = Math.floor(Math.random() * 60);
} else if (runType === "long") {
paceMinutes = 8 + Math.floor(Math.random() * 2);
paceSeconds = Math.floor(Math.random() * 60);
} else {
paceMinutes = 8 + Math.floor(Math.random() * 2);
paceSeconds = Math.floor(Math.random() * 60);
}
const pace = `${paceMinutes}:${paceSeconds.toString().padStart(2, "0")}`;
// Mix surfaces with preference for road
const surface = Math.random() > 0.3 ? "road" : "trail";
mockData.push({
id: Date.now() + Math.random() * 1000,
date: runDate.toISOString(),
distance: parseFloat(distance.toFixed(1)),
pace: pace,
runType: runType,
surface: surface,
});
}
return mockData.sort((a, b) => new Date(a.date) - new Date(b.date));
}
// Chart instances
let weeklyChart = null;
let surfaceChart = null;
let runTypeChart = null;
// Initialize app
document.addEventListener("DOMContentLoaded", function () {
updateStats();
updateRunsTable();
initCharts();
updateCharts();
});
// Add run form handler
document.getElementById("runForm").addEventListener("submit", function (e) {
e.preventDefault();
const run = {
id: Date.now(),
date: new Date().toISOString(),
distance: parseFloat(document.getElementById("distance").value),
pace: document.getElementById("pace").value,
runType: document.getElementById("runType").value,
surface: document.getElementById("surface").value,
};
runs.push(run);
localStorage.setItem("runs", JSON.stringify(runs));
// Reset form
this.reset();
// Update UI
updateStats();
updateRunsTable();
updateCharts();
// Show success animation
showNotification("Run added successfully!");
});
// Update statistics
function updateStats() {
const now = new Date();
const weekStart = new Date(now);
weekStart.setDate(now.getDate() - now.getDay());
weekStart.setHours(0, 0, 0, 0);
let weekTotal = 0;
let weekRoad = 0;
let weekTrail = 0;
let roadTotal = 0;
let trailTotal = 0;
runs.forEach((run) => {
const runDate = new Date(run.date);
// This week's totals
if (runDate >= weekStart) {
weekTotal += run.distance;
if (run.surface === "road") {
weekRoad += run.distance;
} else {
weekTrail += run.distance;
}
}
// All-time totals
if (run.surface === "road") {
roadTotal += run.distance;
} else {
trailTotal += run.distance;
}
});
document.getElementById("weekTotal").textContent = weekTotal.toFixed(1);
document.getElementById("weekRoad").textContent = weekRoad.toFixed(1);
document.getElementById("weekTrail").textContent = weekTrail.toFixed(1);
document.getElementById("roadTotal").textContent = roadTotal.toFixed(1);
document.getElementById("trailTotal").textContent = trailTotal.toFixed(1);
document.getElementById("combinedTotal").textContent = (roadTotal + trailTotal).toFixed(1);
}
// Update runs table
function updateRunsTable() {
const tbody = document.getElementById("runsTable");
tbody.innerHTML = "";
// Sort runs by date (newest first)
const sortedRuns = [...runs].sort((a, b) => new Date(b.date) - new Date(a.date));
// Show only last 10 runs
sortedRuns.slice(0, 10).forEach((run) => {
const row = tbody.insertRow();
const runDate = new Date(run.date);
row.innerHTML = `
<td class="py-2">${runDate.toLocaleDateString()}</td>
<td class="py-2">${run.distance} mi</td>
<td class="py-2">${run.pace}</td>
<td class="py-2">
<span class="px-2 py-1 rounded text-xs ${
run.runType === "easy"
? "bg-green-500/20 text-green-400"
: run.runType === "tempo"
? "bg-yellow-500/20 text-yellow-400"
: "bg-red-500/20 text-red-400"
}">${run.runType}</span>
</td>
<td class="py-2">
<span class="px-2 py-1 rounded text-xs ${
run.surface === "road"
? "bg-blue-500/20 text-blue-400"
: "bg-green-500/20 text-green-400"
}">${run.surface}</span>
</td>
<td class="py-2">
<button onclick="deleteRun(${run.id})" class="text-red-400 hover:text-red-300">
Delete
</button>
</td>
`;
});
}
// Delete run
function deleteRun(id) {
if (confirm("Are you sure you want to delete this run?")) {
runs = runs.filter((run) => run.id !== id);
localStorage.setItem("runs", JSON.stringify(runs));
updateStats();
updateRunsTable();
updateCharts();
showNotification("Run deleted");
}
}
// Initialize charts
function initCharts() {
// Weekly mileage chart
const weeklyCtx = document.getElementById("weeklyChart").getContext("2d");
weeklyChart = new Chart(weeklyCtx, {
type: "line",
data: {
labels: [],
datasets: [
{
label: "Weekly Mileage",
data: [],
borderColor: "rgb(147, 51, 234)",
backgroundColor: "rgba(147, 51, 234, 0.1)",
tension: 0.4,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false,
},
},
scales: {
y: {
beginAtZero: true,
grid: {
color: "rgba(255, 255, 255, 0.1)",
},
ticks: {
color: "rgba(255, 255, 255, 0.7)",
},
},
x: {
grid: {
color: "rgba(255, 255, 255, 0.1)",
},
ticks: {
color: "rgba(255, 255, 255, 0.7)",
},
},
},
},
});
// Surface distribution chart
const surfaceCtx = document.getElementById("surfaceChart").getContext("2d");
surfaceChart = new Chart(surfaceCtx, {
type: "doughnut",
data: {
labels: ["Road", "Trail"],
datasets: [
{
data: [0, 0],
backgroundColor: [
"rgba(59, 130, 246, 0.8)",
"rgba(34, 197, 94, 0.8)",
],
borderColor: [
"rgb(59, 130, 246)",
"rgb(34, 197, 94)",
],
borderWidth: 2,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "bottom",
labels: {
color: "rgba(255, 255, 255, 0.7)",
},
},
},
},
});
// Run type distribution chart
const runTypeCtx = document.getElementById("runTypeChart").getContext("2d");
runTypeChart = new Chart(runTypeCtx, {
type: "doughnut",
data: {
labels: ["Easy", "Tempo", "Long"],
datasets: [
{
data: [0, 0, 0],
backgroundColor: [
"rgba(34, 197, 94, 0.8)",
"rgba(234, 179, 8, 0.8)",
"rgba(239, 68, 68, 0.8)",
],
borderColor: [
"rgb(34, 197, 94)",
"rgb(234, 179, 8)",
"rgb(239, 68, 68)",
],
borderWidth: 2,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "bottom",
labels: {
color: "rgba(255, 255, 255, 0.7)",
},
},
},
},
});
}
// Update charts
function updateCharts() {
// Calculate weekly totals for last 8 weeks
const weeklyData = calculateWeeklyTotals();
weeklyChart.data.labels = weeklyData.labels;
weeklyChart.data.datasets[0].data = weeklyData.data;
weeklyChart.update();
// Calculate surface distribution
let roadMiles = 0;
let trailMiles = 0;
runs.forEach((run) => {
if (run.surface === "road") {
roadMiles += run.distance;
} else {
trailMiles += run.distance;
}
});
surfaceChart.data.datasets[0].data = [roadMiles, trailMiles];
surfaceChart.update();
// Calculate run type distribution
let easyMiles = 0;
let tempoMiles = 0;
let longMiles = 0;
runs.forEach((run) => {
if (run.runType === "easy") {
easyMiles += run.distance;
} else if (run.runType === "tempo") {
tempoMiles += run.distance;
} else {
longMiles += run.distance;
}
});
runTypeChart.data.datasets[0].data = [easyMiles, tempoMiles, longMiles];
runTypeChart.update();
}
// Calculate weekly totals
function calculateWeeklyTotals() {
const weeks = 8;
const labels = [];
const data = [];
const now = new Date();
for (let i = weeks - 1; i >= 0; i--) {
const weekEnd = new Date(now);
weekEnd.setDate(now.getDate() - i * 7);
const weekStart = new Date(weekEnd);
weekStart.setDate(weekEnd.getDate() - 7);
let weekTotal = 0;
runs.forEach((run) => {
const runDate = new Date(run.date);
if (runDate >= weekStart && runDate <= weekEnd) {
weekTotal += run.distance;
}
});
labels.push(`Week ${weeks - i}`);
data.push(weekTotal);
}
return { labels, data };
}
// Show notification
function showNotification(message) {
const notification = document.createElement("div");
notification.className =
"fixed top-4 right-4 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg transform translate-x-full transition-transform duration-300";
notification.textContent = message;
document.body.appendChild(notification);
setTimeout(() => {
notification.style.transform = "translateX(0)";
}, 100);
setTimeout(() => {
notification.style.transform = "translateX(100%)";
setTimeout(() => {
document.body.removeChild(notification);
}, 300);
}, 3000);
}