How to convert/save d3.js graph to pdf/jpeg

前端 未结 4 1273
执笔经年
执笔经年 2020-11-27 15:13

I\'m working on a client-side/javascript function to save or convert an existing D3-SVG graph into a file. I\'ve searched a lot and found some recommendations, mainly using

4条回答
  •  迷失自我
    2020-11-27 15:46

    The Simg library created and suggest by Mauvis Ledford above worked great for allowing my svg charts created with Dimple to be downloaded.

    I did however need to change one aspect of the code to make it work. Inside of the toString() prototype, inside the forEach loop (line 37), if you change "svg.setAttribute(..)" to "svg[0].setAttribute" it will alleviate the "setAttribute(..) is not a function" error. Similarly the same needs to be done right below in the return statement, appending "[0]" after svg (line 39).

    I also had to manually edit the "canvas.width" and "canvas.height" (lines 48 & 49) assignments in the toCanvas() prototype, in order to make the downloaded image a more correct size (it was previously just downloading a static 300x150 square in the top left corner of the chart).

提交回复
热议问题