Commit 1f7df547dfc2f357cccc077729b7b991120bdf0e
1 parent
76cbad19
Exists in
master
Hack SVG plot download support and fix a horrible bug.
Showing
2 changed files
with
52 additions
and
3 deletions
Show diff stats
flaskr/static/js/main.js
... | ... | @@ -0,0 +1,15 @@ |
1 | + | |
2 | + | |
3 | +function saveSvg(svgEl, name) { | |
4 | + svgEl.setAttribute("xmlns", "http://www.w3.org/2000/svg"); | |
5 | + var svgData = svgEl.outerHTML; | |
6 | + var preface = '<?xml version="1.0" standalone="no"?>\r\n'; | |
7 | + var svgBlob = new Blob([preface, svgData], {type:"image/svg+xml;charset=utf-8"}); | |
8 | + var svgUrl = URL.createObjectURL(svgBlob); | |
9 | + var downloadLink = document.createElement("a"); | |
10 | + downloadLink.href = svgUrl; | |
11 | + downloadLink.download = name; | |
12 | + document.body.appendChild(downloadLink); | |
13 | + downloadLink.click(); | |
14 | + document.body.removeChild(downloadLink); | |
15 | +} | |
0 | 16 | \ No newline at end of file | ... | ... |
flaskr/templates/estimation.html
... | ... | @@ -107,6 +107,14 @@ |
107 | 107 | Download Raw YAML |
108 | 108 | </a> |
109 | 109 | </li> |
110 | + <li class="nav-item m-4"> | |
111 | + <a id="cities_footprints_d3viz_lollipop_download" | |
112 | + href="#" | |
113 | + title="This may not work on IE nor Edge. Please use a decent browser like Firefox." | |
114 | + class="btn btn-lg btn-secondary"> | |
115 | + Download Plot SVG | |
116 | + </a> | |
117 | + </li> | |
110 | 118 | {# <li class="nav-item m-4">#} |
111 | 119 | {# <a href="/estimation/{{ estimation.public_id }}.xls" class="btn btn-lg btn-secondary disabled">#} |
112 | 120 | {# Download XLS#} |
... | ... | @@ -282,10 +290,18 @@ jQuery(document).ready(function($){ |
282 | 290 | var width = Math.max(880, $(vizid).parent().width()); |
283 | 291 | width = width - margin.left - margin.right; |
284 | 292 | |
285 | - var svg = d3.select(vizid) | |
293 | + var svg_tag = d3.select(vizid) | |
286 | 294 | .append("svg") |
287 | 295 | .attr("width", width + margin.left + margin.right) |
288 | - .attr("height", height + margin.top + margin.bottom) | |
296 | + .attr("height", height + margin.top + margin.bottom); | |
297 | + | |
298 | + // Add a background | |
299 | + svg_tag.append("rect") | |
300 | + .attr("width", "100%") | |
301 | + .attr("height", "100%") | |
302 | + .attr("fill", "#ffffff"); | |
303 | + | |
304 | + var svg = svg_tag | |
289 | 305 | .append("g") |
290 | 306 | .attr("transform", |
291 | 307 | "translate(" + margin.left + "," + margin.top + ")"); |
... | ... | @@ -300,7 +316,9 @@ jQuery(document).ready(function($){ |
300 | 316 | var x = d3.scaleLinear() |
301 | 317 | .domain([0, axis_x_max]) |
302 | 318 | .range([0, width]); |
319 | + {#.nice();#} | |
303 | 320 | svg.append("g") |
321 | + .attr("class", "axis-bottom") | |
304 | 322 | .attr("transform", "translate(0," + height + ")") |
305 | 323 | .call(d3.axisBottom(x)) |
306 | 324 | .selectAll("text") |
... | ... | @@ -315,6 +333,7 @@ jQuery(document).ready(function($){ |
315 | 333 | })) |
316 | 334 | .padding(1); |
317 | 335 | svg.append("g") |
336 | + .attr("class", "axis-left") | |
318 | 337 | .call(d3.axisLeft(y)); |
319 | 338 | |
320 | 339 | {#svg.append("g")#} |
... | ... | @@ -339,6 +358,7 @@ jQuery(document).ready(function($){ |
339 | 358 | // .attr("x1", function (d) { |
340 | 359 | // return x(d[x_key]); |
341 | 360 | // }) |
361 | + .attr("class", "stick") | |
342 | 362 | .attr("x1", x(0)) |
343 | 363 | .attr("x2", x(0)) |
344 | 364 | .attr("y1", function (d) { |
... | ... | @@ -423,6 +443,7 @@ jQuery(document).ready(function($){ |
423 | 443 | .attr("id", function (d, i) { |
424 | 444 | return "shadow_hover_value_" + i.toString(); |
425 | 445 | }) |
446 | + .attr("class", "value-text") | |
426 | 447 | .attr("font-size", 10) |
427 | 448 | .attr("text-anchor", compute_text_anchor) |
428 | 449 | .attr("transform", compute_text_transform) |
... | ... | @@ -437,6 +458,7 @@ jQuery(document).ready(function($){ |
437 | 458 | .attr("id", function (d, i) { |
438 | 459 | return "hover_value_" + i.toString(); |
439 | 460 | }) |
461 | + .attr("class", "value-text") | |
440 | 462 | .attr("font-size", 10) |
441 | 463 | .attr("text-anchor", compute_text_anchor) |
442 | 464 | .attr("transform", compute_text_transform) |
... | ... | @@ -453,7 +475,7 @@ jQuery(document).ready(function($){ |
453 | 475 | .attr("r", "4") |
454 | 476 | .delay(function (d, i) { return(i*animation_delay); }); |
455 | 477 | |
456 | - svg.selectAll("line") | |
478 | + svg.selectAll("line.stick") | |
457 | 479 | .transition() |
458 | 480 | .duration(animation_duration*0.618) |
459 | 481 | .attr("x1", function (d) { return x(d[x_key]); }) |
... | ... | @@ -465,6 +487,18 @@ jQuery(document).ready(function($){ |
465 | 487 | .attr("transform", "translate("+(-180+width/2.0)+","+(height+111)+")") |
466 | 488 | .text("CO\u2082 emissions equivalent (kg) per target city."); |
467 | 489 | |
490 | + // Download | |
491 | + $(vizid+"_download").click(function(e){ | |
492 | + e.stopPropagation(); | |
493 | + // Show the values | |
494 | + $(vizid+" svg .value-text").css("opacity", 1); | |
495 | + // This possibly won't work on IE and Edge | |
496 | + saveSvg($(vizid + " svg")[0], "travel_carbon_footprint_{{ estimation.public_id }}.svg"); | |
497 | + // Hide the values | |
498 | + $(vizid+" svg .value-text").css("opacity", 0); | |
499 | + return false; | |
500 | + }); | |
501 | + | |
468 | 502 | }); |
469 | 503 | |
470 | 504 | }); | ... | ... |