Colors

When supplying colors to Chart options, you can use a number of formats. You can specify the color as a string in hexadecimal, RGB, or HSL notations. If a color is needed, but not specified, Chart.js will use the global default color. There are 3 color options, stored at Chart.defaults, to set:

NameTypeDefaultDescription
backgroundColorColorrgba(0, 0, 0, 0.1)Background color.
borderColorColorrgba(0, 0, 0, 0.1)Border color.
colorColor#666Font color.

You can also pass a CanvasGradientColors - 图1 (opens new window) object. You will need to create this before passing to the chart, but using it you can achieve some interesting effects.

Patterns and Gradients

An alternative option is to pass a CanvasPatternColors - 图2 (opens new window) or CanvasGradientColors - 图3 (opens new window) object instead of a string colour.

For example, if you wanted to fill a dataset with a pattern from an image you could do the following.

  1. var img = new Image();
  2. img.src = 'https://example.com/my_image.png';
  3. img.onload = function() {
  4. var ctx = document.getElementById('canvas').getContext('2d');
  5. var fillPattern = ctx.createPattern(img, 'repeat');
  6. var chart = new Chart(ctx, {
  7. data: {
  8. labels: ['Item 1', 'Item 2', 'Item 3'],
  9. datasets: [{
  10. data: [10, 20, 30],
  11. backgroundColor: fillPattern
  12. }]
  13. }
  14. });
  15. };

Using pattern fills for data graphics can help viewers with vision deficiencies (e.g. color-blindness or partial sight) to more easily understand your dataColors - 图4 (opens new window).

Using the PatternomalyColors - 图5 (opens new window) library you can generate patterns to fill datasets.

  1. var chartData = {
  2. datasets: [{
  3. data: [45, 25, 20, 10],
  4. backgroundColor: [
  5. pattern.draw('square', '#ff6384'),
  6. pattern.draw('circle', '#36a2eb'),
  7. pattern.draw('diamond', '#cc65fe'),
  8. pattern.draw('triangle', '#ffce56')
  9. ]
  10. }],
  11. labels: ['Red', 'Blue', 'Purple', 'Yellow']
  12. };