D3js: how to generate standalone SVG files? (Nodejs)

前端 未结 5 1646
执念已碎
执念已碎 2020-12-24 03:13

Given a D3js code, such as:

5条回答
  •  遥遥无期
    2020-12-24 03:40

    I recently wanted to do just that and asked a question here. I was pointed to phantomJS. Using PhantomJS, I created a JS -

    svggen.js:

    var page = require('webpage').create(),
        url = 'http://www.example.com/wordcloud.html';
    
    page.open(url, function (status) {
        if (status !== 'success') {
            console.log('Unable to access network');
        } else {
            var svgData = page.evaluate(function(s){
                    var serializer = new XMLSerializer();
                    var element = document.getElementById("svg1");
                    return serializer.serializeToString(element);
            });
            console.log(""+svgData);
        }
        phantom.exit();
    });
    

    wordcloud.html:

    
    
    
    
    
    
    
    

    Then I run

    phantomjs svggen.js > svgFile.svg
    

    The resulting svgFile.svg is a standalone SVG File. For d3cloud check this.

提交回复
热议问题