simg icon indicating copy to clipboard operation
simg copied to clipboard

simg.js:37 Uncaught TypeError: svg.setAttribute is not a function

Open FlorianLeitgeb opened this issue 6 years ago • 1 comments

I have a d3 graph within a svg element and wanted to export it. Just grabbed the svg element and called simg like shown in your example:

function exportToPNG() {
  var simg = new Simg(document.getElementsByTagName("svg"));
  // Replace the current SVG with an image version of it.
  simg.replace();

  // And trigger a download of the rendered image.
  simg.download();
}

simg.js:37 Uncaught TypeError: svg.setAttribute is not a function
    at simg.js:37
    at Array.forEach (<anonymous>)
    at root.Simg.toString (simg.js:36)
    at root.Simg.toSvgImage (simg.js:57)
    at root.Simg.toCanvas (simg.js:44)
    at root.Simg.toImg (simg.js:74)
    at root.Simg.replace (simg.js:90)
    at exportToPNG (itsv.pws.ui.projStruct.js?ver:50)
    at HTMLAnchorElement.onclick (ProjStruct.aspx:1)

FlorianLeitgeb avatar Jun 05 '18 13:06 FlorianLeitgeb

document.getElementsByTagName("svg") returns an array of matching elements. Use

document.getElementsByTagName("svg")[0]

or

d3.select("svg").node()

to get actual DOM Element instead.

ml-eds avatar Sep 27 '18 07:09 ml-eds