Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

02-Scales.md 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. ---
  2. title: Scales
  3. anchor: scales
  4. ---
  5. Scales in v2.0 of Chart.js are significantly more powerful, but also different than those of v1.0.
  6. * Multiple X & Y axes are supported.
  7. * A built-in label auto-skip feature detects would-be overlapping ticks and labels and removes every nth label to keep things displaying normally.
  8. * Scale titles are supported
  9. * New scale types can be extended without writing an entirely new chart type
  10. ### Common Configuration
  11. Every scale extends a core scale class with the following options:
  12. Name | Type | Default | Description
  13. --- | --- | --- | ---
  14. type | String | Chart specific. | Type of scale being employed. Custom scales can be created and registered with a string key. Options: ["category"](#scales-category-scale), ["linear"](#scales-linear-scale), ["logarithmic"](#scales-logarithmic-scale), ["time"](#scales-time-scale), ["radialLinear"](#scales-radial-linear-scale)
  15. display | Boolean | true | If true, show the scale including gridlines, ticks, and labels. Overrides *gridLines.display*, *scaleLabel.display*, and *ticks.display*.
  16. position | String | "left" | Position of the scale. Possible values are 'top', 'left', 'bottom' and 'right'.
  17. beforeUpdate | Function | undefined | Callback called before the update process starts. Passed a single argument, the scale instance.
  18. beforeSetDimensions | Function | undefined | Callback that runs before dimensions are set. Passed a single argument, the scale instance.
  19. afterSetDimensions | Function | undefined | Callback that runs after dimensions are set. Passed a single argument, the scale instance.
  20. beforeDataLimits | Function | undefined | Callback that runs before data limits are determined. Passed a single argument, the scale instance.
  21. afterDataLimits | Function | undefined | Callback that runs after data limits are determined. Passed a single argument, the scale instance.
  22. beforeBuildTicks | Function | undefined | Callback that runs before ticks are created. Passed a single argument, the scale instance.
  23. afterBuildTicks | Function | undefined | Callback that runs after ticks are created. Useful for filtering ticks. Passed a single argument, the scale instance.
  24. beforeTickToLabelConversion | Function | undefined | Callback that runs before ticks are converted into strings. Passed a single argument, the scale instance.
  25. afterTickToLabelConversion | Function | undefined | Callback that runs after ticks are converted into strings. Passed a single argument, the scale instance.
  26. beforeCalculateTickRotation | Function | undefined | Callback that runs before tick rotation is determined. Passed a single argument, the scale instance.
  27. afterCalculateTickRotation | Function | undefined | Callback that runs after tick rotation is determined. Passed a single argument, the scale instance.
  28. beforeFit | Function | undefined | Callback that runs before the scale fits to the canvas. Passed a single argument, the scale instance.
  29. afterFit | Function | undefined | Callback that runs after the scale fits to the canvas. Passed a single argument, the scale instance.
  30. afterUpdate | Function | undefined | Callback that runs at the end of the update process. Passed a single argument, the scale instance.
  31. **gridLines** | Object | - | See [grid line configuration](#scales-grid-line-configuration) section.
  32. **scaleLabel** | Object | | See [scale title configuration](#scales-scale-title-configuration) section.
  33. **ticks** | Object | | See [ticks configuration](#scales-ticks-configuration) section.
  34. #### Grid Line Configuration
  35. The grid line configuration is nested under the scale configuration in the `gridLines` key. It defines options for the grid lines that run perpendicular to the axis.
  36. Name | Type | Default | Description
  37. --- | --- | --- | ---
  38. display | Boolean | true |
  39. color | Color or Array[Color] | "rgba(0, 0, 0, 0.1)" | Color of the grid lines.
  40. lineWidth | Number or Array[Number] | 1 | Stroke width of grid lines
  41. drawBorder | Boolean | true | If true draw border on the edge of the chart
  42. drawOnChartArea | Boolean | true | If true, draw lines on the chart area inside the axis lines. This is useful when there are multiple axes and you need to control which grid lines are drawn
  43. drawTicks | Boolean | true | If true, draw lines beside the ticks in the axis area beside the chart.
  44. tickMarkLength | Number | 10 | Length in pixels that the grid lines will draw into the axis area.
  45. zeroLineWidth | Number | 1 | Stroke width of the grid line for the first index (index 0).
  46. zeroLineColor | Color | "rgba(0, 0, 0, 0.25)" | Stroke color of the grid line for the first index (index 0).
  47. offsetGridLines | Boolean | false | If true, offset labels from grid lines.
  48. #### Scale Title Configuration
  49. The grid line configuration is nested under the scale configuration in the `scaleLabel` key. It defines options for the scale title.
  50. Name | Type | Default | Description
  51. --- | --- | --- | ---
  52. display | Boolean | false |
  53. labelString | String | "" | The text for the title. (i.e. "# of People", "Response Choices")
  54. fontColor | Color | "#666" | Font color for the scale title.
  55. fontFamily| String | "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif" | Font family for the scale title, follows CSS font-family options.
  56. fontSize | Number | 12 | Font size for the scale title.
  57. fontStyle | String | "normal" | Font style for the scale title, follows CSS font-style options (i.e. normal, italic, oblique, initial, inherit).
  58. #### Tick Configuration
  59. The grid line configuration is nested under the scale configuration in the `ticks` key. It defines options for the tick marks that are generated by the axis.
  60. Name | Type | Default | Description
  61. --- | --- | --- | ---
  62. autoSkip | Boolean | true | If true, automatically calculates how many labels that can be shown and hides labels accordingly. Turn it off to show all labels no matter what
  63. callback | Function | `function(value) { return helpers.isArray(value) ? value : '' + value; }` | Returns the string representation of the tick value as it should be displayed on the chart. See [callback](#scales-creating-custom-tick-formats) section below.
  64. display | Boolean | true | If true, show the ticks.
  65. fontColor | Color | "#666" | Font color for the tick labels.
  66. fontFamily | String | "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif" | Font family for the tick labels, follows CSS font-family options.
  67. fontSize | Number | 12 | Font size for the tick labels.
  68. fontStyle | String | "normal" | Font style for the tick labels, follows CSS font-style options (i.e. normal, italic, oblique, initial, inherit).
  69. labelOffset | Number | 0 | Distance in pixels to offset the label from the centre point of the tick (in the y direction for the x axis, and the x direction for the y axis). *Note: this can cause labels at the edges to be cropped by the edge of the canvas*
  70. maxRotation | Number | 90 | Maximum rotation for tick labels when rotating to condense labels. Note: Rotation doesn't occur until necessary. *Note: Only applicable to horizontal scales.*
  71. minRotation | Number | 0 | Minimum rotation for tick labels. *Note: Only applicable to horizontal scales.*
  72. mirror | Boolean | false | Flips tick labels around axis, displaying the labels inside the chart instead of outside. *Note: Only applicable to vertical scales.*
  73. padding | Number | 10 | Padding between the tick label and the axis. *Note: Only applicable to horizontal scales.*
  74. reverse | Boolean | false | Reverses order of tick labels.
  75. #### Creating Custom Tick Formats
  76. The `callback` method may be used for advanced tick customization. In the following example, every label of the Y axis would be displayed in scientific notation.
  77. If the callback returns `null` or `undefined` the associated grid line will be hidden.
  78. ```javascript
  79. var chartInstance = new Chart(ctx, {
  80. type: 'line',
  81. data: data,
  82. options: {
  83. scales: {
  84. yAxes: [{
  85. ticks: {
  86. // Create scientific notation labels
  87. callback: function(value, index, values) {
  88. return value.toExponential();
  89. }
  90. }
  91. }]
  92. }
  93. }
  94. });
  95. ```
  96. ### Category Scale
  97. The category scale will be familiar to those who have used v1.0. Labels are drawn in from the labels array included in the chart data.
  98. #### Configuration Options
  99. The category scale has the following additional options that can be set.
  100. Name | Type | Default | Description
  101. --- | --- | --- | ---
  102. ticks.min | String | - | The minimum item to display. Must be a value in the `labels` array
  103. ticks.max | String | - | The maximum item to display. Must be a value in the `labels` array
  104. gridLines.offsetGridLines | Boolean | - | If true, labels are shifted to be between grid lines. This is used in the bar chart.
  105. ### Linear Scale
  106. The linear scale is use to chart numerical data. It can be placed on either the x or y axis. The scatter chart type automatically configures a line chart to use one of these scales for the x axis. As the name suggests, linear interpolation is used to determine where a value lies on the axis.
  107. #### Configuration Options
  108. The following options are provided by the linear scale. They are all located in the `ticks` sub options.
  109. Name | Type | Default | Description
  110. --- | --- | --- | ---
  111. beginAtZero | Boolean | - | if true, scale will inclulde 0 if it is not already included.
  112. min | Number | - | User defined minimum number for the scale, overrides minimum value from data.
  113. max | Number | - | User defined maximum number for the scale, overrides maximum value from data.
  114. maxTicksLimit | Number | 11 | Maximum number of ticks and gridlines to show. If not defined, it will limit to 11 ticks but will show all gridlines.
  115. stepSize | Number | - | User defined fixed step size for the scale. If set, the scale ticks will be enumerated by multiple of stepSize, having one tick per increment. If not set, the ticks are labeled automatically using the nice numbers algorithm.
  116. stepSize | Number | - | if defined, it can be used along with the min and the max to give a custom number of steps. See the example below.
  117. suggestedMax | Number | - | User defined maximum number for the scale, overrides maximum value *except for if* it is lower than the maximum value.
  118. suggestedMin | Number | - | User defined minimum number for the scale, overrides minimum value *except for if* it is higher than the minimum value.
  119. #### Example Configuration
  120. The following example creates a line chart with a vertical axis that goes from 0 to 5 in 0.5 sized steps.
  121. ```javascript
  122. var chartInstance = new Chart(ctx, {
  123. type: 'line',
  124. data: data,
  125. options: {
  126. scales: {
  127. yAxes: [{
  128. ticks: {
  129. max: 5,
  130. min: 0,
  131. stepSize: 0.5
  132. }
  133. }]
  134. }
  135. }
  136. });
  137. ```
  138. ### Logarithmic Scale
  139. The logarithmic scale is use to chart numerical data. It can be placed on either the x or y axis. As the name suggests, logarithmic interpolation is used to determine where a value lies on the axis.
  140. #### Configuration Options
  141. The following options are provided by the logarithmic scale. They are all located in the `ticks` sub options.
  142. Name | Type | Default | Description
  143. --- | --- | --- | ---
  144. min | Number | - | User defined minimum number for the scale, overrides minimum value from data.
  145. max | Number | - | User defined maximum number for the scale, overrides maximum value from data.
  146. #### Example Configuration
  147. The following example creates a chart with a logarithmic X axis that ranges from 1 to 1000.
  148. ```javascript
  149. var chartInstance = new Chart(ctx, {
  150. type: 'line',
  151. data: data,
  152. options: {
  153. scales: {
  154. xAxes: [{
  155. type: 'logarithmic',
  156. position: 'bottom',
  157. ticks: {
  158. min: 1,
  159. max: 1000
  160. }
  161. }]
  162. }
  163. }
  164. })
  165. ```
  166. ### Time Scale
  167. The time scale is used to display times and dates. It can only be placed on the X axis. When building its ticks, it will automatically calculate the most comfortable unit base on the size of the scale.
  168. #### Configuration Options
  169. The following options are provided by the logarithmic scale. They are all located in the `time` sub options.
  170. Name | Type | Default | Description
  171. --- | --- | --- | ---
  172. displayFormats | Object | - | See [Display Formats](#scales-display-formats) section below.
  173. isoWeekday | Boolean | false | If true and the unit is set to 'week', iso weekdays will be used.
  174. max | [Time](#scales-date-formats) | - | If defined, this will override the data maximum
  175. min | [Time](#scales-date-formats) | - | If defined, this will override the data minimum
  176. parser | String or Function | - | If defined as a string, it is interpreted as a custom format to be used by moment to parse the date. If this is a function, it must return a moment.js object given the appropriate data value.
  177. round | String | - | If defined, dates will be rounded to the start of this unit. See [Time Units](#scales-time-units) below for the allowed units.
  178. tooltipFormat | String | '' | The moment js format string to use for the tooltip.
  179. unit | String | - | If defined, will force the unit to be a certain type. See [Time Units](#scales-time-units) section below for details.
  180. unitStepSize | Number | 1 | The number of units between grid lines.
  181. #### Date Formats
  182. When providing data for the time scale, Chart.js supports all of the formats that Moment.js accepts. See [Moment.js docs](http://momentjs.com/docs/#/parsing/) for details.
  183. #### Display Formats
  184. The following display formats are used to configure how different time units are formed into strings for the axis tick marks. See [moment.js](http://momentjs.com/docs/#/displaying/format/) for the allowable format strings.
  185. Name | Default
  186. --- | ---
  187. millisecond | 'SSS [ms]'
  188. second | 'h:mm:ss a'
  189. minute | 'h:mm:ss a'
  190. hour | 'MMM D, hA'
  191. day | 'll'
  192. week | 'll'
  193. month | 'MMM YYYY'
  194. quarter | '[Q]Q - YYYY'
  195. year | 'YYYY'
  196. For example, to set the display format for the 'quarter' unit to show the month and year, the following config would be passed to the chart constructor.
  197. ```javascript
  198. var chartInstance = new Chart(ctx, {
  199. type: 'line',
  200. data: data,
  201. options: {
  202. scales: {
  203. xAxes: [{
  204. type: 'time',
  205. time: {
  206. displayFormats: {
  207. quarter: 'MMM YYYY'
  208. }
  209. }
  210. }]
  211. }
  212. }
  213. })
  214. ```
  215. #### Time Units
  216. The following time measurements are supported. The names can be passed as strings to the `time.unit` config option to force a certain unit.
  217. * millisecond
  218. * second
  219. * minute
  220. * hour
  221. * day
  222. * week
  223. * month
  224. * quarter
  225. * year
  226. For example, to create a chart with a time scale that always displayed units per month, the following config could be used.
  227. ```javascript
  228. var chartInstance = new Chart(ctx, {
  229. type: 'line',
  230. data: data,
  231. options: {
  232. scales: {
  233. xAxes: [{
  234. time: {
  235. unit: 'month'
  236. }
  237. }]
  238. }
  239. }
  240. })
  241. ```
  242. ### Radial Linear Scale
  243. The radial linear scale is used specifically for the radar and polar are chart types. It overlays the chart area, rather than being positioned on one of the edges.
  244. #### Configuration Options
  245. The following additional configuration options are provided by the radial linear scale.
  246. Name | Type | Default | Description
  247. --- | --- | --- | ---
  248. lineArc | Boolean | false | If true, circular arcs are used else straight lines are used. The former is used by the polar area chart and the latter by the radar chart
  249. angleLines | Object | - | See the Angle Line Options section below for details.
  250. pointLabels | Object | - | See the Point Label Options section below for details.
  251. ticks | Object | - | See the Ticks table below for options.
  252. #### Angle Line Options
  253. The following options are used to configure angled lines that radiate from the center of the chart to the point labels. They can be found in the `angleLines` sub options. Note that these options only apply if `lineArc` is false.
  254. Name | Type | Default | Description
  255. --- | --- | --- | ---
  256. display | Boolean | true | If true, angle lines are shown.
  257. color | Color | 'rgba(0, 0, 0, 0.1)' | Color of angled lines
  258. lineWidth | Number | 1 | Width of angled lines
  259. #### Point Label Options
  260. The following options are used to configure the point labels that are shown on the perimeter of the scale. They can be found in the `pointLabels` sub options. Note that these options only apply if `lineArc` is false.
  261. Name | Type | Default | Description
  262. --- | --- | --- | ---
  263. callback | Function | - | Callback function to transform data label to axis label
  264. fontColor | Color | '#666' | Font color
  265. fontFamily | String | "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif" | Font family to render
  266. fontSize | Number | 10 | Font size in pixels
  267. fontStyle | String | 'normal' | Font Style to use
  268. #### Tick Options
  269. Name | Type | Default | Description
  270. --- | --- | --- | ---
  271. backdropColor | Color | 'rgba(255, 255, 255, 0.75)' | Color of label backdrops
  272. backdropPaddingX | Number | 2 | Horizontal padding of label backdrop
  273. backdropPaddingY | Number | 2 | Vertical padding of label backdrop
  274. beginAtZero | Boolean | - | if true, scale will inclulde 0 if it is not already included.
  275. min | Number | - | User defined minimum number for the scale, overrides minimum value from data.
  276. max | Number | - | User defined maximum number for the scale, overrides maximum value from data.
  277. maxTicksLimit | Number | 11 | Maximum number of ticks and gridlines to show. If not defined, it will limit to 11 ticks but will show all gridlines.
  278. showLabelBackdrop | Boolean | true | If true, draw a background behind the tick labels
  279. stepSize | Number | - | User defined fixed step size for the scale. If set, the scale ticks will be enumerated by multiple of stepSize, having one tick per increment. If not set, the ticks are labeled automatically using the nice numbers algorithm.
  280. stepSize | Number | - | if defined, it can be used along with the min and the max to give a custom number of steps. See the example below.
  281. suggestedMax | Number | - | User defined maximum number for the scale, overrides maximum value *except for if* it is lower than the maximum value.
  282. suggestedMin | Number | - | User defined minimum number for the scale, overrides minimum value *except for if* it is higher than the minimum value.
  283. ### Update Default Scale config
  284. The default configuration for a scale can be easily changed using the scale service. Pass in a partial configuration that will be merged with the current scale default configuration.
  285. For example, to set the minimum value of 0 for all linear scales, you would do the following. Any linear scales created after this time would now have a minimum of 0.
  286. ```
  287. Chart.scaleService.updateScaleDefaults('linear', {
  288. ticks: {
  289. min: 0
  290. }
  291. })
  292. ```