HTML
CSS
JavaScript
HTML
<!DOCTYPE html>
<html lang="zh" >
<head>
<meta charset="UTF-8">
<title>Chart.js Canvas background</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
<link rel="stylesheet" type="text/css" href="http://lyear.itshubao.com/v4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="http://lyear.itshubao.com/v4/css/style.min.css">
</head>

<body>
<div class="chart-example">
  <ul class="nav nav-tabs">
    <li class="nav-item">
      <a class="nav-link active" data-toggle="tab" href="#color" aria-selected="true">color</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" data-toggle="tab" href="#image" aria-selected="false">image</a>
    </li>
  </ul>
  <div class="tab-content">
    <div class="tab-pane fade show active" id="color">
      <div class="chart-view"><canvas id="myChart-color"></canvas></div>
      <ul class="nav nav-tabs mt-3">
        <li class="nav-item">
          <a class="nav-link active" data-toggle="tab" href="#color-config" aria-selected="true">Config</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" data-toggle="tab" href="#color-setup" aria-selected="false">Setup</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" data-toggle="tab" href="#color-plugin" aria-selected="false">Plugin</a>
        </li>
      </ul>
      <div class="tab-content">
        <div class="tab-pane fade show active tab-pane-code" id="color-config">
          <pre>
const config = {
  type: 'doughnut',
  data: data,
  options: {
    plugins: {
      customCanvasBackgroundColor: {
        color: 'lightGreen',
      }
    }
  },
  plugins: [plugin],
};
          </pre>
        </div>
        <div class="tab-pane fade tab-pane-code" id="color-setup">
          <pre>
const data = {
  labels: [
    'Red',
    'Blue',
    'Yellow'
  ],
  datasets: [{
    label: 'My First Dataset',
    data: [300, 50, 100],
    backgroundColor: [
      'rgb(255, 99, 132)',
      'rgb(54, 162, 235)',
      'rgb(255, 205, 86)'
    ],
    hoverOffset: 4
  }]
};
          </pre>
        </div>
        <div class="tab-pane fade tab-pane-code" id="color-plugin">
          <pre>
// 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().
const plugin = {
  id: 'customCanvasBackgroundColor',
  beforeDraw: (chart, args, options) => {
    const {ctx} = chart;
    ctx.save();
    ctx.globalCompositeOperation = 'destination-over';
    ctx.fillStyle = options.color || '#99ffff';
    ctx.fillRect(0, 0, chart.width, chart.height);
    ctx.restore();
  }
};
          </pre>
        </div>
      </div>
    </div>
    <div class="tab-pane fade" id="image">
      <div class="chart-view"><canvas id="myChart-image"></canvas></div>
      
      <ul class="nav nav-tabs mt-3">
        <li class="nav-item">
          <a class="nav-link active" data-toggle="tab" href="#image-config" aria-selected="true">Config</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" data-toggle="tab" href="#image-setup" aria-selected="false">Setup</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" data-toggle="tab" href="#image-plugin" aria-selected="false">Plugin</a>
        </li>
      </ul>
      <div class="tab-content">
        <div class="tab-pane fade show active tab-pane-code" id="image-config">
          <pre>
const config2 = {
  type: 'doughnut',
  data: data,
  plugins: [plugin],
};
          </pre>
        </div>
        <div class="tab-pane fade tab-pane-code" id="image-setup">
          <pre>
const data = {
  labels: [
    'Red',
    'Blue',
    'Yellow'
  ],
  datasets: [{
    label: 'My First Dataset',
    data: [300, 50, 100],
    backgroundColor: [
      'rgb(255, 99, 132)',
      'rgb(54, 162, 235)',
      'rgb(255, 205, 86)'
    ],
    hoverOffset: 4
  }]
};
          </pre>
        </div>
        <div class="tab-pane fade tab-pane-code" id="image-plugin">
          <pre>
// 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().
const image = new Image();
image.src = 'https://www.chartjs.org/img/chartjs-logo.svg';

const plugin = {
  id: 'customCanvasBackgroundImage',
  beforeDraw: (chart) => {
    if (image.complete) {
      const ctx = chart.ctx;
      const {top, left, width, height} = chart.chartArea;
      const x = left + width / 2 - image.width / 2;
      const y = top + height / 2 - image.height / 2;
      ctx.drawImage(image, x, y);
    } else {
      image.onload = () => chart.draw();
    }
  }
};
          </pre>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/jquery.min.js"></script>
<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/popper.min.js"></script>
<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/Chart.js/4.1.1/chart.umd.min.js"></script>
</body>
</html>
CSS
body{
  background: #fff;
}
.tab-content .chart-view {
  max-width: 800px;
}
.tab-content .tab-pane-code {
  height: 360px;
  overflow: auto;
}
.tab-content .tab-pane-code pre {
  height: 100%;
  margin: 0px;
}
.chart-example {
  width: calc(100% - 2px);
  border: 1px solid #ececec;
  padding: 10px;
}
JavaScript
// 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().
const plugin = {
  id: 'customCanvasBackgroundColor',
  beforeDraw: (chart, args, options) => {
    const {ctx} = chart;
    ctx.save();
    ctx.globalCompositeOperation = 'destination-over';
    ctx.fillStyle = options.color || '#99ffff';
    ctx.fillRect(0, 0, chart.width, chart.height);
    ctx.restore();
  }
};

const data = {
  labels: [
    'Red',
    'Blue',
    'Yellow'
  ],
  datasets: [{
    label: 'My First Dataset',
    data: [300, 50, 100],
    backgroundColor: [
      'rgb(255, 99, 132)',
      'rgb(54, 162, 235)',
      'rgb(255, 205, 86)'
    ],
    hoverOffset: 4
  }]
};

const config = {
  type: 'doughnut',
  data: data,
  options: {
    plugins: {
      customCanvasBackgroundColor: {
        color: 'lightGreen',
      }
    }
  },
  plugins: [plugin],
};

const ctx = document.getElementById('myChart-color');

new Chart(ctx, config);


// 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().
const image = new Image();
image.src = 'https://www.chartjs.org/img/chartjs-logo.svg';

const plugin2 = {
  id: 'customCanvasBackgroundImage',
  beforeDraw: (chart) => {
    if (image.complete) {
      const ctx = chart.ctx;
      const {top, left, width, height} = chart.chartArea;
      const x = left + width / 2 - image.width / 2;
      const y = top + height / 2 - image.height / 2;
      ctx.drawImage(image, x, y);
    } else {
      image.onload = () => chart.draw();
    }
  }
};

const config2 = {
  type: 'doughnut',
  data: data,
  plugins: [plugin2],
};

const ctx2 = document.getElementById('myChart-image');

new Chart(ctx2, config2);