Cartesian Axes

Axes that follow a cartesian grid are known as ‘Cartesian Axes’. Cartesian axes are used for line, bar, and bubble charts. Four cartesian axes are included in Chart.js by default.

Visual Components

A cartesian axis is composed of visual components that can be individually configured. These components are:

Border

The axis border is drawn at the edge of the axis, beside the chart area. In the image below, it is drawn in red.

Cartesian Axes - 图1

config setup

  1. const config = {
  2. type: 'line',
  3. data,
  4. options: {
  5. scales: {
  6. x: {
  7. grid: {
  8. borderColor: 'red'
  9. }
  10. }
  11. }
  12. }
  13. };
  1. const labels = Utils.months({count: 7});
  2. const data = {
  3. labels: labels,
  4. datasets: [{
  5. label: 'My First dataset',
  6. backgroundColor: 'rgba(54, 162, 235, 0.5)',
  7. borderColor: 'rgb(54, 162, 235)',
  8. borderWidth: 1,
  9. data: [10, 20, 30, 40, 50, 0, 5],
  10. }]
  11. };

Grid lines

The grid lines for an axis are drawn on the chart area. In the image below, they are red.

Cartesian Axes - 图2

config setup

  1. const config = {
  2. type: 'line',
  3. data,
  4. options: {
  5. scales: {
  6. x: {
  7. grid: {
  8. color: 'red',
  9. borderColor: 'grey',
  10. tickColor: 'grey'
  11. }
  12. }
  13. }
  14. }
  15. };
  1. const labels = Utils.months({count: 7});
  2. const data = {
  3. labels: labels,
  4. datasets: [{
  5. label: 'My First dataset',
  6. backgroundColor: 'rgba(54, 162, 235, 0.5)',
  7. borderColor: 'rgb(54, 162, 235)',
  8. borderWidth: 1,
  9. data: [10, 20, 30, 40, 50, 0, 5],
  10. }]
  11. };

Ticks and Tick Marks

Ticks represent data values on the axis that appear as labels. The tick mark is the extension of the grid line from the axis border to the label. In this example, the tick mark is drawn in red while the tick label is drawn in blue.

Cartesian Axes - 图3

config setup

  1. const config = {
  2. type: 'line',
  3. data,
  4. options: {
  5. scales: {
  6. x: {
  7. grid: {
  8. tickColor: 'red'
  9. },
  10. ticks: {
  11. color: 'blue',
  12. }
  13. }
  14. }
  15. }
  16. };
  1. const labels = Utils.months({count: 7});
  2. const data = {
  3. labels: labels,
  4. datasets: [{
  5. label: 'My First dataset',
  6. backgroundColor: 'rgba(54, 162, 235, 0.5)',
  7. borderColor: 'rgb(54, 162, 235)',
  8. borderWidth: 1,
  9. data: [10, 20, 30, 40, 50, 0, 5],
  10. }]
  11. };

Title

The title component of the axis is used to label the data. In the example below, it is shown in red.

Cartesian Axes - 图4

config setup

  1. const config = {
  2. type: 'line',
  3. data,
  4. options: {
  5. scales: {
  6. x: {
  7. title: {
  8. color: 'red',
  9. display: true,
  10. text: 'Month'
  11. }
  12. }
  13. }
  14. }
  15. };
  1. const labels = Utils.months({count: 7});
  2. const data = {
  3. labels: labels,
  4. datasets: [{
  5. label: 'My First dataset',
  6. backgroundColor: 'rgba(54, 162, 235, 0.5)',
  7. borderColor: 'rgb(54, 162, 235)',
  8. borderWidth: 1,
  9. data: [10, 20, 30, 40, 50, 0, 5],
  10. }]
  11. };

Common Configuration

Common options to all cartesian axes

Namespace: options.scales[scaleId]

NameTypeDefaultDescription
boundsstring‘ticks’Determines the scale bounds. more…
positionstringPosition of the axis. more…
axisstringWhich type of axis this is. Possible values are: ‘x’, ‘y’. If not set, this is inferred from the first character of the ID which should be ‘x’ or ‘y’.
offsetbooleanfalseIf true, extra space is added to the both edges and the axis is scaled to fit into the chart area. This is set to true for a bar chart by default.
titleobjectScale title configuration. more…

