Axes

Axes are an integral part of a chart. They are used to determine how data maps to a pixel value on the chart. In a cartesian chart, there is 1 or more X-axis and 1 or more Y-axis to map points onto the 2-dimensional canvas. These axes are known as ‘cartesian axes’.

In a radial chart, such as a radar chart or a polar area chart, there is a single axis that maps points in the angular and radial directions. These are known as ‘radial axes’.

Scales in Chart.js >v2.0 are significantly more powerful, but also different than those of v1.0.

  • Multiple X & Y axes are supported.
  • A built-in label auto-skip feature detects would-be overlapping ticks and labels and removes every nth label to keep things displaying normally.
  • Scale titles are supported.
  • New scale types can be extended without writing an entirely new chart type.

Common Configuration

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.

Tick Configuration

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.

Axis Range Settings

Given the number of axis range settings, it is important to understand how they all interact with each other.

The suggestedMax and suggestedMin settings only change the data values that are used to scale the axis. These are useful for extending the range of the axis while maintaining the auto fit behaviour.

  1. let minDataValue = Math.min(mostNegativeValue, options.suggestedMin);
  2. let maxDataValue = Math.max(mostPositiveValue, options.suggestedMax);

In this example, the largest positive value is 50, but the data maximum is expanded out to 100. However, because the lowest data value is below the suggestedMin setting, it is ignored.

  1. let chart = new Chart(ctx, {
  2. type: 'line',
  3. data: {
  4. datasets: [{
  5. label: 'First dataset',
  6. data: [0, 20, 40, 50]
  7. }],
  8. labels: ['January', 'February', 'March', 'April']
  9. },
  10. options: {
  11. scales: {
  12. y: {
  13. suggestedMin: 50,
  14. suggestedMax: 100
  15. }
  16. }
  17. }
  18. });

In contrast to the suggested* settings, the min and max settings set explicit ends to the axes. When these are set, some data points may not be visible.

Stacking

By default data is not stacked. If the stacked option of the value scale (y-axis on horizontal chart) is true, positive and negative values are stacked separately. Additionally a stack option can be defined per dataset to further divide into stack groups more…. For some charts, you might want to stack positive and negative values together. That can be achieved by specifying stacked: 'single'.

Callbacks

There are a number of config callbacks that can be used to change parameters in the scale at different points in the update process. The options are supplied at the top level of the axis options.

Namespace: options.scales[scaleId]

NameArgumentsDescription
beforeUpdateaxisCallback called before the update process starts.
beforeSetDimensionsaxisCallback that runs before dimensions are set.
afterSetDimensionsaxisCallback that runs after dimensions are set.
beforeDataLimitsaxisCallback that runs before data limits are determined.
afterDataLimitsaxisCallback that runs after data limits are determined.
beforeBuildTicksaxisCallback that runs before ticks are created.
afterBuildTicksaxisCallback that runs after ticks are created. Useful for filtering ticks.
beforeTickToLabelConversionaxisCallback that runs before ticks are converted into strings.
afterTickToLabelConversionaxisCallback that runs after ticks are converted into strings.
beforeCalculateTickRotationaxisCallback that runs before tick rotation is determined.
afterCalculateTickRotationaxisCallback that runs after tick rotation is determined.
beforeFitaxisCallback that runs before the scale fits to the canvas.
afterFitaxisCallback that runs after the scale fits to the canvas.
afterUpdateaxisCallback that runs at the end of the update process.

Updating Axis Defaults

The default configuration for a scale can be easily changed. All you need to do is set the new options to Chart.defaults.scales[type].

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.

  1. Chart.defaults.scales.linear.min = 0;

Creating New Axes

To create a new axis, see the developer docs.