1

I have plotted some graphs using d3.js. In d3.tsv function I have passed a URL to a php script that performs some query on database and returns output to the client in TSV format.

e.g.:

d3.tsv("getData.php", function(error, data) {
      data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.close = +d.close;
      });

Is there some way to put a download button along with plotting graph so that when user clicks on the button, the data gets downloaded in a file at client side without making request to "getData.php" again. In other words, I don't want to fetch data again from the server. Instead I want to use data that was fetched previously while plotting the graph.

Thank you in advance for your valuable advice(s).

Vaibhav
  • 11
  • 2

2 Answers2

0

This looks like it might help: http://blogs.adobe.com/cantrell/archives/2012/01/how-to-download-data-as-a-file-from-javascript.html

I’m currently working on an HTML/JavaScript application that allows you to author content entirely on the client. I want to let users download that content and save it locally, but without bouncing it off a server. After some trial and error, I have it working fairly well using a data URI. Rather than explain it, it’s probably easiest just to show the code:

HTML:

<a href="javascript:onDownload();">Download</a>

JavaScript code:

function onDownload() { document.location = 'data:Application/octet-stream,' + encodeURIComponent(dataToDownload); }

The only limitation is that I can’t figure out a way to give the downloaded file a name (and have concluded that it’s not currently possible, though I’m happy to be proven wrong). I’ve only tested the code in Safari and Chrome, and in both cases, the file name defaults to "download" (with no extension). All the data is in the file, but it’s not a very intuitive experience for the end user.

amp13
  • 1
0

I figured it out. I used Downloadify tool to do that work for me.

https://github.com/dcneiner/Downloadify

Vaibhav
  • 11
  • 2