77c8dbdb031daf1fe22bf37443d8fd1fade34409c9208fab8ee7eb51455fae33f455a53baf9f9626cab66064793d968364ed82fd24d0ed37d21cdbed91c92f 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Formatting
  2. ## Data Transformation
  3. Data values are converted to string (`'' + value`). If `value` is an object, the following rules apply first:
  4. - `value = value.label` if defined and not null
  5. - else `value = value.r` if defined and not null
  6. - else `value = 'key[0]: value[key[0]], key[1]: value[key[1]], ...'`
  7. This default behavior can be overridden thanks to the `formatter` option. It accepts a function called for every data and that takes two arguments:
  8. - `value`: the current data value
  9. - `context`: contextual information (see [option context](options.md#option-context))
  10. Example:
  11. ```javascript
  12. formatter: function(value, context) {
  13. return context.dataIndex + ': ' + Math.round(value*100) + '%';
  14. }
  15. // label for data at index 0 with value 0.23: "0: 23%"
  16. // label for data at index 1 with value 0.42: "1: 42%"
  17. // ...
  18. ```
  19. > **Tip:** the first argument being the value, you can directly use generic methods:
  20. ```javascript
  21. formatter: Math.round
  22. formatter: Math.floor
  23. formatter: Math.ceil
  24. // ...
  25. ```
  26. ## Custom Labels
  27. It's also possible to display text other than the data values, for example, the associated labels:
  28. ```javascript
  29. new Chart('id', {
  30. type: 'bar',
  31. data: {
  32. labels: ['foo', 'bar'],
  33. datasets: [{
  34. data: [42, 24]
  35. }]
  36. },
  37. options: {
  38. plugins: {
  39. datalabels: {
  40. formatter: function(value, context) {
  41. return context.chart.data.labels[context.dataIndex];
  42. }
  43. }
  44. }
  45. }
  46. });
  47. // label for data at index 0: "foo"
  48. // label for data at index 1: "bar"
  49. // ...
  50. ```
  51. > **Tip:** `chart.data.labels` is given as an example but it works with any source:
  52. ```javascript
  53. context.dataset.data[context.dataIndex].label; // labels in each data object
  54. context.dataset.labels[context.dataIndex]; // labels store in the dataset
  55. globalLabels[context.dataIndex]; // labels store outside the chart
  56. // ...
  57. ```
  58. ## Multiline Labels
  59. Labels can be displayed on multiple lines by using the newline character (`\n`) between each line or by providing an array of strings where each item represents a new line.
  60. Example:
  61. ```javascript
  62. formatter: function(value) {
  63. return 'line1\nline2\n' + value;
  64. // eq. return ['line1', 'line2', value]
  65. }
  66. ```
  67. > **Tip:** the space between each line can be adjusted using the `font.lineHeight` option.
  68. ## Text Alignment
  69. The `textAlign` option only applies to [multiline labels](#multiline-labels) and specifies the text alignment being used when drawing the label text (see [`CanvasRenderingContext2D.textAlign`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign)). Note that right-to-left text detection based on the current locale is not currently implemented.
  70. Supported values for `textAlign`:
  71. - `start` (default): the text is left-aligned
  72. - `center`: the text is centered
  73. - `end`: the text is right-aligned
  74. - `left`: alias of `start`
  75. - `right`: alias of `right`