231d1b9837376b44928a1b7bfde140171c8aac8581cd55d25be73a6a8ee0becde9bf414d52f600db0011fda6ad8ed8d39cc39845a20ad86eb028f5eec53e03 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Options
  2. The plugin options can be changed at 3 different levels and with the following priority:
  3. - per dataset: `dataset.datalabels.*`
  4. - per chart: `options.plugins.datalabels.*`
  5. - globally: `Chart.defaults.global.plugins.datalabels.*`
  6. Available options:
  7. | Name | Type | [Scriptable](#scriptable-options) | [Indexable](#indexable-options) | Default
  8. | ---- | ---- | :----: | :----: | ----
  9. | [`align`](positioning.md#alignment-and-offset) | `Number/String` | Yes | Yes | `'center'`
  10. | [`anchor`](positioning.md#anchoring) | `String` | Yes | Yes | `'center'`
  11. | `backgroundColor` | [`Style`](#style-options)/`null` | Yes | Yes | `null`
  12. | `borderColor` | [`Style`](#style-options)/`null` | Yes | Yes | `null`
  13. | `borderRadius` | `Number` | Yes | Yes | `0`
  14. | `borderWidth` | `Number` | Yes | Yes | `0`
  15. | `color` | [`Style`](#style-options) | Yes | Yes | `0`
  16. | [`display`](positioning.md#visibility) | `Boolean` | Yes | Yes | `true`
  17. | `font` | `Object` | Yes | Yes | -
  18. | `font.family` | `String` | - | - | [`defaultFontFamily`](http://www.chartjs.org/docs/latest/general/fonts.html)
  19. | `font.size` | `String` | - | - | [`defaultFontSize`](http://www.chartjs.org/docs/latest/general/fonts.html)
  20. | `font.style` | `String` | - | - | [`defaultFontStyle`](http://www.chartjs.org/docs/latest/general/fonts.html)
  21. | `font.weight` | `String` | - | - | `'normal'`
  22. | `font.lineHeight` | `Number/String` | - | - | `1.2`
  23. | [`listeners`](events.md) | `Object` | - | - | `{}`
  24. | [`offset`](positioning.md#alignment-and-offset) | `Number` | Yes | Yes | `4`
  25. | `opacity` | `Number` | Yes | Yes | `1`
  26. | `padding` | `Number/Object` | Yes | Yes | -
  27. | `padding.top` | `Number` | - | - | `4`
  28. | `padding.right` | `Number` | - | - | `4`
  29. | `padding.bottom` | `Number` | - | - | `4`
  30. | `padding.left` | `Number` | - | - | `4`
  31. | [`rotation`](positioning.md#rotation) | `Number` | Yes | Yes | `0`
  32. | [`textAlign`](formatting.md#text-alignment) | `String` | Yes | Yes | `start`
  33. | [`formatter`](formatting.md#data-transformation) | `Function/null` | - | - | -
  34. ## Scriptable Options
  35. Scriptable options also accept a function which is called for each data and that takes the unique argument `context` representing contextual information (see [option context](options.md#option-context)).
  36. Example:
  37. ```javascript
  38. color: function(context) {
  39. var index = context.dataIndex;
  40. var value = context.dataset.data[index];
  41. return value < 0 ? 'red' : // draw negative values in red
  42. index % 2 ? 'blue' : // else, alternate values in blue and green
  43. 'green';
  44. }
  45. ```
  46. ## Option Context
  47. The option context is used to give contextual information when resolving options. It mainly applies to [scriptable options](#scriptable-options) but also to some function options such as [`formatter`](formatting.md#data-transformation).
  48. The context object contains the following properties:
  49. - `active` (bool): whether the associated element is hovered ([see interactions](http://www.chartjs.org/docs/latest/general/interactions/))
  50. - `chart` (Chart): the associated chart
  51. - `dataIndex` (int): index of the associated data
  52. - `dataset` (object): the dataset at index `datasetIndex`
  53. - `datasetIndex` (int): index of the associated dataset
  54. ## Indexable Options
  55. Indexable options also accept an array in which each item corresponds to the element at the same index. Note that this method requires to provide as many items as data, so, in most cases, using a [function](#scriptable-options) is more appropriated.
  56. Example:
  57. ```javascript
  58. color: [
  59. 'red', // color for data at index 0
  60. 'blue', // color for data at index 1
  61. 'green', // color for data at index 2
  62. 'black', // color for data at index 3
  63. //...
  64. ]
  65. ```
  66. ## Style Options
  67. Style options are usually inputs for [`fillStyle`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle) or [`strokeStyle`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/strokeStyle).
  68. The following values are supported:
  69. - string parsed as [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) value
  70. - [CanvasGradient](https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient) object (linear or radial gradient)
  71. - [CanvasPattern](https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern) object (a repetitive image)
  72. Examples:
  73. ```javascript
  74. color: 'green' // named color
  75. color: '#dc143c' // HEX color
  76. color: 'rgb(51, 170, 51)' // RGB color (opaque)
  77. color: 'rgba(51, 170, 51, .5)' // RGBa color (semi-transparent)
  78. // ...
  79. ```