我正在使用d3.js可视化一些数据。我希望能够获取它生成的SVG代码并将其存储为.SVG图像文件(用于在Inkscape/Illustrator中编辑)
我尝试过简单地复制svg标记的内容,即
<;svg>;
<--剪断-->;
</svg>;
输入一个名为image.svg的文件,但这会遗漏颜色/样式信息,这是在两个单独的CSS文件中
我正在处理这个例子
有没有一个简单的方法可以做到这一点
这里有一个很好的方法,可以使用svg-crowbar.js在您的站点上提供一个按钮,允许用户将您的可视化下载为svg
1)定义按钮的CSS:
。下载{
背景:#333;
颜色:#FFF;
字号:900;
边框:2倍实心#B10000;
填充:4px;
保证金:4倍;
}
2)定义按钮的HTML/JS:
<;i class=“download”href=“javascript:(函数(){var e=document.createElement('script');if(window.location.protocol=='https:'){e.setAttribute('src','https://rawgit.com/NYTimes/svg-crowbar/gh-pages/svg-crowbar.js');}else{e.setAttribute('src','http://nytimes.github.com/svg-crowbar/svg-crowbar.js');}e.setAttribute('class','svg crowbar');document.body.appendChild(e);})(;”>;<--⤋-->;<;大>;⇩<;/big>;下载<;/i>;
下面是对同一个javascript的详细介绍:
javascript:(函数(){
var e=document.createElement('script');
如果(window.location.protocol==='https:'){
e、 setAttribute('src','https://rawgit.com/NYTimes/svg-crowbar/gh-pages/svg-crowbar.js');
}否则{
e、 setAttribute('src','http://nytimes.github.com/svg-crowbar/svg-crowbar.js');
}
e、 setAttribute('class','svg crowbar');
文件.正文.附件(e);
})();
3)完成。这将生成一个svg下载,Inkscape可以打开
注意:svg-crowbar.js是从https://rawgit.com 或http://nytimes.github.com; 您可能更愿意将其集成到您的网站/文件夹中