Commit 7d42e277735e5617e61f9e561c294daad810aa81

Authored by hitier
1 parent 5a31ca2a

Download chart csv

app/main/static/js/charges.js
@@ -85,9 +85,10 @@ function build_chart(div_selector, data_url, entity_name, category_type) { @@ -85,9 +85,10 @@ function build_chart(div_selector, data_url, entity_name, category_type) {
85 .style("opacity", 1) 85 .style("opacity", 1)
86 tooltip_hamburger 86 tooltip_hamburger
87 .html("<span>Export </span>" + 87 .html("<span>Export </span>" +
88 - "<li>To CSV</li>" + 88 + "<li id='to_csv'>To CSV</li>" +
89 "<li id='to_png'>To PNG</li>") 89 "<li id='to_png'>To PNG</li>")
90 d3.select("#to_png").on("click", download_png); 90 d3.select("#to_png").on("click", download_png);
  91 + d3.select("#to_csv").on("click", function(){download_csv(this, data_url);})
91 } else { 92 } else {
92 d3.select(".tooltip_hamburger").remove(); 93 d3.select(".tooltip_hamburger").remove();
93 } 94 }
app/main/static/js/svg_to_png.js
  1 +var download_csv = function (node, rest_csv_url) {
  2 + var chart_div = node.parentNode.parentNode.parentNode;
  3 + var chart_title = chart_div.id;
  4 + fetch(rest_csv_url)
  5 + .then(function (r) {
  6 + r.headers.set('content-type', 'text/csv')
  7 + return r.blob()
  8 + })
  9 + .then((blob) => {
  10 + console.log(blob)
  11 + saveAs(blob, chart_title + '.csv'); // FileSaver.js function
  12 + })
  13 +}
  14 +
1 var download_png = function () { 15 var download_png = function () {
2 // This callback is supposed to be called on the click event of a button child of the div containing the svg. 16 // This callback is supposed to be called on the click event of a button child of the div containing the svg.
3 // We then get the parent of this btn to guess the chart's title, width and heigth 17 // We then get the parent of this btn to guess the chart's title, width and heigth
@@ -14,6 +28,7 @@ var download_png = function () { @@ -14,6 +28,7 @@ var download_png = function () {
14 svgString2Image(svgString, 2 * width, 2 * height, 'png', save); // passes Blob and filesize String to the callback 28 svgString2Image(svgString, 2 * width, 2 * height, 'png', save); // passes Blob and filesize String to the callback
15 29
16 function save(dataBlob, filesize) { 30 function save(dataBlob, filesize) {
  31 + console.log(dataBlob)
17 saveAs(dataBlob, chart_title); // FileSaver.js function 32 saveAs(dataBlob, chart_title); // FileSaver.js function
18 } 33 }
19 } 34 }
@@ -38,8 +53,8 @@ function getSVGString(svgNode) { @@ -38,8 +53,8 @@ function getSVGString(svgNode) {
38 53
39 // Add Parent element Id and Classes to the list 54 // Add Parent element Id and Classes to the list
40 selectorTextArr.push('#' + parentElement.id); 55 selectorTextArr.push('#' + parentElement.id);
41 - for (var c = 0; c < parentElement.classList.length; c++){  
42 - if (!contains('.' + parentElement.classList[c], selectorTextArr)){ 56 + for (var c = 0; c < parentElement.classList.length; c++) {
  57 + if (!contains('.' + parentElement.classList[c], selectorTextArr)) {
43 selectorTextArr.push('.' + parentElement.classList[c]); 58 selectorTextArr.push('.' + parentElement.classList[c]);
44 } 59 }
45 } 60 }
@@ -52,7 +67,7 @@ function getSVGString(svgNode) { @@ -52,7 +67,7 @@ function getSVGString(svgNode) {
52 selectorTextArr.push('#' + id); 67 selectorTextArr.push('#' + id);
53 68
54 var classes = nodes[i].classList; 69 var classes = nodes[i].classList;
55 - for (var c = 0; c < classes.length; c++){ 70 + for (var c = 0; c < classes.length; c++) {
56 if (!contains('.' + classes[c], selectorTextArr)) 71 if (!contains('.' + classes[c], selectorTextArr))
57 selectorTextArr.push('.' + classes[c]); 72 selectorTextArr.push('.' + classes[c]);
58 } 73 }
@@ -76,7 +91,7 @@ function getSVGString(svgNode) { @@ -76,7 +91,7 @@ function getSVGString(svgNode) {
76 if (typeof cssRule.selectorText === 'undefined') { 91 if (typeof cssRule.selectorText === 'undefined') {
77 continue; 92 continue;
78 } 93 }
79 - var classFromSelector = '.'+cssRule.selectorText.split('.')[1] 94 + var classFromSelector = '.' + cssRule.selectorText.split('.')[1]
80 if (contains(classFromSelector, selectorTextArr)) 95 if (contains(classFromSelector, selectorTextArr))
81 extractedCSSText += cssRule.cssText; 96 extractedCSSText += cssRule.cssText;
82 } 97 }
@@ -103,7 +118,8 @@ function getSVGString(svgNode) { @@ -103,7 +118,8 @@ function getSVGString(svgNode) {
103 function svgString2Image(svgString, width, height, format, callback) { 118 function svgString2Image(svgString, width, height, format, callback) {
104 var format = format ? format : 'png'; 119 var format = format ? format : 'png';
105 120
106 - var imgsrc = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svgString))); // Convert SVG string to data URL 121 + // Convert SVG string to data URL
  122 + var imgsrc = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svgString)));
107 123
108 var canvas = document.createElement("canvas"); 124 var canvas = document.createElement("canvas");
109 var context = canvas.getContext("2d"); 125 var context = canvas.getContext("2d");
app/templates/charges-includes.html
@@ -2,6 +2,5 @@ @@ -2,6 +2,5 @@
2 src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/f1a01896135ab378aa5c0118eadd81da55e698d8/canvas-toBlob.js"></script> 2 src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/f1a01896135ab378aa5c0118eadd81da55e698d8/canvas-toBlob.js"></script>
3 <script 3 <script
4 src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script> 4 src="https://cdn.rawgit.com/eligrey/FileSaver.js/e9d941381475b5df8b7d7691013401e171014e89/FileSaver.min.js"></script>
5 -<script src="https://d3js.org/d3.v6.min.js"></script>  
6 <script src="{{ url_for('main.static', filename='js/svg_to_png.js', version=config.VERSION) }}" type="text/javascript"></script> 5 <script src="{{ url_for('main.static', filename='js/svg_to_png.js', version=config.VERSION) }}" type="text/javascript"></script>
7 <script src="{{ url_for('main.static', filename='js/charges.js', version=config.VERSION) }}" type="text/javascript"></script> 6 <script src="{{ url_for('main.static', filename='js/charges.js', version=config.VERSION) }}" type="text/javascript"></script>