|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Final fix: Apply the working structure from exemplar. |
| 4 | +1. Only ctx at the start (no canvas) |
| 5 | +2. medianValuesPlugin |
| 6 | +3. DPI fix (which defines canvas) |
| 7 | +4. Chart creation |
| 8 | +5. Event handlers AFTER chart |
| 9 | +""" |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | +import re |
| 13 | + |
| 14 | +def fix_chart_file(file_path): |
| 15 | + """Fix a single chart file.""" |
| 16 | + |
| 17 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 18 | + content = f.read() |
| 19 | + |
| 20 | + # Step 1: Remove any existing "const canvas = " line that comes before the DPI fix |
| 21 | + # This is the line that comes right before "const ctx" |
| 22 | + content = re.sub( |
| 23 | + r' const canvas = document\.getElementById\(\'severityChart\'\);\n const ctx = document\.getElementById\(\'severityChart\'\)\.getContext\(\'2d\'\);', |
| 24 | + r' const ctx = document.getElementById(\'severityChart\').getContext(\'2d\');', |
| 25 | + content |
| 26 | + ) |
| 27 | + |
| 28 | + # Step 2: Ensure the DPI fix is in the right place and defines canvas |
| 29 | + # Remove any existing DPI fix |
| 30 | + content = re.sub( |
| 31 | + r'\n // Fix blurry canvas on high-DPI displays\n const dpr = window\.devicePixelRatio \|\| 1;\n const (rect|canvas) = .*?\n .*?\n .*?\n ctx\.scale\(dpr, dpr\);', |
| 32 | + '', |
| 33 | + content, |
| 34 | + flags=re.DOTALL |
| 35 | + ) |
| 36 | + |
| 37 | + # Add DPI fix right before chart creation |
| 38 | + dpi_fix = ''' |
| 39 | + // Fix blurry canvas on high-DPI displays |
| 40 | + const dpr = window.devicePixelRatio || 1; |
| 41 | + const canvas = document.getElementById('severityChart'); |
| 42 | + const rect = canvas.getBoundingClientRect(); |
| 43 | + canvas.width = rect.width * dpr; |
| 44 | + canvas.height = rect.height * dpr; |
| 45 | + ctx.scale(dpr, dpr); |
| 46 | +
|
| 47 | +''' |
| 48 | + |
| 49 | + # Insert before "const chart = new Chart" |
| 50 | + content = re.sub( |
| 51 | + r'( const chart = new Chart\(ctx,)', |
| 52 | + dpi_fix + r'\1', |
| 53 | + content |
| 54 | + ) |
| 55 | + |
| 56 | + # Step 3: Move mouse event handlers after chart creation |
| 57 | + # First, extract the handlers |
| 58 | + mouse_handler_pattern = r'(\n // Add mouse move handler for hover interaction\n const chartCanvas = document\.getElementById\(\'severityChart\'\);\n const tooltip = document\.getElementById\(\'expertTooltip\'\);.*?chartCanvas\.addEventListener\(\'mouseleave\'.*?\n \}\);\n)' |
| 59 | + |
| 60 | + match = re.search(mouse_handler_pattern, content, re.DOTALL) |
| 61 | + |
| 62 | + if match: |
| 63 | + handlers = match.group(1) |
| 64 | + # Remove from current location |
| 65 | + content = content.replace(handlers, '') |
| 66 | + |
| 67 | + # Insert right before "// UI Controls" |
| 68 | + content = re.sub( |
| 69 | + r'(\n // UI Controls)', |
| 70 | + handlers + r'\1', |
| 71 | + content |
| 72 | + ) |
| 73 | + |
| 74 | + # Write back |
| 75 | + with open(file_path, 'w', encoding='utf-8') as f: |
| 76 | + f.write(content) |
| 77 | + |
| 78 | + print(f"Fixed: {file_path.name}") |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + """Process all severity chart files.""" |
| 83 | + |
| 84 | + # Find all BAU and PM severity charts |
| 85 | + bau_charts = sorted(Path('.').glob('risk*_bau_chart.html')) |
| 86 | + pm_charts = sorted(Path('.').glob('risk*_pm_chart.html')) |
| 87 | + |
| 88 | + all_charts = bau_charts + pm_charts |
| 89 | + |
| 90 | + if not all_charts: |
| 91 | + print("No severity charts found!") |
| 92 | + return |
| 93 | + |
| 94 | + print(f"Found {len(all_charts)} severity charts") |
| 95 | + print(f"Applying final fix...\n") |
| 96 | + |
| 97 | + for chart_file in all_charts: |
| 98 | + fix_chart_file(chart_file) |
| 99 | + |
| 100 | + print(f"\nCompleted!") |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + main() |
0 commit comments