export-svg-chart

0.2.1 • Public • Published

export-svg-chart npm Build Status dependency Status devDependency Status

Converts SVG+CSS charts built with front-end technologies like D3 into SVG/PNG Node buffers for server-side use.

Install

npm install export-svg-chart --save

Example

To get SVG and PNG exports of your chart, you need to point the library towards a page that takes care of rendering it. This can be a dynamic rendering with UI elements, or an "export/print"-specific page with another skin for the chart.

const fs = require('fs');
const exportSvgChart = require('export-svg-chart');
 
const options = {
    url: 'http://bl.ocks.org/mbostock/raw/7341714/',
    selector: '.chart',
};
 
exportSvgChart(options, (err, buffers) => {
    if (err) return console.log(err);
 
    fs.writeFile('basic-example.svg', buffers.svg);
    fs.writeFile('basic-example.png', buffers.pngs);
});

http://bl.ocks.org/mbostock/raw/7341714/ becomes:

basic example PNG

Basic example SVG

What it does

Under the hood, this is using Electron which uses Chromium's rendering engine. The export is generated with saveSvgAsPng.

Caveats

  • saveSvgAsPng doesn't support inlining of font face definitions (yet).
  • If the chart is styled with ancestor selectors that are outside of it, the selectors need to be re-mapped when they are inlined. This isn't supported within this library at the moment.

Dependents (0)

Package Sidebar

Install

npm i export-svg-chart

Weekly Downloads

1

Version

0.2.1

License

ISC

Last publish

Collaborators

  • thibaudcolas