4
4
< link rel ="icon " href ="data:;base64,iVBORw0KGgo= " />
5
5
< title > $PAGE_TITLE</ title >
6
6
< script src ="https://cdn.plot.ly/plotly-3.0.1.min.js " charset ="utf-8 "> </ script >
7
+ < style >
8
+ html ,
9
+ body {
10
+ height : 100% ;
11
+ margin : 0 ;
12
+ padding : 0 ;
13
+ }
14
+ # myDiv {
15
+ height : $HEIGHT;
16
+ width: 100% ;
17
+ min-height : 500px ;
18
+ }
19
+ </ style >
7
20
</ head >
8
21
9
22
< body >
10
- < div id ="myDiv " style =" height: $HEIGHT; width: 100% " class ="plotly-graph-div "> </ div >
23
+ < div id ="myDiv " class ="plotly-graph-div "> </ div >
11
24
< script >
12
- // Simple CSV loading function similar to plot_bck.html
25
+ // Helper function to parse CSV text into JSON
26
+ function csvToJson ( csvText ) {
27
+ const lines = csvText . split ( "\n" ) ;
28
+ const headers = lines [ 0 ] . split ( "," ) ;
29
+
30
+ return lines
31
+ . slice ( 1 )
32
+ . filter ( ( line ) => line . trim ( ) !== "" )
33
+ . map ( ( line ) => {
34
+ const values = line . split ( "," ) ;
35
+ const row = { } ;
36
+
37
+ headers . forEach ( ( header , index ) => {
38
+ row [ header . trim ( ) ] = values [ index ] ? values [ index ] . trim ( ) : "" ;
39
+ } ) ;
40
+
41
+ return row ;
42
+ } ) ;
43
+ }
44
+
45
+ // Simple CSV loading function
13
46
function loadCSV ( url ) {
14
47
return fetch ( url )
15
48
. then ( ( response ) => {
18
51
}
19
52
return response . text ( ) ;
20
53
} )
21
- . then ( ( text ) => {
22
- // Simple CSV parsing
23
- const lines = text . split ( "\n" ) ;
24
- const headers = lines [ 0 ] . split ( "," ) ;
25
- const result = [ ] ;
26
-
27
- for ( let i = 1 ; i < lines . length ; i ++ ) {
28
- if ( ! lines [ i ] . trim ( ) ) continue ;
29
- const values = lines [ i ] . split ( "," ) ;
30
- const row = { } ;
31
-
32
- headers . forEach ( ( header , j ) => {
33
- row [ header . trim ( ) ] = values [ j ] ? values [ j ] . trim ( ) : "" ;
34
- } ) ;
35
-
36
- result . push ( row ) ;
37
- }
38
- return result ;
39
- } ) ;
54
+ . then ( ( text ) => csvToJson ( text ) ) ;
40
55
}
41
56
42
57
async function makePlot ( ) {
58
+ console . log ( "Starting plot generation" ) ;
59
+ const myDiv = document . getElementById ( "myDiv" ) ;
60
+ if ( ! myDiv ) {
61
+ console . error ( "Plot container not found" ) ;
62
+ return ;
63
+ }
64
+
43
65
try {
44
66
const srv_url = "http://localhost:$SERVER_PORT" ;
45
67
const dict_var_info = $DICT_VAR_INFO ;
46
68
const dict_data = { } ;
47
69
const avail_groups = [ ] ;
48
70
49
71
// Show loading indicator
50
- document . getElementById ( " myDiv" ) . innerHTML = '<div style="text-align:center;padding-top:100px;">Loading data...</div>' ;
72
+ myDiv . innerHTML = '<div style="text-align:center;padding-top:100px;">Loading data...</div>' ;
51
73
52
- // Simple server accessibility check
74
+ // Check if server is accessible first
53
75
const serverResponse = await fetch ( srv_url ) ;
54
76
if ( ! serverResponse . ok ) {
55
77
throw new Error ( `Server not accessible (status: ${ serverResponse . status } )` ) ;
88
110
}
89
111
}
90
112
91
- console . log ( "DATA LOADING COMPLETED " ) ;
113
+ console . log ( "Data loaded successfully " ) ;
92
114
93
115
// Check if we have any data to plot
94
116
if ( Object . keys ( dict_data ) . length === 0 ) {
100
122
101
123
} catch ( error ) {
102
124
console . error ( "Error loading or processing data:" , error ) ;
125
+ let errorMessage = error . message ;
126
+ if ( error . message . includes ( 'Failed to fetch' ) || error . message . includes ( 'ERR_CONNECTION_REFUSED' ) ) {
127
+ errorMessage = 'Server connection failed. Make sure the Python server is running.' ;
128
+ }
129
+ myDiv . innerHTML = `<div style="text-align:center;padding-top:100px;color:red;">Error: ${ errorMessage } </div>` ;
103
130
}
104
131
}
105
132
114
141
}
115
142
116
143
function plot ( dict_data , dict_var_info , avail_groups ) {
144
+ console . log ( "Creating plot" ) ;
145
+
117
146
// Group variables by their groups
118
147
const uniq_groups = [ ...new Set ( avail_groups ) ] ;
119
148
const flag_groups = [ ] ;
196
225
} ;
197
226
198
227
// Calculate domains for data plots
199
- var divHeight = parseFloat ( document . getElementById ( " myDiv" ) . style . height ) || 600 ;
228
+ var divHeight = parseFloat ( myDiv . style . height ) || 600 ;
200
229
var spacing = ( 0.02 * 100 ) / divHeight ;
201
230
var t_offset = err_plot_height + 2 * spacing ;
202
231
268
297
layout [ "xaxis" + ( nb_groups + 1 ) ] [ "title" ] = gen_x_axis_title ;
269
298
}
270
299
271
- // Create the plot
300
+ // Create the plot with a callback to handle completion
272
301
Plotly . newPlot ( "myDiv" , traces , layout , { responsive : true } )
273
302
. then ( function ( ) {
274
303
console . log ( "Plot rendered successfully" ) ;
292
321
// Function to check if Plotly is loaded and then make the plot
293
322
function waitForPlotlyAndMakePlot ( ) {
294
323
if ( typeof Plotly !== "undefined" ) {
324
+ // Plotly is available, make the plot
295
325
makePlot ( ) ;
296
326
} else {
327
+ // Plotly not yet available, check again in 100ms
297
328
setTimeout ( waitForPlotlyAndMakePlot , 100 ) ;
298
329
}
299
330
}
302
333
if ( document . readyState === "loading" ) {
303
334
document . addEventListener ( "DOMContentLoaded" , waitForPlotlyAndMakePlot ) ;
304
335
} else {
336
+ // DOM is already loaded, call immediately
305
337
waitForPlotlyAndMakePlot ( ) ;
306
338
}
307
339
335
367
} ) ;
336
368
</ script >
337
369
</ body >
338
- </ html >
370
+ </ html >
0 commit comments