Canvas background

    In the two example plugins underneath here you can see how you can draw a color or image to the canvas as background. This way of giving the chart a background is only necessary if you want to export the chart with that specific background. For normal use you can set the background more easily with CSS (opens new window).

    config setup plugin

    1. labels: [
    2. 'Red',
    3. 'Blue',
    4. 'Yellow'
    5. ],
    6. datasets: [{
    7. label: 'My First Dataset',
    8. data: [300, 50, 100],
    9. backgroundColor: [
    10. 'rgb(255, 99, 132)',
    11. 'rgb(255, 205, 86)'
    12. hoverOffset: 4
    13. }]
    14. };

    config setup plugin

    1. const config = {
    2. type: 'doughnut',
    3. data: data,
    4. plugins: [plugin],
    5. };
    1. // Note: changes to the plugin code is not reflected to the chart, because the plugin is loaded at chart construction time and editor changes only trigger an chart.update().
    2. const image = new Image();
    3. image.src = 'https://www.chartjs.org/img/chartjs-logo.svg';
    4. id: 'custom_canvas_background_image',
    5. beforeDraw: (chart) => {
    6. if (image.complete) {
    7. const ctx = chart.ctx;
    8. const {top, left, width, height} = chart.chartArea;
    9. const x = left + width / 2 - image.width / 2;
    10. const y = top + height / 2 - image.height / 2;
    11. ctx.drawImage(image, x, y);
    12. } else {
    13. image.onload = () => chart.draw();
    14. }
    15. }