|
| 1 | +/** |
| 2 | + * Analysis functionality for the CodeClash trajectory viewer |
| 3 | + */ |
| 4 | + |
| 5 | +let lineCountChart = null; |
| 6 | +let analysisData = null; |
| 7 | + |
| 8 | +/** |
| 9 | + * Initialize the analysis functionality |
| 10 | + */ |
| 11 | +function initializeAnalysis() { |
| 12 | + const analysisDataElement = document.getElementById("analysis-data"); |
| 13 | + if (!analysisDataElement) { |
| 14 | + return; // No analysis data available |
| 15 | + } |
| 16 | + |
| 17 | + try { |
| 18 | + analysisData = JSON.parse(analysisDataElement.textContent); |
| 19 | + |
| 20 | + if ( |
| 21 | + analysisData && |
| 22 | + analysisData.all_files && |
| 23 | + analysisData.all_files.length > 0 |
| 24 | + ) { |
| 25 | + setupFileDropdown(); |
| 26 | + createLineCountChart(analysisData.all_files[0]); // Start with first file |
| 27 | + } |
| 28 | + } catch (error) { |
| 29 | + console.error("Error parsing analysis data:", error); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Setup the file dropdown change handler |
| 35 | + */ |
| 36 | +function setupFileDropdown() { |
| 37 | + const dropdown = document.getElementById("file-dropdown"); |
| 38 | + if (!dropdown) return; |
| 39 | + |
| 40 | + dropdown.addEventListener("change", function () { |
| 41 | + const selectedFile = this.value; |
| 42 | + if (selectedFile && lineCountChart) { |
| 43 | + updateLineCountChart(selectedFile); |
| 44 | + } |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Create the line count chart for a specific file |
| 50 | + */ |
| 51 | +function createLineCountChart(fileName) { |
| 52 | + const canvas = document.getElementById("line-count-chart"); |
| 53 | + if (!canvas || !analysisData) return; |
| 54 | + |
| 55 | + const ctx = canvas.getContext("2d"); |
| 56 | + |
| 57 | + // Destroy existing chart if it exists |
| 58 | + if (lineCountChart) { |
| 59 | + lineCountChart.destroy(); |
| 60 | + } |
| 61 | + |
| 62 | + const chartData = prepareChartData(fileName); |
| 63 | + |
| 64 | + lineCountChart = new Chart(ctx, { |
| 65 | + type: "line", |
| 66 | + data: chartData, |
| 67 | + options: { |
| 68 | + responsive: true, |
| 69 | + maintainAspectRatio: false, |
| 70 | + plugins: { |
| 71 | + title: { |
| 72 | + display: true, |
| 73 | + text: `Line Count Over Rounds: ${fileName}`, |
| 74 | + }, |
| 75 | + legend: { |
| 76 | + display: true, |
| 77 | + position: "top", |
| 78 | + }, |
| 79 | + }, |
| 80 | + scales: { |
| 81 | + x: { |
| 82 | + title: { |
| 83 | + display: true, |
| 84 | + text: "Round", |
| 85 | + }, |
| 86 | + type: "linear", |
| 87 | + position: "bottom", |
| 88 | + }, |
| 89 | + y: { |
| 90 | + title: { |
| 91 | + display: true, |
| 92 | + text: "Line Count", |
| 93 | + }, |
| 94 | + beginAtZero: true, |
| 95 | + }, |
| 96 | + }, |
| 97 | + interaction: { |
| 98 | + intersect: false, |
| 99 | + mode: "index", |
| 100 | + }, |
| 101 | + }, |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Update the existing chart with data for a new file |
| 107 | + */ |
| 108 | +function updateLineCountChart(fileName) { |
| 109 | + if (!lineCountChart || !analysisData) return; |
| 110 | + |
| 111 | + const chartData = prepareChartData(fileName); |
| 112 | + lineCountChart.data = chartData; |
| 113 | + lineCountChart.options.plugins.title.text = `Line Count Over Rounds: ${fileName}`; |
| 114 | + lineCountChart.update(); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Prepare chart data for a specific file |
| 119 | + */ |
| 120 | +function prepareChartData(fileName) { |
| 121 | + const datasets = []; |
| 122 | + const colors = [ |
| 123 | + "#FF6384", |
| 124 | + "#36A2EB", |
| 125 | + "#FFCE56", |
| 126 | + "#4BC0C0", |
| 127 | + "#9966FF", |
| 128 | + "#FF9F40", |
| 129 | + "#FF6384", |
| 130 | + "#C9CBCF", |
| 131 | + "#4BC0C0", |
| 132 | + "#FF6384", |
| 133 | + ]; |
| 134 | + |
| 135 | + let colorIndex = 0; |
| 136 | + |
| 137 | + // Get all rounds across all players |
| 138 | + const allRounds = new Set(); |
| 139 | + Object.values(analysisData.line_counts_by_round).forEach((playerData) => { |
| 140 | + Object.keys(playerData).forEach((round) => { |
| 141 | + allRounds.add(parseInt(round)); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + const sortedRounds = Array.from(allRounds).sort((a, b) => a - b); |
| 146 | + |
| 147 | + // Create dataset for each player |
| 148 | + Object.entries(analysisData.line_counts_by_round).forEach( |
| 149 | + ([playerName, playerData]) => { |
| 150 | + const data = []; |
| 151 | + |
| 152 | + sortedRounds.forEach((round) => { |
| 153 | + const roundData = playerData[round]; |
| 154 | + if (roundData && roundData[fileName] !== undefined) { |
| 155 | + data.push({ |
| 156 | + x: round, |
| 157 | + y: roundData[fileName], |
| 158 | + }); |
| 159 | + } else { |
| 160 | + // If file doesn't exist in this round, use 0 or previous value |
| 161 | + const prevValue = data.length > 0 ? data[data.length - 1].y : 0; |
| 162 | + data.push({ |
| 163 | + x: round, |
| 164 | + y: prevValue, |
| 165 | + }); |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + datasets.push({ |
| 170 | + label: playerName, |
| 171 | + data: data, |
| 172 | + borderColor: colors[colorIndex % colors.length], |
| 173 | + backgroundColor: colors[colorIndex % colors.length] + "20", // Add transparency |
| 174 | + borderWidth: 2, |
| 175 | + fill: false, |
| 176 | + tension: 0.1, |
| 177 | + }); |
| 178 | + |
| 179 | + colorIndex++; |
| 180 | + }, |
| 181 | + ); |
| 182 | + |
| 183 | + return { datasets }; |
| 184 | +} |
| 185 | + |
| 186 | +// Initialize when DOM is loaded |
| 187 | +document.addEventListener("DOMContentLoaded", function () { |
| 188 | + initializeAnalysis(); |
| 189 | +}); |
0 commit comments