React ChartsAxis Labels

Axis labels provide clear identification and context for the data represented on each axis in a chart.

Collision Avoidance Copy Link

AG Charts has a number of methods to avoid axis label collisions, which occur when two labels overlap or when a label exceeds the axis's maxThicknessRatio.

In this example:

  • Use the grab handle in the bottom right of the example to resize the chart. Observe how the labels behave with the different options applied.
  • The first group of buttons allows enabling and disabling collision avoidance altogether.
  • The second group of buttons allows changing the length of the labels. This more easily shows the wrapping and skipping behaviour.
  • The Rotation and Wrapping controls enable exploration of their behaviour with all the available options.
  • If enabled, labels try wrapping, then rotation, and finally skipping if needed.

Collision avoidance is enabled by default, to disable set label.avoidCollisions to false.

{
    label: {
        avoidCollisions: false,
    }
}

Wrapping Copy Link

Text wrapping allows labels to fit within constrained space by breaking across multiple lines.

Wrapping behaviour is set with the label.wrapping property:

  • 'on-space' - Wraps only on whitespace. If no suitable space is found, the next avoidance strategy is used.
  • 'always' - Forces wrapping to fit within the available space, including breaking words if needed.
  • 'hyphenate' - Similar to 'always', but inserts a hyphen (-) when wrapping in the middle of a word.
  • 'never' - Disables wrapping. Labels will remain on a single line, with the next avoidance strategy being used.
{
    label: {
        wrapping: 'hyphenate',
    }
}

This defaults to on-space for Category Axes, and to never on all other axes types.

Rotation Copy Link

Rotating axis labels allows fitting more labels into a smaller area, at the expense of readability.

Three rotation options are available:

  • No rotation.
  • Fixed rotation - labels are always rotated by the amount specified in the rotation property.
  • Automatic rotation - labels are rotated if any label will be wider than the gap between ticks.

Automatic rotation can be enabled or disabled using the autoRotate property. It is also possible to specify a rotation angle for the automatic rotation via the autoRotateAngle property.

Category axes have autoRotate enabled by default with a default autoRotateAngle of 335.

Skipping Copy Link

Label skipping is performed automatically when there is a high likelihood of collisions between labels. Labels that would collide are not displayed.

A collision is defined as labels coming within 10 pixels of each other. This minimum gap allowed between the axis labels before skipping can be configured using the label.minSpacing property.

{
    label: {
        minSpacing: 20,
    }
}

If autoRotate is enabled, rotation will be attempted before label skipping applies.

Label Text Formatting Copy Link

The label text is taken directly from the data.

To format how this is displayed, either use a Global Formatter, or specify an axis label level Formatter or Format as explained below.

This section discusses formatting the label text. See the Axis Intervals section to learn how to configure which labels are shown.

Formatter Copy Link

The label.formatter callback allows maximum flexibility for controlling what appears in the labels.

{
    label: {
        formatter: function(params) {
            return (params.value * 100) + '%';
        }
    }
}

In the above example:

  • The number axis uses a formatter to multiply by 100 and append '%' to all values.
  • The category axis uses a formatter to add '==' around the 'Windows' label only.

The formatter function receives a single params object which contains:

  • The raw value of the label (without any default formatting applied).
  • The index of the label in the data array.
  • The number of fractionDigits, if the value is a number.

It is called for each label and should return a string.

Format Copy Link

The label.format property takes a static string representing a time or number format. This has less flexibility than the formatter, but can be serialised. For all time axes, an object format mapping a unit of time to a format string can also be used.

{
    label: {
        format: "%b %Y",
    }
}
{
    label: {
        format: "$#{0>6.2f}",
    }
}

In the above example:

  • The time axis uses a format to display the short month name and full year for all values.
  • The number axis uses a format to prepend a '$' and use 2 decimal places for all values. Shorter numbers are padding with '0'.

The syntax used in the format strings depends on the axis type.

See the Formatters page for a full list of the available formats.

Next Up Copy Link

Continue to the next section to learn about Grid Lines.