Spaces:
Running
Running
improve
Browse files- src/components/charts.ts +66 -38
src/components/charts.ts
CHANGED
|
@@ -230,47 +230,49 @@ const acwrAxisColorPlugin = {
|
|
| 230 |
// Register the axis color plugin
|
| 231 |
Chart.register(acwrAxisColorPlugin);
|
| 232 |
|
| 233 |
-
// Plugin to draw
|
| 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 |
-
// Register the
|
| 273 |
-
|
|
|
|
| 274 |
|
| 275 |
// Function to get activity emoji based on type
|
| 276 |
function getActivityEmoji(activityType: string): string {
|
|
@@ -458,6 +460,20 @@ const commonOptions = {
|
|
| 458 |
borderWidth: 1,
|
| 459 |
padding: 12,
|
| 460 |
displayColors: true,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
},
|
| 462 |
},
|
| 463 |
scales: {
|
|
@@ -474,8 +490,20 @@ const commonOptions = {
|
|
| 474 |
font: {
|
| 475 |
size: 10,
|
| 476 |
},
|
| 477 |
-
autoSkip:
|
| 478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
},
|
| 480 |
},
|
| 481 |
},
|
|
|
|
| 230 |
// Register the axis color plugin
|
| 231 |
Chart.register(acwrAxisColorPlugin);
|
| 232 |
|
| 233 |
+
// Plugin to draw one vertical lines per week
|
| 234 |
+
function weekGridPlugin(dayOfWeek = 1) { // 0:sunday, 1:monday, ... , 6:saturday
|
| 235 |
+
return {
|
| 236 |
+
id: 'weekGrid',
|
| 237 |
+
beforeDatasetsDraw(chart: Chart) {
|
| 238 |
+
const ctx = chart.ctx;
|
| 239 |
+
const chartArea = chart.chartArea;
|
| 240 |
+
const xScale = chart.scales['x'];
|
| 241 |
+
|
| 242 |
+
if (!xScale || !chartArea) return;
|
| 243 |
+
|
| 244 |
+
ctx.save();
|
| 245 |
+
ctx.strokeStyle = 'rgba(148, 163, 184, 0.3)';
|
| 246 |
+
ctx.lineWidth = 1;
|
| 247 |
+
|
| 248 |
+
// Draw a line for each matching day in the data
|
| 249 |
+
const labels = chart.data.labels || [];
|
| 250 |
+
labels.forEach((label, index) => {
|
| 251 |
+
if (typeof label === 'string') {
|
| 252 |
+
const [year, month, day] = label.split('-').map(Number);
|
| 253 |
+
const date = new Date(year, month - 1, day);
|
| 254 |
+
|
| 255 |
+
// If it's Monday, draw a vertical line
|
| 256 |
+
if (date.getDay() === dayOfWeek) {
|
| 257 |
+
const x = xScale.getPixelForValue(index);
|
| 258 |
+
if (x >= chartArea.left && x <= chartArea.right) {
|
| 259 |
+
ctx.beginPath();
|
| 260 |
+
ctx.moveTo(x, chartArea.top);
|
| 261 |
+
ctx.lineTo(x, chartArea.bottom);
|
| 262 |
+
ctx.stroke();
|
| 263 |
+
}
|
| 264 |
}
|
| 265 |
}
|
| 266 |
+
});
|
|
|
|
| 267 |
|
| 268 |
+
ctx.restore();
|
| 269 |
+
},
|
| 270 |
+
}
|
| 271 |
};
|
| 272 |
|
| 273 |
+
// Register the week grid plugin
|
| 274 |
+
const GRID_DAY_OF_WEEK = 0;//sunday
|
| 275 |
+
Chart.register(weekGridPlugin(GRID_DAY_OF_WEEK));
|
| 276 |
|
| 277 |
// Function to get activity emoji based on type
|
| 278 |
function getActivityEmoji(activityType: string): string {
|
|
|
|
| 460 |
borderWidth: 1,
|
| 461 |
padding: 12,
|
| 462 |
displayColors: true,
|
| 463 |
+
callbacks: {
|
| 464 |
+
label: function (context: any) {
|
| 465 |
+
const label = context.dataset.label || '';
|
| 466 |
+
const value = context.parsed.y;
|
| 467 |
+
|
| 468 |
+
// For daily values (scatter datasets), show only the value
|
| 469 |
+
if (label.includes('Daily')) {
|
| 470 |
+
return `${label}: ${value !== null ? value.toFixed(2) : 'N/A'}`;
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
// For other datasets, show default format
|
| 474 |
+
return `${label}: ${value !== null ? value.toFixed(2) : 'N/A'}`;
|
| 475 |
+
}
|
| 476 |
+
}
|
| 477 |
},
|
| 478 |
},
|
| 479 |
scales: {
|
|
|
|
| 490 |
font: {
|
| 491 |
size: 10,
|
| 492 |
},
|
| 493 |
+
autoSkip: false,
|
| 494 |
+
callback: function (_value: any, index: number): string {
|
| 495 |
+
const labels = (this as any).chart.data.labels || [];
|
| 496 |
+
const label = labels[index];
|
| 497 |
+
if (typeof label === 'string') {
|
| 498 |
+
const [year, month, day] = label.split('-').map(Number);
|
| 499 |
+
const date = new Date(year, month - 1, day);
|
| 500 |
+
// Show tick only on vertical grid lines
|
| 501 |
+
if (date.getDay() === GRID_DAY_OF_WEEK) {
|
| 502 |
+
return `${day}/${month}`;
|
| 503 |
+
}
|
| 504 |
+
}
|
| 505 |
+
return '';
|
| 506 |
+
},
|
| 507 |
},
|
| 508 |
},
|
| 509 |
},
|