Commit 7d42e277735e5617e61f9e561c294daad810aa81
1 parent
5a31ca2a
Exists in
master
and in
4 other branches
Download chart csv
Showing
3 changed files
with
23 additions
and
7 deletions
Show diff stats
app/main/static/js/charges.js
... | ... | @@ -85,9 +85,10 @@ function build_chart(div_selector, data_url, entity_name, category_type) { |
85 | 85 | .style("opacity", 1) |
86 | 86 | tooltip_hamburger |
87 | 87 | .html("<span>Export </span>" + |
88 | - "<li>To CSV</li>" + | |
88 | + "<li id='to_csv'>To CSV</li>" + | |
89 | 89 | "<li id='to_png'>To PNG</li>") |
90 | 90 | d3.select("#to_png").on("click", download_png); |
91 | + d3.select("#to_csv").on("click", function(){download_csv(this, data_url);}) | |
91 | 92 | } else { |
92 | 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 | 15 | var download_png = function () { |
2 | 16 | // This callback is supposed to be called on the click event of a button child of the div containing the svg. |
3 | 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 | 28 | svgString2Image(svgString, 2 * width, 2 * height, 'png', save); // passes Blob and filesize String to the callback |
15 | 29 | |
16 | 30 | function save(dataBlob, filesize) { |
31 | + console.log(dataBlob) | |
17 | 32 | saveAs(dataBlob, chart_title); // FileSaver.js function |
18 | 33 | } |
19 | 34 | } |
... | ... | @@ -38,8 +53,8 @@ function getSVGString(svgNode) { |
38 | 53 | |
39 | 54 | // Add Parent element Id and Classes to the list |
40 | 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 | 58 | selectorTextArr.push('.' + parentElement.classList[c]); |
44 | 59 | } |
45 | 60 | } |
... | ... | @@ -52,7 +67,7 @@ function getSVGString(svgNode) { |
52 | 67 | selectorTextArr.push('#' + id); |
53 | 68 | |
54 | 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 | 71 | if (!contains('.' + classes[c], selectorTextArr)) |
57 | 72 | selectorTextArr.push('.' + classes[c]); |
58 | 73 | } |
... | ... | @@ -76,7 +91,7 @@ function getSVGString(svgNode) { |
76 | 91 | if (typeof cssRule.selectorText === 'undefined') { |
77 | 92 | continue; |
78 | 93 | } |
79 | - var classFromSelector = '.'+cssRule.selectorText.split('.')[1] | |
94 | + var classFromSelector = '.' + cssRule.selectorText.split('.')[1] | |
80 | 95 | if (contains(classFromSelector, selectorTextArr)) |
81 | 96 | extractedCSSText += cssRule.cssText; |
82 | 97 | } |
... | ... | @@ -103,7 +118,8 @@ function getSVGString(svgNode) { |
103 | 118 | function svgString2Image(svgString, width, height, format, callback) { |
104 | 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 | 124 | var canvas = document.createElement("canvas"); |
109 | 125 | var context = canvas.getContext("2d"); | ... | ... |
app/templates/charges-includes.html
... | ... | @@ -2,6 +2,5 @@ |
2 | 2 | src="https://cdn.rawgit.com/eligrey/canvas-toBlob.js/f1a01896135ab378aa5c0118eadd81da55e698d8/canvas-toBlob.js"></script> |
3 | 3 | <script |
4 | 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 | 5 | <script src="{{ url_for('main.static', filename='js/svg_to_png.js', version=config.VERSION) }}" type="text/javascript"></script> |
7 | 6 | <script src="{{ url_for('main.static', filename='js/charges.js', version=config.VERSION) }}" type="text/javascript"></script> | ... | ... |