简体   繁体   English

有什么办法可以在svg旁边和svg一起下载html表?

[英]Is there any way to download the html table just beside an svg along with the svg?

I am using d3.js to create a pie diagram to show the coverage of a particular industry and just beside the svg I have an html table to represent the different colors for different industries. 我正在使用d3.js创建一个饼图来显示特定行业的覆盖范围,而在svg旁边,我有一个html表来代表不同行业的不同颜色。 I used canvas.toBlob() to download the svg as a png. 我使用canvas.toBlob()将svg下载为png。 Is there any way to download the html along with the svg. 有什么办法可以将svg和html一起下载。

 $('#downloadHeatMap') .click( function() { if (!isIE && (width > 32767 || height > 32767)) { alert("Unable to download, Image width/height limit exceeded"); } else if (isIE && (width > 8192 || height > 8192)) { alert("Unable to download, Image width/height limit exceeded"); } else if (isChrome && (width * height) > 268435456) { alert("Unable to download, Image width/height limit exceeded"); } else if (isFirefox && (width * height) > 472907776) { alert("Unable to download, Image width/height limit exceeded"); } else { var html = d3.select("svg").attr("version", 1.1).attr( "xmlns", "http://www.w3.org/2000/svg").node().parentNode.innerHTML; var svg = $('svg').parent().html(); var canvas = document.querySelector("canvas"); context = canvas.getContext("2d"); canvas.width = width + 100; canvas.height = height + 50; canvg(document.getElementById('drawingArea'), html); canvas.toBlob(function(blob) { saveAs(blob, "heatMapDiagram.png"); }); } }); 

Here is the table i have beside the svg 这是我在svg旁边的桌子

 <div class="pure-pusher-container"> <div class="container-fluid"> <div class="row"> <div class="col-md-8 text-center"> <h1 ><span class="pull-right">Industry Distribution Map</span></h1> </div> <div class="col-md-2 col-md-offset-2"> <input type="button" value="Download Heatmap" class="btn btn-primary" style="margin-top: 20px;" id="downloadHeatMap"> </div> </div> <div class="row" style="margin-top: 40px;"> <div class="col-md-10" id="heatMapDiv"><span class="pull-right" id="vis"></span></div> <div class="col-md-2" style="margin-top: 40px;"> <table class="table"> <thead> <tr> <th>Color</th> <th >% Coverage</th> </tr> </thead> <tbody> <tr> <td style="background-color:#FF4242;"></td> <td> 0 </td> </tr> <tr> <td style="background-color:#B84C4C;"></td> <td> 1 - 20</td> </tr> <tr> <td style="background-color:#de783b;"></td> <td> 21 - 40</td> </tr> <tr> <td style="background-color:#BC9BE0;"></td> <td> 41 - 60</td> </tr> <tr> <td style="background-color:#5687d1;"></td> <td> 61 - 80</td> </tr> <tr> <td style="background-color:#6ab975;"></td> <td> 81 - 100</td> </tr> </tbody> </table> </div> </div> </div> </div> 

Is there any way to save the html table along with my svg as a png ? 有什么办法可以将html表和我的svg保存为png吗?

First create a DOM element that contains both the table and the chart, side by side. 首先创建一个同时包含表和图表的DOM元素。 Then you can use the html2canvas JavaScript library to screenshot that entire DOM element into a canvas element, and then download it as a png to the user's computer: 然后,您可以使用html2canvas JavaScript库将整个DOM元素截图到canvas元素,然后将其作为png下载到用户的计算机:

html2canvas(document.getElementById("yourContainerID"), {
  onrendered: function(canvas) {
    canvas.toBlob(function(blob) {
        saveAs(blob, "heatMapDiagram.png");
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM