-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_blank_charts_final.py
More file actions
81 lines (58 loc) · 2.7 KB
/
fix_blank_charts_final.py
File metadata and controls
81 lines (58 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
"""
Final fix: Add ctx definition exactly where it should be, matching exemplar structure.
"""
import re
from pathlib import Path
def fix_blank_charts(file_path):
"""Add ctx and DPI fix in exact exemplar structure."""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Find: const canvas = document.getElementById('severityChart');
# followed by blank lines, and replace with the correct structure
# Pattern to find the canvas line followed by whitespace
pattern = r"(// Create chart with custom plugin for colored squares\s*const canvas = document\.getElementById\('severityChart'\);)\s*\n\s*\n\s*\n\s*(let hoveredExpertIndex)"
# Replacement with ctx definition
replacement = r'''\1
const ctx = document.getElementById('severityChart').getContext('2d');
\2'''
content = re.sub(pattern, replacement, content)
# Now add the DPI fix after medianValuesPlugin closes
# Find the closing of medianValuesPlugin
dpi_fix = '''
// Fix blurry canvas on high-DPI displays
const dpr = window.devicePixelRatio || 1;
const canvas = document.getElementById('severityChart');
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
ctx.scale(dpr, dpr);
'''
# Pattern: end of medianValuesPlugin, before any // Track hover or chartCanvas references
# Look for the plugin closing followed by whitespace
plugin_pattern = r'(const medianValuesPlugin = \{[^}]*afterDatasetsDraw: \(chart\) => \{(?:[^{}]|\{[^{}]*\})*\}\s*\};)\s*\n(\s*const chartCanvas|// Track|let hoveredExpertIndex)'
# Check if DPI fix is already there
if '// Fix blurry canvas on high-DPI displays' not in content:
# Insert after the plugin
plugin_replacement = r'\1' + dpi_fix + '\n\2'
content = re.sub(plugin_pattern, plugin_replacement, content, flags=re.DOTALL)
# Write back
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Updated: {file_path.name}")
def main():
"""Process all severity chart files."""
# Find all BAU and PM severity charts
bau_charts = sorted(Path('.').glob('risk*_bau_chart.html'))
pm_charts = sorted(Path('.').glob('risk*_pm_chart.html'))
all_charts = bau_charts + pm_charts
if not all_charts:
print("No severity charts found!")
return
print(f"Found {len(all_charts)} severity charts")
print(f"Adding ctx definition and DPI fix...\n")
for chart_file in all_charts:
fix_blank_charts(chart_file)
print(f"\nCompleted! Updated {len(all_charts)} files.")
if __name__ == '__main__':
main()