We just wrapped up AG Studio Launch Week See what we shipped

React ChartsAxis Labels

Version 14.3.0

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 dropdown allows enabling and disabling collision avoidance altogether.
  • The second dropdown allows changing the length of the labels. This more easily shows the wrapping and skipping behaviour.
  • The Rotation, Wrapping and Truncation controls enable exploration of their behaviour with all the available options.
  • If enabled, labels try wrapping and font size reduction, then truncation, 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. This defaults to 'on-space' for Category Axes, and to 'never' on all other axes types.

The available options are:

  • '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.
  • Special spaces:
    • A zero-width space (\u200B) adds a break point without a visible gap, allowing on-space wrapping to break a word at that point.
    • A non-breaking space (\u00A0) looks like a space but will not be a break point with on-space wrapping.
    • Other Unicode spaces and tabs act as ordinary spaces.
{
    label: {
        wrapping: 'hyphenate',
    },
}

In this example:

  • Use the Word Separator dropdown to change the word separator used in the labels. This allows testing of the different wrapping options.
  • The Wrapping dropdown allows choosing the wrapping option, and the sliders change the maximum label dimensions.
  • Drag the bottom-right corner to resize the chart and see the labels refit in each scenario.

Font Size Reduction Copy Link

Set label.minimumFontSize to let a label that does not fit shrink towards that size instead of being truncated.

{
    label: {
        maxWidth: 120,
        minimumFontSize: 8,
    },
}

Each label takes the largest font size between label.fontSize and label.minimumFontSize at which it fits whole, wrapping according to label.wrapping at each size, so labels on the same axis can end up at different sizes. A label is only truncated, or hidden with truncate: false, when it still does not fit at minimumFontSize.

Labels fit within maxWidth and maxHeight when set, and within the space available to the axis and clear of an axis scrollbar. With only minimumFontSize set, labels shrink to fit the space available to the axis.

Truncation Copy Link

Set truncate: true on a label to truncate the label’s text when there isn't enough space to display it fully. This ensures as much as possible of each label remains readable without reducing the number of ticks.

When truncation occurs, hovering over the truncated text will display a tooltip with the full label text.

A label that still exceeds maxWidth or maxHeight after wrapping and font size reduction is truncated with an ellipsis, even when truncate is not set. Set truncate: false to hide such a label instead.

Truncation takes precedence over auto-rotation. When truncation is enabled, auto-rotation will not apply.

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.

Alignment Copy Link

Axis labels are aligned within the axis thickness and bands, with a default alignment derived from the axis position and label rotation. Both directions can be set explicitly.

Text Alignment Copy Link

Use label.textAlign to control the horizontal alignment of labels. This is most useful on a vertical axis containing labels of differing width.

{
    axes: {
        x: {
            label: {
                textAlign: 'center',
            },
        },
        y: {
            label: {
                textAlign: 'left',
            },
        },
    },
}

In this example:

  • Use the buttons to see the textAlign options for each axis.
  • On the vertical number axis, labels are horizontally aligned within the axis thickness.
  • On the horizontal category axis, labels are horizontally aligned within each category band.

Vertical Alignment Copy Link

Use label.verticalAlign to control the vertical alignment of labels. This is most useful on a horizontal axis containing labels of differing height.

{
    axes: {
        x: {
            label: {
                verticalAlign: 'top',
            },
        },
        y: {
            label: {
                verticalAlign: 'middle',
            },
        },
    },
}

In this example:

  • Use the buttons to see the verticalAlign options for each axis.
  • On the horizontal category axis, labels are vertically aligned within the axis thickness.
  • On the vertical number axis, labels are vertically aligned around the tick they label.

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 padded 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.

Customisation Copy Link

See Fills & Borders for details on customising axis labels.