Add expandable rating history charts with interactive tooltips

- Implement clickable player rows to expand/collapse rating history
- Add rating history scraping from PDGA history pages
- Create custom SVG line charts showing rating progression over time
- Add interactive tooltips with date and rating on hover
- Include visual highlights when hovering over data points
- Implement anti-flicker tooltip system with delayed hiding
- Add large hover areas (12px radius) for better user experience
- Show grid lines, axis labels, and responsive chart scaling
- Cache rating history data to avoid repeated API calls

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Samuel Enocsson
2025-08-12 12:40:15 +02:00
parent 2d55651f43
commit 2ab4869fb9
2 changed files with 338 additions and 3 deletions
+103
View File
@@ -423,6 +423,109 @@ app.get('/api/ratings/progress', (req, res) => {
});
});
async function fetchRatingHistory(pdgaNumber) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'www.pdga.com',
port: 443,
path: `/player/${pdgaNumber}/history`,
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
},
timeout: 30000
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode === 200) {
resolve(data);
} else {
reject(new Error(`HTTP ${res.statusCode}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
req.on('timeout', () => {
req.destroy();
reject(new Error('Request timeout'));
});
req.setTimeout(30000);
req.end();
});
}
function parseRatingHistory(html) {
const history = [];
// Find all table rows with rating data
const rowMatches = html.match(/<tr[^>]*>[\s\S]*?<\/tr>/gi);
if (rowMatches) {
for (const row of rowMatches) {
// Skip header rows and empty rows
if (row.includes('<th') || !row.includes('<td')) continue;
// Extract date, rating, and rounds from table cells
const cellMatches = row.match(/<td[^>]*>(.*?)<\/td>/gi);
if (cellMatches && cellMatches.length >= 2) {
const dateText = cellMatches[0].replace(/<[^>]*>/g, '').trim();
const ratingText = cellMatches[1].replace(/<[^>]*>/g, '').trim();
// Parse date (DD-Mon-YYYY format)
const dateMatch = dateText.match(/(\d{1,2})-([A-Za-z]{3})-(\d{4})/);
if (dateMatch && !isNaN(parseInt(ratingText))) {
const [, day, month, year] = dateMatch;
const monthMap = {
'Jan': 0, 'Feb': 1, 'Mar': 2, 'Apr': 3, 'May': 4, 'Jun': 5,
'Jul': 6, 'Aug': 7, 'Sep': 8, 'Oct': 9, 'Nov': 10, 'Dec': 11
};
const date = new Date(parseInt(year), monthMap[month], parseInt(day));
history.push({
date: date.toISOString().split('T')[0], // YYYY-MM-DD format
rating: parseInt(ratingText),
displayDate: dateText
});
}
}
}
}
// Sort by date (oldest first for chart display)
return history.sort((a, b) => new Date(a.date) - new Date(b.date));
}
app.get('/api/rating-history/:pdgaNumber', async (req, res) => {
try {
const { pdgaNumber } = req.params;
console.log(`Fetching rating history for PDGA ${pdgaNumber}...`);
const html = await fetchRatingHistory(pdgaNumber);
const history = parseRatingHistory(html);
res.json({
pdgaNumber: parseInt(pdgaNumber),
history
});
} catch (error) {
console.error('Error fetching rating history:', error.message);
res.status(500).json({ error: 'Failed to fetch rating history' });
}
});
app.post('/api/predicted-rating/:pdgaNumber', async (req, res) => {
let browser = null;
try {