I am using d3.js to visualize some data. I would like to be able to take the SVG code that it generates and store it as a .svg
image file (for editing in Inkscape/Illustrator).
I have tried simply copying the contents of the svg tag i.e.
<svg>
<!--snip-->
</svg>
into a file called image.svg, but this misses out on the color/styling information, which is in two separate CSS files.
I'm working with this example.
Is there a simple way to do this?
I think SVG Crowbar may be the best way to deal with this.
Documentation
This is late, but with D3.js it would be simple to inline the CSS. You would do something like:
My function quantize is just a quick hack for demonstration, but you could look at colorbrewer to work out the logic for applying quantiles to colors.
There's an extension called "svg-grabber" (svg export is another option) you can try it, it works by just clicking the extension icon, this is the result for stackoverflow page:
Here's a nice way to use svg-crowbar.js to provide a button on your site to allow your users to download your visualization as svg.
1) Define your button's CSS:
2) Define your button's HTML/JS:
Here's a closer look at that same javascript:
3) You're done. This produces an svg download that Inkscape can open.
Note: svg-crowbar.js is loaded from https://rawgit.com or http://nytimes.github.com; you may prefer to integrate it into your website/folder.
This works for me in Chrome v16b and Safari v5.1 on Windows: http://phrogz.net/SVG/chloropleth.html
All I did was use the Developer Tools to Copy as HTML the SVG node, paste it into a blank document, and add the links to the two CSS files. Does this appear correctly for you in Safari?
Edit: Here it is as a standalone SVG file: http://phrogz.net/SVG/chloropleth.svg
For this I added the
xmlns
attribute to the<svg>
and the external links:Again, verified to work in Chrome and Safari.