Colors

Charts support three color options:

  • for geometric elements, you can change background and border colors;
  • for textual elements, you can change the font color.

Also, you can change the whole canvas background.

Default colors

If a color is not specified, a global default color from Chart.defaults is used:

NameTypeDescriptionDefault value
backgroundColorColorBackground colorrgba(0, 0, 0, 0.1)
borderColorColorBorder colorrgba(0, 0, 0, 0.1)
colorColorFont color#666

You can reset default colors by updating these properties of Chart.defaults:

  1. Chart.defaults.backgroundColor = '#9BD0F5';
  2. Chart.defaults.borderColor = '#36A2EB';
  3. Chart.defaults.color = '#000';

Per-dataset color settings

If your chart has multiple datasets, using default colors would make individual datasets indistinguishable. In that case, you can set backgroundColor and borderColor for each dataset:

  1. const data = {
  2. labels: ['A', 'B', 'C'],
  3. datasets: [
  4. {
  5. label: 'Dataset 1',
  6. data: [1, 2, 3],
  7. borderColor: '#36A2EB',
  8. backgroundColor: '#9BD0F5',
  9. },
  10. {
  11. label: 'Dataset 2',
  12. data: [2, 3, 4],
  13. borderColor: '#FF6384',
  14. backgroundColor: '#FFB1C1',
  15. }
  16. ]
  17. };

However, setting colors for each dataset might require additional work that you’d rather not do. In that case, consider using the following plugins with pre-defined or generated palettes.

Default color palette

If you don’t have any preference for colors, you can use the built-in Colors plugin. It will cycle through a palette of seven Chart.js brand colors:

Colors plugin palette

All you need is to import and register the plugin:

  1. import { Colors } from 'chart.js';
  2. Chart.register(Colors);

Note

If you are using the UMD version of Chart.js, this plugin will be enabled by default. You can disable it by setting the enabled option to false:

  1. const options = {
  2. plugins: {
  3. colors: {
  4. enabled: false
  5. }
  6. }
  7. };

Dynamic datasets at runtime

By default the colors plugin only works when you initialize the chart without any colors for the border or background specified. If you want to force the colors plugin to always color your datasets, for example when using dynamic datasets at runtime you will need to set the forceOverride option to true:

  1. const options = {
  2. plugins: {
  3. colors: {
  4. forceOverride: true
  5. }
  6. }
  7. };

Advanced color palettes

See the awesome listColors - 图2 (opens new window) for plugins that would give you more flexibility defining color palettes.

Color formats

You can specify the color as a string in either of the following notations:

NotationExampleExample with transparency
HexadecimalColors - 图3 (opens new window)#36A2EB#36A2EB80
RGBColors - 图4 (opens new window) or RGBAColors - 图5 (opens new window)rgb(54, 162, 235)rgba(54, 162, 235, 0.5)
HSLColors - 图6 (opens new window) or HSLAColors - 图7 (opens new window)hsl(204, 82%, 57%)hsla(204, 82%, 57%, 0.5)

Alternatively, you can pass a CanvasPatternColors - 图8 (opens new window) or CanvasGradientColors - 图9 (opens new window) object instead of a string color to achieve some interesting effects.

Patterns and Gradients

For example, you can fill a dataset with a pattern from an image.

  1. const img = new Image();
  2. img.src = 'https://example.com/my_image.png';
  3. img.onload = () => {
  4. const ctx = document.getElementById('canvas').getContext('2d');
  5. const fillPattern = ctx.createPattern(img, 'repeat');
  6. const 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. };

Pattern fills can help viewers with vision deficiencies (e.g., color-blindness or partial sight) more easily understand your dataColors - 图10 (opens new window).

You can use the PatternomalyColors - 图11 (opens new window) library to generate patterns to fill datasets:

  1. const 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. };