1

I am migrating from vega 2.x to 5.13.0 version. I am trying to generate chart SVG, so I can use as per my requirement. For that I am using toSVG(). While using that either success or catch method both are not firing.


      var view = new vega.View(vega.parse(chartJson), {renderer: 'none'});
      view.toSVG().then(function (svg) {
        console.log(svg)
      }).catch(function(err){
        console.log(err)
      });

1 Answers1

1

You could do the below:

var _v = new vega.View(vega.parse(spec))...

_v.toSVG()
  .then(svgString => {
      const filename = 'chart.svg';
      const url = 'data:image/svg+xml,' + encodeURIComponent(svgString);
      const link = document.createElement('a');
      link.setAttribute('href', url);
      link.setAttribute('target', '_blank');
      link.setAttribute('download', filename);
      link.dispatchEvent(new MouseEvent('click'));
    })

This will download the SVG. Adapted from a different stackoverflow answer (for mime type) and Vega test cases (for handling toSVG() promise).

learner
  • 150
  • 3
  • 16