1

我正在尝试在 Node.js 中使用 D3 和 Rickshaw。该服务必须创建一个时间序列图,然后使用运行 imagemagick 的子进程将原始 SVG 转换为 png。我真的只需要一个原始的 svg 字符串来进行转换。 人力车有什么方法可以访问原始 SVG 字符串吗?

4

1 回答 1

2

Rickshawd3在内部使用,您可以像在真实 DOM 中一样获取 SVG 字符串:

var Rickshaw = require('rickshaw'),
    jsdom = require('jsdom');

jsdom.env({
    features: { QuerySelector: true },
    html: '<html><body><div id="graph"></div></body></html>',
    done: function (err, window) {
        this.window = window;
        this.document = window.document;
        // classListShim(window.self); // <-- see comment below

        var graph = new Rickshaw.Graph({
            series: [ { data: [ { x: 0, y: 2 }, { x: 1, y: 4 }] } ],
            renderer: 'area',
            element: window.document.querySelector('#graph')
        });

        graph.render();
        // This will output the `svg` element in HTML format.
        console.log(window.document.querySelector('#graph').innerHTML);
    }
});

但是,由于当前jsdom不支持classList,您需要使用应用在 上的classListshimwindow.self

完整代码:https ://gist.github.com/musically-ut/9154438

于 2014-02-22T12:58:49.620 回答