[英]Saving d3js plot locally
I am creating a plot using d3js. 我正在使用d3js创建图。 I also want to give the ability to the user to save that plot locally at his computer. 我还希望使用户能够在自己的计算机上本地保存该图。 Therefore, I used a slightly changed version of the code mentioned at this question to create a downloadable picture of that plot. 因此,我使用此问题中提到的代码的稍有变化的版本来创建该图的可下载图片。
So I have the following code: 所以我有以下代码:
$("svg").attr({ version: '1.1' , xmlns:"http://www.w3.org/2000/svg"});
var svg = $("#chart-main").html(); var b64 = btoa(unescape(encodeURIComponent(svg)));//Base64.encode(svg); // or use btoa if supported
// Works in recent Webkit(Chrome)
$("body").append($("<img src='data:image/svg+xml;base64,\n"+b64+"' alt='file.svg'/>"));
// Works in Firefox 3.6 and Webit and possibly any browser which supports the data-uri
$("body").append($("<a href-lang='image/svg+xml' href='data:image/svg+xml;base64,\n"+b64+"' title='file.svg'>Download</a>"));
However, when I press on the download link I get the following error: This page contains the following errors: 但是,当我按下载链接时,出现以下错误:此页面包含以下错误:
error on line 6 at column 16: 第16行第6行出现错误
Entity 'nbsp' not defined 未定义实体“ nbsp”
The link is like data:image/svg+xml;base64,CgkJCQkJP....CgkJCQk=
链接就像data:image/svg+xml;base64,CgkJCQkJP....CgkJCQk=
How can I fix that? 我该如何解决?
The problem is that
问题是
is an HTML entity. 是HTML实体。
A stand-alone SVG file, as an XML format, only supports the five pre-defined XML entities: &
一个独立的SVG文件(一种XML格式)仅支持五个预定义的XML实体: &
, "
, "
, '
, '
, <
, <
, and >
和>
. 。
To represent any other unicode character, however, you can use a character reference based on the unicode value. 但是,要表示任何其他unicode字符,您可以使用基于unicode值的字符引用。 The code for a non-breaking space is  
不间断空格的代码是 
. 。 Use this to replace
使用它来替换
and your SVG file should validate. 并且您的SVG文件应经过验证。
PS I find FileFormat.info's character search site useful for looking up Unicode values. PS我发现FileFormat.info的字符搜索站点对于查找Unicode值很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.