Common options to all axes

Namespace: options.scales[scaleId]

NameTypeDefaultDescription
typestringType of scale being employed. Custom scales can be created and registered with a string key. This allows changing the type of an axis for a chart.
alignToPixelsbooleanfalseAlign pixel values to device pixels.
backgroundColorColorBackground color of the scale area.
displayboolean|stringtrueControls the axis global visibility (visible when true, hidden when false). When display: ‘auto’, the axis is visible only if at least one associated dataset is visible.
gridobjectGrid line configuration. more…
minnumberUser defined minimum number for the scale, overrides minimum value from data. more…
maxnumberUser defined maximum number for the scale, overrides maximum value from data. more…
reversebooleanfalseReverse the scale.
stackedboolean|stringfalseShould the data be stacked. more…
suggestedMaxnumberAdjustment used when calculating the maximum data value. more…
suggestedMinnumberAdjustment used when calculating the minimum data value. more…
ticksobjectTick configuration. more…
weightnumber0The weight used to sort the axis. Higher weights are further away from the chart area.

Axis Position

An axis can either be positioned at the edge of the chart, at the center of the chart area, or dynamically with respect to a data value.

To position the axis at the edge of the chart, set the position option to one of: 'top', 'left', 'bottom', 'right'. To position the axis at the center of the chart area, set the position option to 'center'. In this mode, either the axis option is specified or the axis ID starts with the letter ‘x’ or ‘y’. To position the axis with respect to a data value, set the position option to an object such as:

  1. {
  2. x: -20
  3. }

This will position the axis at a value of -20 on the axis with ID “x”. For cartesian axes, only 1 axis may be specified.

Scale Bounds

The bounds property controls the scale boundary strategy (bypassed by min/max options).

  • 'data': makes sure data are fully visible, labels outside are removed
  • 'ticks': makes sure ticks are fully visible, data outside are truncated

Tick Configuration

Common tick options to all cartesian axes

Namespace: options.scales[scaleId].ticks

NameTypeDefaultDescription
alignstring‘center’The tick alignment along the axis. Can be ‘start’, ‘center’, or ‘end’.
crossAlignstring‘near’The tick alignment perpendicular to the axis. Can be ‘near’, ‘center’, or ‘far’. See Tick Alignment
sampleSizenumberticks.lengthThe number of ticks to examine when deciding how many labels will fit. Setting a smaller value will be faster, but may be less accurate when there is large variability in label length.
autoSkipbooleantrueIf true, automatically calculates how many labels can be shown and hides labels accordingly. Labels will be rotated up to maxRotation before skipping any. Turn autoSkip off to show all labels no matter what.
autoSkipPaddingnumber3Padding between the ticks on the horizontal axis when autoSkip is enabled.
labelOffsetnumber0Distance in pixels to offset the label from the centre point of the tick (in the x-direction for the x-axis, and the y-direction for the y-axis). Note: this can cause labels at the edges to be cropped by the edge of the canvas
maxRotationnumber50Maximum rotation for tick labels when rotating to condense labels. Note: Rotation doesn’t occur until necessary. Note: Only applicable to horizontal scales.
minRotationnumber0Minimum rotation for tick labels. Note: Only applicable to horizontal scales.
mirrorbooleanfalseFlips tick labels around axis, displaying the labels inside the chart instead of outside. Note: Only applicable to vertical scales.
paddingnumber0Padding between the tick label and the axis. When set on a vertical axis, this applies in the horizontal (X) direction. When set on a horizontal axis, this applies in the vertical (Y) direction.

Common tick options to all axes

Namespace: options.scales[scaleId].ticks

NameTypeScriptableDefaultDescription
callbackfunctionReturns the string representation of the tick value as it should be displayed on the chart. See callback.
displaybooleantrueIf true, show tick labels.
colorColorYesChart.defaults.colorColor of ticks.
fontFontYesChart.defaults.fontSee Fonts
majorobject{}Major ticks configuration.
paddingnumber3Sets the offset of the tick labels from the axis
textStrokeColorColorYes``The color of the stroke around the text.
textStrokeWidthnumberYes0Stroke width around the text.
znumber0z-index of tick layer. Useful when ticks are drawn on chart area. Values <= 0 are drawn under datasets, > 0 on top.

