我正在尝试在Node中使用D3和人力车。该服务必须创建一个时序图,然后使用运行imagemagick的子进程将原始SVG转换为png。我真的只需要一个原始的svg字符串即可进行转换。人力车有什么办法可以访问原始的SVG字符串吗?
人力车在d3
内部使用,您可以像在真正的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
,因此您需要使用应用于的classList
填充程序window.self
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句