画布中的JS图表无法转换为图像或打印

时间:2018-06-22 04:05:15

标签: javascript html jscharts

代码如下:

<canvas id="gender" height="258" width="518" style="width: 415px; height: 207px;"></canvas>

这是生成JS饼图的脚本

<script>
    var ctx = document.getElementById("gender").getContext('2d');
    var myPieChart = new Chart(ctx,{
        type: 'pie',
        data: {
            labels: ['Male 59.79%','Female 40.21%'],
            datasets: [
                {
                data: [116,78],
                backgroundColor: ['#FFFF19','#19D119']
                }
            ]
        },
        options: {
            responsive: true
        }
    });
</script>

饼图在浏览器上呈现。这是生成图像并打印文档的功能:

function PrintElem(){
    var mywindow = window.open('', 'PRINT', 'height=400,width=600');
    mywindow.document.write('<html><head><meta charset="utf-8"><title>Analysis</title>');
    mywindow.document.write('</head><body >');
    var canvas = document.getElementById("gender");
    var context = canvas.getContext("2d");
    var imgData = canvas.toDataURL("image/png");
    mywindow.document.write('<img src="'+imgData+'"/>');
    mywindow.document.write('</body></html>');
    mywindow.document.close();  
    mywindow.focus();  
    mywindow.print();
    mywindow.close();
    return true;
}

我已经解码了base64并发现图像是正确的。但是,该图形未打印在输出中。我正在使用Google Chrome浏览器。怎么了?

0 个答案:

没有答案