Tick Alignment

The alignment of ticks is primarily controlled using two settings on the tick configuration object: align and crossAlign. The align setting configures how labels align with the tick mark along the axis direction (i.e. horizontal for a horizontal axis and vertical for a vertical axis). The crossAlign setting configures how labels align with the tick mark in the perpendicular direction (i.e. vertical for a horizontal axis and horizontal for a vertical axis). In the example below, the crossAlign setting is used to left align the labels on the Y axis.

Cartesian Axes - 图5

config setup

  1. const config = {
  2. type: 'bar',
  3. data,
  4. options: {
  5. indexAxis: 'y',
  6. scales: {
  7. y: {
  8. ticks: {
  9. crossAlign: 'far',
  10. }
  11. }
  12. }
  13. }
  14. };
  1. const labels = Utils.months({count: 7});
  2. const data = {
  3. labels: labels,
  4. datasets: [{
  5. label: 'My First dataset',
  6. backgroundColor: [
  7. 'rgba(255, 99, 132, 0.2)',
  8. 'rgba(255, 159, 64, 0.2)',
  9. 'rgba(255, 205, 86, 0.2)',
  10. 'rgba(75, 192, 192, 0.2)',
  11. 'rgba(54, 162, 235, 0.2)',
  12. 'rgba(153, 102, 255, 0.2)',
  13. 'rgba(201, 203, 207, 0.2)'
  14. ],
  15. borderColor: [
  16. 'rgb(255, 99, 132)',
  17. 'rgb(255, 159, 64)',
  18. 'rgb(255, 205, 86)',
  19. 'rgb(75, 192, 192)',
  20. 'rgb(54, 162, 235)',
  21. 'rgb(153, 102, 255)',
  22. 'rgb(201, 203, 207)'
  23. ],
  24. borderWidth: 1,
  25. data: [65, 59, 80, 81, 56, 55, 40],
  26. }]
  27. };

Note

The crossAlign setting is only effective when these preconditions are met:

  • tick rotation is 0
  • axis position is 'top', ‘left', 'bottom' or 'right'

Axis ID

The properties dataset.xAxisID or dataset.yAxisID have to match to scales property. This is especially needed if multi-axes charts are used.

  1. var myChart = new Chart(ctx, {
  2. type: 'line',
  3. data: {
  4. datasets: [{
  5. // This dataset appears on the first axis
  6. yAxisID: 'first-y-axis'
  7. }, {
  8. // This dataset appears on the second axis
  9. yAxisID: 'second-y-axis'
  10. }]
  11. },
  12. options: {
  13. scales: {
  14. 'first-y-axis': {
  15. type: 'linear'
  16. },
  17. 'second-y-axis': {
  18. type: 'linear'
  19. }]
  20. }
  21. }
  22. });

Creating Multiple Axes

With cartesian axes, it is possible to create multiple X and Y axes. To do so, you can add multiple configuration objects to the xAxes and yAxes properties. When adding new axes, it is important to ensure that you specify the type of the new axes as default types are not used in this case.

In the example below, we are creating two Y axes. We then use the yAxisID property to map the datasets to their correct axes.

  1. var myChart = new Chart(ctx, {
  2. type: 'line',
  3. data: {
  4. datasets: [{
  5. data: [20, 50, 100, 75, 25, 0],
  6. label: 'Left dataset',
  7. // This binds the dataset to the left y axis
  8. yAxisID: 'left-y-axis'
  9. }, {
  10. data: [0.1, 0.5, 1.0, 2.0, 1.5, 0],
  11. label: 'Right dataset',
  12. // This binds the dataset to the right y axis
  13. yAxisID: 'right-y-axis'
  14. }],
  15. labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
  16. },
  17. options: {
  18. scales: {
  19. 'left-y-axis': {
  20. type: 'linear',
  21. position: 'left'
  22. },
  23. 'right-y-axis': {
  24. type: 'linear',
  25. position: 'right'
  26. }]
  27. }
  28. }
  29. });