function createRatingChart(container, history) { const width = container.clientWidth - 40; const height = 250; const margin = { top: 20, right: 30, bottom: 40, left: 60 }; const chartWidth = width - margin.left - margin.right; const chartHeight = height - margin.top - margin.bottom; const pdgaNumber = container.id.replace('chart-', ''); const tooltip = document.getElementById(`tooltip-${pdgaNumber}`); const ratings = history.map(h => h.rating); const minRating = Math.min(...ratings) - 10; const maxRating = Math.max(...ratings) + 10; const xStep = chartWidth / (history.length - 1 || 1); const yScale = chartHeight / (maxRating - minRating); let svg = ``; svg += ``; svg += ``; for (let i = 0; i <= 5; i++) { const y = margin.top + (i * chartHeight / 5); const rating = Math.round(maxRating - (i * (maxRating - minRating) / 5)); svg += ``; svg += `${rating}`; } let pathData = ''; const points = []; history.forEach((point, i) => { const x = margin.left + (i * xStep); const y = margin.top + ((maxRating - point.rating) * yScale); points.push({ x, y, rating: point.rating, date: point.displayDate }); if (i === 0) { pathData += `M ${x} ${y}`; } else { pathData += ` L ${x} ${y}`; } }); svg += ``; points.forEach((point, i) => { svg += ``; svg += ``; }); const labelStep = Math.max(1, Math.floor(history.length / 6)); history.forEach((point, i) => { if (i % labelStep === 0 || i === history.length - 1) { const x = margin.left + (i * xStep); const date = new Date(point.date).toLocaleDateString('en-US', { month: 'short', year: '2-digit' }); svg += `${date}`; } }); svg += `Rating`; svg += ''; container.innerHTML = svg; setTimeout(() => { const svgElement = document.getElementById(`svg-${pdgaNumber}`); if (svgElement) { const hoverAreas = svgElement.querySelectorAll('.hover-area'); const dataPoints = svgElement.querySelectorAll('.data-point'); let currentTooltip = null; let tooltipTimeout = null; hoverAreas.forEach((area, i) => { area.addEventListener('mouseenter', (e) => { if (tooltipTimeout) { clearTimeout(tooltipTimeout); tooltipTimeout = null; } if (currentTooltip !== null && currentTooltip !== i) { dataPoints[currentTooltip].setAttribute('r', '4'); dataPoints[currentTooltip].setAttribute('fill', '#007bff'); } currentTooltip = i; const point = points[i]; tooltip.innerHTML = `${point.date}
Rating: ${point.rating}`; tooltip.style.display = 'block'; tooltip.style.left = `${e.clientX + 15}px`; tooltip.style.top = `${e.clientY - 35}px`; dataPoints[i].setAttribute('r', '6'); dataPoints[i].setAttribute('fill', '#0056b3'); }); area.addEventListener('mousemove', (e) => { if (currentTooltip === i) { tooltip.style.left = `${e.clientX + 15}px`; tooltip.style.top = `${e.clientY - 35}px`; } }); area.addEventListener('mouseleave', () => { if (currentTooltip === i) { tooltipTimeout = setTimeout(() => { tooltip.style.display = 'none'; dataPoints[i].setAttribute('r', '4'); dataPoints[i].setAttribute('fill', '#007bff'); currentTooltip = null; }, 100); } }); }); } }, 100); }