Commit 955be6d002853ca9b704a33bb1854ca1e001e2f9

Authored by Antoine Goutenoir
1 parent 70d40a43
Exists in master

fix: infinite loop

Co-authored by: @Adrenesis
Showing 1 changed file with 53 additions and 8 deletions   Show diff stats
flaskr/static/js/plots/emissions-per-distance.js
... ... @@ -8,10 +8,20 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
8 8  
9 9  
10 10 function getTicks(maxValue, interval, startValue = 0) {
  11 + // console.log("getTicks", maxValue, interval, startValue);
  12 + if (0 === interval) {
  13 + console.error("No interval for ticks.");
  14 + return [0];
  15 + }
  16 + if (Math.sign(maxValue-startValue) !== Math.sign(interval)) {
  17 + console.warn("Wrong interval sign for ticks. Corrected.");
  18 + interval *= -1;
  19 + }
11 20 let range = [];
12 21 for (let i = startValue; i <= maxValue; i += interval) {
13 22 range.push(i);
14 23 }
  24 + // console.log("range", range);
15 25 return range;
16 26 }
17 27  
... ... @@ -21,8 +31,30 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
21 31 return range;
22 32 }
23 33  
24   - function getLeftTicks(maxemissions) {
25   - return getTicks(maxemissions, Math.floor((maxemissions / 8) / 1000) * 1000);
  34 + function getLeftTicks(maximumValue) {
  35 + maximumValue = 3200+1;
  36 + if (0 > maximumValue) {
  37 + console.error("Only positive values are supported on left axis.");
  38 + }
  39 + if (0 === maximumValue) {
  40 + return [0];
  41 + }
  42 + let ticksAmount = 8.0; // MUST BE > 2
  43 + let magnitude = 100000.0;
  44 + let interval = 0;
  45 + while (interval * (ticksAmount) < maximumValue) {
  46 + interval = (
  47 + Math.floor(
  48 + (maximumValue / (ticksAmount-1)) / magnitude
  49 + )
  50 + *
  51 + magnitude
  52 + );
  53 + //console.log("interval + magnitude", interval, magnitude);
  54 + magnitude *= 0.1;
  55 + }
  56 +
  57 + return getTicks(maximumValue, interval);
26 58 }
27 59  
28 60 function getRightTicks(maxemissionsPercent) {
... ... @@ -134,6 +166,7 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
134 166 }
135 167  
136 168 document.addEventListener("DOMContentLoaded", () => {
  169 + console.info("[Emissions Per Distance] Starting…");
137 170 width = Math.max(880, $(containerSelector).parent().width());
138 171 width = width - margin.left - margin.right;
139 172 let maxemissions = 0;
... ... @@ -163,7 +196,13 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
163 196 let attendeeNumber = trainAttendee + planeAttendee;
164 197 let distance_km = datum.distance_km / attendeeNumber;
165 198 let co2_kg = parseFloat(datum.co2_kg);
166   - if (co2_kg === "NaN" || distance_km / sliceThickness > 37 || distance_km === "NaN") {
  199 + if (
  200 + (co2_kg === "NaN")
  201 + ||
  202 + (distance_km === "NaN")
  203 + ||
  204 + (distance_km / sliceThickness > 37)
  205 + ) {
167 206 return;
168 207 }
169 208 rows.push(datum);
... ... @@ -173,6 +212,7 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
173 212 };
174 213  
175 214 const on_csv_ready = function () {
  215 + console.info("[Emissions Per Distance] Generating…");
176 216 for (let i = 0; i <= maxDistance / sliceThickness; i++) {
177 217 emissionsPerGroup[i] = 0;
178 218 attendeeNumberPerGroup[i] = 0;
... ... @@ -192,7 +232,6 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
192 232 maxemissionsPercent = Math.max(maxemissionsPercent, element / emissionsSum * 100.0)
193 233 });
194 234 maxDistance += 2000;
195   - // console.log(maxDistance);
196 235  
197 236 // Title
198 237 svg.append("text")
... ... @@ -207,7 +246,9 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
207 246 .domain([0, maxDistance])
208 247 .range([0, width]);
209 248 let xAxis = d3.axisBottom(x)
210   - .tickValues(getBottomTicks(maxDistance));
  249 + .ticks(11)
  250 + // .tickValues(getBottomTicks(maxDistance))
  251 + ;
211 252 svg.append("g")
212 253 .attr("transform", "translate(0," + height + ")")
213 254 .attr("class", "x axis")
... ... @@ -224,7 +265,9 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
224 265 .domain([0, maxemissions])
225 266 .range([height, 0]);
226 267 let ylAxis = d3.axisLeft(yl)
227   - .tickValues(getLeftTicks(maxemissions));
  268 + .ticks(13)
  269 + // .tickValues(getLeftTicks(maxemissions))
  270 + ;
228 271 svg.append("g")
229 272 .attr("class", "yl axis")
230 273 .call(ylAxis);
... ... @@ -234,7 +277,9 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
234 277 .domain([0, maxemissionsPercent])
235 278 .range([height, 0]);
236 279 let yrAxis = d3.axisRight(yr)
237   - .tickValues(getRightTicks(maxemissionsPercent));
  280 + .ticks(20)
  281 + // .tickValues(getRightTicks(maxemissionsPercent))
  282 + ;
238 283 svg.append("g")
239 284 .attr("transform", "translate(" + width + ", 0)")
240 285 .attr("class", "yr axis")
... ... @@ -268,7 +313,6 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
268 313 .thresholds(x.ticks(Math.floor(maxDistance / sliceThickness))); // then the numbers of bins
269 314  
270 315 let histolol = histogram(0);
271   - // console.log(histolol);
272 316 let barSettings = [];
273 317 emissionsPerGroup.forEach((element, index) => {
274 318 barSettings[index] = {
... ... @@ -306,6 +350,7 @@ function draw_emissions_per_distance(containerSelector, csvUrl) {
306 350 .style("z-index", "500")
307 351 .style("fill", "#4444E5");
308 352 addVerticalLineAndListenCursor(x, attendeeNumberPerGroup, attendeeSum);
  353 + console.info("[Emissions Per Distance] Done.");
309 354 };
310 355  
311 356 d3.csv(csvUrl, on_csv_datum)
... ...