Fix Google Charts loading for AJAX-loaded bar graph

The issue was that google.load() doesn't work reliably when called
from AJAX-loaded content. The callback wasn't firing.

Solution:
- Load Google Charts library once in main application.html.erb layout
- Bar graph partial now just polls for google.visualization to be ready
- Uses retry logic (50 attempts @ 100ms = 5 second timeout)
- Returns success/failure boolean for proper flow control
- Removed duplicate script loading from partial

This ensures Google Charts is available globally for all chart views
(bar graphs, pie charts, performance charts) without timing issues.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Ken Johnson
2025-12-07 02:31:36 -05:00
parent b47a70d8b8
commit 8abf409041
4 changed files with 37 additions and 30 deletions
+28 -30
View File
@@ -1,25 +1,17 @@
<div id="column_chart"></div> <div id="column_chart"></div>
<!-- Google Visualization JS -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript"> <script type="text/javascript">
// Prevent multiple initializations
if (typeof window.barChartLoaded === 'undefined') {
window.barChartLoaded = false;
}
function drawChart3() { function drawChart3() {
// Guard against calling before Google Charts is loaded // Guard against calling before Google Charts is loaded
if (typeof google === 'undefined' || !google.visualization) { if (typeof google === 'undefined' || !google.visualization) {
console.warn('Google Charts not loaded yet'); console.warn('Google Charts not loaded yet, waiting...');
return; return false;
} }
var chartElement = document.getElementById('column_chart'); var chartElement = document.getElementById('column_chart');
if (!chartElement) { if (!chartElement) {
console.warn('Chart element not found'); console.warn('Chart element not found');
return; return false;
} }
var data = google.visualization.arrayToDataTable([ var data = google.visualization.arrayToDataTable([
@@ -59,27 +51,33 @@ function drawChart3() {
var chart = new google.visualization.ColumnChart(chartElement); var chart = new google.visualization.ColumnChart(chartElement);
chart.draw(data, options); chart.draw(data, options);
console.log('Bar chart drawn successfully');
return true;
} }
function initializeBarChart() { // Wait for Google Charts to be ready, then draw
// Check if Google Charts visualization is already loaded function initBarChart() {
if (typeof google !== 'undefined' && google.visualization && google.visualization.ColumnChart) { var attempts = 0;
drawChart3(); var maxAttempts = 50; // 5 seconds max
window.barChartLoaded = true;
} else if (typeof google !== 'undefined' && google.load && !window.barChartLoaded) { var checkAndDraw = function() {
// Load Google Charts if (drawChart3()) {
google.load("visualization", "1", { // Success!
packages: ["corechart"], return;
callback: function() { }
drawChart3();
window.barChartLoaded = true; attempts++;
} if (attempts < maxAttempts) {
}); setTimeout(checkAndDraw, 100);
} else { } else {
console.error('Google JSAPI not available'); console.error('Failed to load Google Charts after ' + (maxAttempts * 100) + 'ms');
} }
};
checkAndDraw();
} }
// Initialize immediately (page is already loaded when this partial loads via AJAX) // Initialize when partial loads
initializeBarChart(); console.log('Bar chart partial loaded, waiting for Google Charts...');
initBarChart();
</script> </script>
+9
View File
@@ -29,6 +29,15 @@
<!-- Bootstrap JS - loaded last --> <!-- Bootstrap JS - loaded last -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- Google Charts - load once globally for dashboard charts -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load Google Charts library once for the entire application
if (typeof google !== 'undefined' && google.load) {
google.load("visualization", "1", {packages:["corechart"]});
}
</script>
<!-- Modern Design System --> <!-- Modern Design System -->
<style> <style>
:root { :root {
Binary file not shown.
Binary file not shown.