简体   繁体   English

d3.js下载图为svg图像

[英]d3.js download graph as svg image

I have been lookig around for a way to download the generated svg from d3.js and I either end up with phantom.js which seems kinda overkill (or at least intimidating given the "simplicity" of the problem) or svg-crowbar.js which is apparently only for chrome (I need firefox). 我一直在寻找从d3.js下载生成的svg的方法,我最终得到了phantom.js,这看起来有点矫枉过正(或者至少令人生畏,因为问题的“简单性”)或者svg-crowbar.js这显然只适用于chrome(我需要firefox)。

I also found the following code: 我还发现了以下代码:

//Encode the SVG
var serializer = new XMLSerializer();
var xmlString = serializer.serializeToString(d3.select('svg').node());
var imgData = 'data:image/svg+xml;base64,' + btoa(xmlString);
//Use the download attribute (or a shim) to provide a link
<a href="'+imgData+'" download="download">Download</a>

on https://groups.google.com/forum/#!topic/d3-js/RnORDkLeS-Q https://groups.google.com/forum/#!topic/d3-js/RnORDkLeS-Q

Which should download the svg (if I could get it to work). 哪个应该下载svg(如果我可以让它工作)。 I was thinking that instead of supplying a download button, clicking a certain svg-element should also do? 我在想,而不是提供下载按钮,单击某个svg-element也应该这样做? I currently have the following code but it doesn't work: 我目前有以下代码,但它不起作用:

 var svg = d3.select(elementid).append("svg")
                        .attr("width", 500)
                        .attr("height", 500)
                        .append("g")

            var serializer = new XMLSerializer();
            var xmlString = serializer.serializeToString(d3.select('svg').node());
            var imgData = 'data:image/svg+xml;base64,' + btoa(xmlString);
            //Use the download attribute (or a shim) to provide a link

                    svg.append("rect")
                            .attr("x", 20)
                            .attr("y", 20)
                            .attr("width", 130)
                            .attr("height", 160)
                            .attr("fill", "red")
                            .attr("id", "rectLabel")
                            .attr('xlink:href',imgData);

the svg only draws the rectangle should allow you to download the svg (with the rectangle) as an .svg file when the rectangle is pressed. svg只绘制矩形应允许您在按下矩形时将svg(带矩形)下载为.svg文件。 I don't know if I am on the right track here. 我不知道我是否在正确的轨道上。

I am very new to d3.js but basically I am looking for a possible fix / alternative for client side d3.js svg download in Firefox. 我是d3.js的新手,但基本上我正在为Firefox中的客户端d3.js svg下载寻找可能的修复/替代方案。 Preferably I would have the download "button" as part of the svg. 我最好将下载“按钮”作为svg的一部分。

thanks in advance! 提前致谢!

Ok , I have settled with just allowing the specification of a button (with downloadID) and have added this in the code after creating the svg. 好吧,我已经决定只允许指定按钮(带有downloadID)并在创建svg后在代码中添加了这个。

            if (p.graph.svg.downloadID != undefined){
                var serializer = new XMLSerializer();
                var xmlString = serializer.serializeToString(d3.select('svg').node());
                var imgData = 'data:image/svg+xml;base64,' + btoa(xmlString);

                function writeDownloadLink(){
                    var html = d3.select(elementid).select("svg")
                            .attr("title", "svg_title")
                            .attr("version", 1.1)
                            .attr("xmlns", "http://www.w3.org/2000/svg")
                            .node().parentNode.innerHTML;  

                    d3.select(this)
                            .attr("href-lang", "image/svg+xml")
                            .attr("href", "data:image/svg+xml;base64,\n" + btoa(unescape(encodeURIComponent(html))))
                };

                var idselector = "#"+p.graph.svg.downloadID;

                d3.select(idselector)
                        .on("mouseover", writeDownloadLink);

            } 

Not what I had in mind at first but works for me. 不是我一开始的想法,但对我有用。

I've modified the svg-crowbar script to be a function called downloadSVG() that can be called from within your script. 我已经将svg-crowbar脚本修改为一个名为downloadSVG()的函数,可以在脚本中调用它。 The function downloads SVG(s) on the webpage. 该功能在网页上下载SVG。 The function can be found at: https://bitbucket.org/mas29/public_resources/raw/b9bafa002053b4609bd5186010d19e959cba33d4/scripts/js/svg_download/downloadSVG.js . 该功能可在以下网址找到: https//bitbucket.org/mas29/public_resources/raw/b9bafa002053b4609bd5186010d19e959cba33d4/scripts/js/svg_download/downloadSVG.js

Let's say you have a button that, when pushed, should download the SVG(s). 假设您有一个按钮,当按下该按钮时,应该下载SVG。 Just tack on the downloadSVG() as part of the "click" function, like so: 只需将downloadSVG()作为“click”功能的一部分,就像这样:

d3.select("body").append("button")
        .attr("type","button")
        .attr("class", "downloadButton")
        .text("Download SVG")
        .on("click", function() {
            // download the svg
            downloadSVG();
        });

I found this block to have the best solution. 我发现这个块有最好的解决方案。

  • Markup: 标记:

     <a id="download" href="#">Download SVG</button> 
  • javasript: javasript:

     d3.select("#download").on("click", function() { d3.select(this) .attr("href", 'data:application/octet-stream;base64,' + btoa(d3.select("#line").html())) .attr("download", "viz.svg") }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM