编程语言
首页 > 编程语言> > javascript-无法使用jspdf和chartjs向画布添加背景色

javascript-无法使用jspdf和chartjs向画布添加背景色

作者:互联网

我正在使用jspdf插件将条形图导出为pdf.图表上的条形为白色,因此当我导出为pdf(作为png图像)时,这些条在pdf的透明背景上不可见.我在容器上添加了灰色背景色,以使白色条清晰可见.当我将图形导出为pdf时,它仍显示透明背景而不是灰色.有人可以告诉我我在想什么吗?

javascript

    $(document).ready(function(){

    $('#hyppdf').click(function(){
    var canvasImg = document.getElementById("myChart").toDataURL("image/png",     
    1.0);
    var doc = new jsPDF();
    doc.setFontSize(33);
    doc.setFillColor(135, 124,45,0);
    doc.addImage(canvasImg, 'png', 10, 10, 150, 100);
    doc.save('sample.pdf');
    })

    var ctx = document.getElementById("myChart").getContext('2d');

  var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["M", "T", "W", "T", "F", "S", "S"],
    datasets: [{
      label: 'apples',
      data: [12, 19, 3, 17, 28, 24, 7],
      backgroundColor: "rgba(255,255,255,1)"
    }, {
      label: 'oranges',
      data: [30, 29, 5, 5, 20, 3, 10],
      backgroundColor: "rgba(255,153,0,1)"
    }]
  }
});

})

的HTML:

    <div class="container" style="background-color:#ccc">
  <h2> <a id="hyppdf" href="#">download</a></h2>
  <div>
    <canvas id="myChart"></canvas>
  </div>
</div>

https://jsfiddle.net/2gnajnz4/5/

谢谢

解决方法:

我认为您可以通过在图像上添加rect并将其导出为pdf来解决您的问题:

  doc.setFillColor(204, 204,204,0);
  doc.rect(10, 10, 150, 160, "F");

查看更新的fiddle

标签:jspdf,chart-js,javascript,jquery
来源: https://codeday.me/bug/20191026/1936078.html