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

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

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

v2g6jxz6

v2g6jxz61#

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

const plugin = {
  id: 'custom_canvas_background_color',
  beforeDraw: (chart) => {
    const {ctx} = chart;
    ctx.save();
    ctx.globalCompositeOperation = 'destination-over';
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, chart.width, chart.height);
    ctx.restore();
  }
};

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

const myChart = new Chart(ctx, {
  type: 'bar',
  plugins: [plugin], // <<--- adds plugin to color the background of the canvas
  data: chartData
});

相关问题