ChartJS 如何在Chart JS中使用jsPDF将图表导出为PDF

lsmepo6l  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(273)

我尝试将条形图下载到PDF,但无法正常工作。
试图下载的条形图到pdf。图表因为给予我很多问题。java脚本的图形,我已经尝试,但没有效用,请有人能帮助我
第一个

v2g6jxz6

v2g6jxz61#

下面是一个工作示例的代码段。https://codepen.io/stockinail/pen/OJENbwM
需要一个额外的插件来为画布的背景着色,如CHARt.JS doc https://www.chartjs.org/docs/latest/configuration/canvas-background.html中所报告的:

  1. const plugin = {
  2. id: 'custom_canvas_background_color',
  3. beforeDraw: (chart) => {
  4. const {ctx} = chart;
  5. ctx.save();
  6. ctx.globalCompositeOperation = 'destination-over';
  7. ctx.fillStyle = 'white';
  8. ctx.fillRect(0, 0, chart.width, chart.height);
  9. ctx.restore();
  10. }
  11. };

必须将插件添加到图表配置中,如下所示:

  1. const myChart = new Chart(ctx, {
  2. type: 'bar',
  3. plugins: [plugin], // <<--- adds plugin to color the background of the canvas
  4. data: chartData
  5. });
展开查看全部

相关问题