Save / export stand-alone SVG-file with legend #1936
Replies: 2 comments
-
Please let's open questions in this space ("discussions"), and keep issues for bug reports. Plot generates both SVG (for the chart itself) and HTML (for the legends, title, subtitle…), so you need something that exports HTML to the output format that suits you best. A browser can save a PDF, for example. The bubkoo/html-to-image package is also great (this is what the observable website uses for its "download as png" feature. I'm still looking for a good generic "html to svg" package, but in the meantime I've developed a partial something that works for me: (See also observablehq/feedback#541) |
Beta Was this translation helpful? Give feedback.
-
Thanks. I tried Allow me to ask why you do that? Is there a technical reason why the legend and title are not a part of the main SVG? I normally use Matplotlib for Python where these are just a part of the main plot. The caption is not a part of the plot, as it is usually a longer text written below the plot. The reason I want to be able to save the plot as a stand-alone SVG-file, is so the users of my web-site can easily save and use the plot elsewhere, e.g. on other web-forums, in Word / PDF documents, etc. Regarding where to post different kinds of issues here on GitHub, you may want to make a little template that people see when they create a new issue, so they know where to post, and what info they should provide you with. I can see you work very hard, so I want to make it as easy as possible for you to deal with the issues I bring up. |
Beta Was this translation helpful? Give feedback.
-
I searched and there are a few issues that relate to this, but they apparently don't solve my problem.
I'm using Observable Plot in a web-site and I am having trouble saving / exporting the SVG that is generated by Plot when there is a legend.
This is essentially my HTML file, which has a button that uses the well-known
filesaver.js
script to save the plot as an SVG-file:This works and saves the contents to a file. But I cannot open the file in e.g. InkScape or even in the web-browser, which gives the error: "This XML file does not appear to have any style information associated with it. The document tree is shown below."
The contents of the file start with
<figure class="plot-d6a7b5-figure" style="max-width: initial;">
. Then there are some<svg>
elements for the legend. Further down the main SVG element begins with<svg class="plot-d6a7b5" fill="currentColor" font-family="system-ui, sans-serif" font-size="10" text-anchor="middle" width="640" height="400" viewBox="0 0 640 400">
.I tried removing everything before the main
<svg>
element and adding thexmlns
attribute like so:<svg ... xmlns="http://www.w3.org/2000/svg">
, and then it worked for the main<svg>
element, but I cannot get it to work for the legend.I also tried adding
<!--?xml version="1.0" encoding="utf-8"?-->
to the start of the SVG-file, but that doesn't seem to matter.I don't know much about the SVG format. So what is the correct way of extracting the SVG generated by Observable Plot and saving it to a stand-alone SVG-file that can be opened in other programs?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions