|
1 | 1 | # Colors |
2 | 2 |
|
3 | | -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: |
| 3 | +Charts support three color options: |
| 4 | +* for geometric elements, you can change *background* and *border* colors; |
| 5 | +* for textual elements, you can change the *font* color. |
4 | 6 |
|
5 | | -| Name | Type | Default | Description |
6 | | -| ---- | ---- | ------- | ----------- |
7 | | -| `backgroundColor` | `Color` | `rgba(0, 0, 0, 0.1)` | Background color. |
8 | | -| `borderColor` | `Color` | `rgba(0, 0, 0, 0.1)` | Border color. |
9 | | -| `color` | `Color` | `#666` | Font color. |
| 7 | +Also, you can change the whole [canvas background](.../configuration/canvas-background.html). |
10 | 8 |
|
11 | | -You can also pass a [CanvasGradient](https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient) object. You will need to create this before passing to the chart, but using it you can achieve some interesting effects. |
| 9 | +## Default colors |
12 | 10 |
|
13 | | -## Patterns and Gradients |
| 11 | +If a color is not specified, a global default color from `Chart.defaults` is used: |
| 12 | + |
| 13 | +| Name | Type | Description | Default value |
| 14 | +| ---- | ---- | ----------- | ------------- |
| 15 | +| `backgroundColor` | [`Color`](../api/#color) | Background color | `rgba(0, 0, 0, 0.1)` |
| 16 | +| `borderColor` | [`Color`](../api/#color) | Border color | `rgba(0, 0, 0, 0.1)` |
| 17 | +| `color` | [`Color`](../api/#color) | Font color | `#666` |
| 18 | + |
| 19 | +You can reset default colors by updating these properties of `Chart.defaults`: |
| 20 | + |
| 21 | +```javascript |
| 22 | +Chart.defaults.backgroundColor = '#9BD0F5'; |
| 23 | +Chart.defaults.borderColor = '#36A2EB'; |
| 24 | +Chart.defaults.color = '#000'; |
| 25 | +``` |
| 26 | + |
| 27 | +### Per-dataset color settings |
| 28 | + |
| 29 | +If your chart has multiple datasets, using default colors would make individual datasets indistiguishable. In that case, you can set `backgroundColor` and `borderColor` for each dataset: |
| 30 | + |
| 31 | +```javascript |
| 32 | +const data = { |
| 33 | + labels: ['A', 'B', 'C'], |
| 34 | + datasets: [ |
| 35 | + { |
| 36 | + label: 'Dataset 1', |
| 37 | + data: [1, 2, 3], |
| 38 | + borderColor: '#36A2EB', |
| 39 | + backgroundColor: '#9BD0F5', |
| 40 | + }, |
| 41 | + { |
| 42 | + label: 'Dataset 2', |
| 43 | + data: [2, 3, 4], |
| 44 | + borderColor: '#FF6384', |
| 45 | + backgroundColor: '#FFB1C1', |
| 46 | + } |
| 47 | + ] |
| 48 | +}; |
| 49 | +``` |
| 50 | + |
| 51 | +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. |
| 52 | + |
| 53 | +### Default color palette |
| 54 | + |
| 55 | +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: |
| 56 | + |
| 57 | +<div style="max-width: 500px;"> |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +</div> |
| 62 | + |
| 63 | +All you need is to import and register the plugin: |
14 | 64 |
|
15 | | -An alternative option is to pass a [CanvasPattern](https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern) or [CanvasGradient](https://developer.mozilla.org/en/docs/Web/API/CanvasGradient) object instead of a string colour. |
| 65 | +```javascript |
| 66 | +import { Colors } from 'chart.js'; |
| 67 | + |
| 68 | +Chart.register(Colors); |
| 69 | +``` |
| 70 | + |
| 71 | +:::tip Note |
| 72 | + |
| 73 | +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`: |
| 74 | + |
| 75 | +```js |
| 76 | +const options = { |
| 77 | + plugins: { |
| 78 | + colors: { |
| 79 | + enabled: false |
| 80 | + } |
| 81 | + } |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +::: |
| 86 | + |
| 87 | +### Advanced color palettes |
16 | 88 |
|
17 | | -For example, if you wanted to fill a dataset with a pattern from an image you could do the following. |
| 89 | +See the [awesome list](https://github.com/chartjs/awesome#plugins) for plugins that would give you more flexibility defining color palettes. |
| 90 | + |
| 91 | +## Color formats |
| 92 | + |
| 93 | +You can specify the color as a string in either of the following notations: |
| 94 | + |
| 95 | +| Notation | Example | Example with transparency |
| 96 | +| -------- | ------- | ------------------------- |
| 97 | +| [Hexademical](https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color) | `#36A2EB` | `#36A2EB80` |
| 98 | +| [RGB](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb) or [RGBA](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgba) | `rgb(54, 162, 235)` | `rgba(54, 162, 235, 0.5)` |
| 99 | +| [HSL](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsl) or [HSLA](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/hsla) | `hsl(204, 82%, 57%)` | `hsla(204, 82%, 57%, 0.5)` |
| 100 | + |
| 101 | +Alternatively, you can pass a [CanvasPattern](https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern) or [CanvasGradient](https://developer.mozilla.org/en/docs/Web/API/CanvasGradient) object instead of a string color to achieve some interesting effects. |
| 102 | + |
| 103 | +## Patterns and Gradients |
| 104 | + |
| 105 | +For example, you can fill a dataset with a pattern from an image. |
18 | 106 |
|
19 | 107 | ```javascript |
20 | 108 | const img = new Image(); |
21 | 109 | img.src = 'https://example.com/my_image.png'; |
22 | | -img.onload = function() { |
23 | | - const ctx = document.getElementById('canvas').getContext('2d'); |
24 | | - const fillPattern = ctx.createPattern(img, 'repeat'); |
25 | | - |
26 | | - const chart = new Chart(ctx, { |
27 | | - data: { |
28 | | - labels: ['Item 1', 'Item 2', 'Item 3'], |
29 | | - datasets: [{ |
30 | | - data: [10, 20, 30], |
31 | | - backgroundColor: fillPattern |
32 | | - }] |
33 | | - } |
34 | | - }); |
| 110 | +img.onload = () => { |
| 111 | + const ctx = document.getElementById('canvas').getContext('2d'); |
| 112 | + const fillPattern = ctx.createPattern(img, 'repeat'); |
| 113 | + |
| 114 | + const chart = new Chart(ctx, { |
| 115 | + data: { |
| 116 | + labels: ['Item 1', 'Item 2', 'Item 3'], |
| 117 | + datasets: [{ |
| 118 | + data: [10, 20, 30], |
| 119 | + backgroundColor: fillPattern |
| 120 | + }] |
| 121 | + } |
| 122 | + }); |
35 | 123 | }; |
36 | 124 | ``` |
| 125 | +Pattern fills can help viewers with vision deficiencies (e.g., color-blindness or partial sight) [more easily understand your data](http://betweentwobrackets.com/data-graphics-and-colour-vision/). |
37 | 126 |
|
38 | | -Using pattern fills for data graphics can help viewers with vision deficiencies (e.g. color-blindness or partial sight) to [more easily understand your data](http://betweentwobrackets.com/data-graphics-and-colour-vision/). |
39 | | - |
40 | | -Using the [Patternomaly](https://github.com/ashiguruma/patternomaly) library you can generate patterns to fill datasets. |
| 127 | +You can use the [Patternomaly](https://github.com/ashiguruma/patternomaly) library to generate patterns to fill datasets: |
41 | 128 |
|
42 | 129 | ```javascript |
43 | 130 | const chartData = { |
44 | | - datasets: [{ |
45 | | - data: [45, 25, 20, 10], |
46 | | - backgroundColor: [ |
47 | | - pattern.draw('square', '#ff6384'), |
48 | | - pattern.draw('circle', '#36a2eb'), |
49 | | - pattern.draw('diamond', '#cc65fe'), |
50 | | - pattern.draw('triangle', '#ffce56') |
51 | | - ] |
52 | | - }], |
53 | | - labels: ['Red', 'Blue', 'Purple', 'Yellow'] |
| 131 | + datasets: [{ |
| 132 | + data: [45, 25, 20, 10], |
| 133 | + backgroundColor: [ |
| 134 | + pattern.draw('square', '#ff6384'), |
| 135 | + pattern.draw('circle', '#36a2eb'), |
| 136 | + pattern.draw('diamond', '#cc65fe'), |
| 137 | + pattern.draw('triangle', '#ffce56') |
| 138 | + ] |
| 139 | + }], |
| 140 | + labels: ['Red', 'Blue', 'Purple', 'Yellow'] |
54 | 141 | }; |
55 | 142 | ``` |
0 commit comments