I have came across many topics concerning the various ways of exporting d3.js powered visuals into an image file. Those solutions may work for me sometimes, but I have a big motivation for finding an offline/IDE equivalent to this. In my case, there is a lot of time when I'm programming without access to the internet, but would still greatly benefit from being able to export a static PNG of the graph for use in documents/reports.
Even if I was to copy the relevant libraries to my local host, I don't think I could export an image. If I'm not mistaken, the available solutions thus far need the internet for queuing up the file. Here is what I mean, this is a snippet from one such method:
function getSVGString( svgNode ) {
svgNode.setAttribute('xlink', 'http://www.w3.org/1999/xlink');
var cssStyleText = getCSSStyles( svgNode );
appendCSS( cssStyleText, svgNode )
var serializer = new XMLSerializer();
var svgString = serializer.serializeToString(svgNode);
svgString = svgString.replace(/(\w+)?:?xlink=/g, 'xmlns:xlink=') // Fix root xlink without namespace
svgString = svgString.replace(/NS\d+:href/g, 'xlink:href') // Safari NS namespace fix
Another reason is if I don't need to serve the file to users, I simply want to have a PNG for my own uses, it would seem to me that there would be an even simpler way to achieve this. And thus I wouldn't have to go through such elaborate steps as in the above example.
I could be wrong, either way, please let me know your thoughts on this.
Update:
Separate Attempt: Try #2
This one kind of works, the png downloads to drive at least, but it's not cropped right. Most of the graph is cropped out. I'm not sure how to fix that. I'm also not sure if the file type can be specified. I would like to choose download as 'graph.svg' sometimes too.
Update:
My persistent monkey see monkey do approach is getting me some progress. I can now save as png with the correct cropping. However, I cannot save the graph as an SVG. Which is funny, because that is kind of its original format. I would like to award this question to someone who can demonstrate with one of my live examples above how to save the graph as an svg.
On a side note, I'm including a very lengthy blob and file saver function within my graph's index.html and using a button to start the download. This is mostly because I'm against the clock and I'm starting to get desperate. Ultimately, if there was a more elegant solution that didn't require so many external libraries that would be great. Let me also add this approach is not ideal for multiple graphs. I currently have to add the button all functions to every index.html that has a graph I'd like to convert. I'm not a computer, and I can't iterate fast enough.