Skip to content

Commit 86da620

Browse files
Propagate changes to plot template from pyfunnel
1 parent 60426f3 commit 86da620

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

buildingspy/templates/plot.html

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,45 @@
44
<link rel="icon" href="data:;base64,iVBORw0KGgo=" />
55
<title>$PAGE_TITLE</title>
66
<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>
720
</head>
821

922
<body>
10-
<div id="myDiv" style="height: $HEIGHT; width: 100%" class="plotly-graph-div"></div>
23+
<div id="myDiv" class="plotly-graph-div"></div>
1124
<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
1346
function loadCSV(url) {
1447
return fetch(url)
1548
.then((response) => {
@@ -18,38 +51,27 @@
1851
}
1952
return response.text();
2053
})
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));
4055
}
4156

4257
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+
4365
try {
4466
const srv_url = "http://localhost:$SERVER_PORT";
4567
const dict_var_info = $DICT_VAR_INFO;
4668
const dict_data = {};
4769
const avail_groups = [];
4870

4971
// 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>';
5173

52-
// Simple server accessibility check
74+
// Check if server is accessible first
5375
const serverResponse = await fetch(srv_url);
5476
if (!serverResponse.ok) {
5577
throw new Error(`Server not accessible (status: ${serverResponse.status})`);
@@ -88,7 +110,7 @@
88110
}
89111
}
90112

91-
console.log("DATA LOADING COMPLETED");
113+
console.log("Data loaded successfully");
92114

93115
// Check if we have any data to plot
94116
if (Object.keys(dict_data).length === 0) {
@@ -100,6 +122,11 @@
100122

101123
} catch (error) {
102124
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>`;
103130
}
104131
}
105132

@@ -114,6 +141,8 @@
114141
}
115142

116143
function plot(dict_data, dict_var_info, avail_groups) {
144+
console.log("Creating plot");
145+
117146
// Group variables by their groups
118147
const uniq_groups = [...new Set(avail_groups)];
119148
const flag_groups = [];
@@ -196,7 +225,7 @@
196225
};
197226

198227
// Calculate domains for data plots
199-
var divHeight = parseFloat(document.getElementById("myDiv").style.height) || 600;
228+
var divHeight = parseFloat(myDiv.style.height) || 600;
200229
var spacing = (0.02 * 100) / divHeight;
201230
var t_offset = err_plot_height + 2 * spacing;
202231

@@ -268,7 +297,7 @@
268297
layout["xaxis" + (nb_groups + 1)]["title"] = gen_x_axis_title;
269298
}
270299

271-
// Create the plot
300+
// Create the plot with a callback to handle completion
272301
Plotly.newPlot("myDiv", traces, layout, { responsive: true })
273302
.then(function() {
274303
console.log("Plot rendered successfully");
@@ -292,8 +321,10 @@
292321
// Function to check if Plotly is loaded and then make the plot
293322
function waitForPlotlyAndMakePlot() {
294323
if (typeof Plotly !== "undefined") {
324+
// Plotly is available, make the plot
295325
makePlot();
296326
} else {
327+
// Plotly not yet available, check again in 100ms
297328
setTimeout(waitForPlotlyAndMakePlot, 100);
298329
}
299330
}
@@ -302,6 +333,7 @@
302333
if (document.readyState === "loading") {
303334
document.addEventListener("DOMContentLoaded", waitForPlotlyAndMakePlot);
304335
} else {
336+
// DOM is already loaded, call immediately
305337
waitForPlotlyAndMakePlot();
306338
}
307339

@@ -335,4 +367,4 @@
335367
});
336368
</script>
337369
</body>
338-
</html>
370+
</html>

0 commit comments

Comments
 (0)