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.
import { Component } from "@angular/core";
import { AgCharts } from "ag-charts-angular";
import {
AgBarSeriesOptions,
AgCartesianChartOptions,
AgCategoryAxisOptions,
AgNumberAxisOptions,
BarSeriesModule,
CategoryAxisModule,
LegendModule,
ModuleRegistry,
NumberAxisModule,
TextWrap,
} from "ag-charts-community";
import { getData } from "./data";
import clone from "clone";
ModuleRegistry.registerModules([
BarSeriesModule,
CategoryAxisModule,
LegendModule,
NumberAxisModule,
]);
@Component({
selector: "my-app",
standalone: true,
imports: [AgCharts],
template: `<div class="example-controls">
<div class="controls-row">
<div class="gap-right">
<label for="avoidance-select">Avoid Collisions:</label>
<select id="avoidance-select" (change)="avoidanceChange($event)">
<option value="enabled">Enabled (default)</option>
<option value="disabled">Disabled</option>
</select>
</div>
<div>
<label for="label-select">Labels: </label>
<select id="label-select" (change)="labelChange($event)">
<option value="year">Short</option>
<option value="sportStar">Long</option>
</select>
</div>
<button class="push-right" (click)="reset()">Reset</button>
</div>
<div class="controls-row">
<div class="gap-right">
<label for="truncation-select">Truncation: </label>
<select id="truncation-select" (change)="truncationChange($event)">
<option value="disabled">Disabled (default)</option>
<option value="enabled">Enabled</option>
</select>
</div>
<div class="gap-right">
<label for="rotation-select">Rotation: </label>
<select id="rotation-select" (change)="rotationChange($event)">
<option value="auto">Auto rotation (default)</option>
<option value="disabled">No rotation</option>
<option value="fixed">Fixed rotation</option>
</select>
</div>
<div>
<label for="wrap-select">Wrapping: </label>
<select id="wrap-select" (change)="wrapChange($event)">
<option value="on-space">on-space (default)</option>
<option value="always">always</option>
<option value="hyphenate">hyphenate</option>
<option value="never">never</option>
</select>
</div>
</div>
</div>
<div class="resizable-container">
<ag-charts
[options]="options"
class="resizable"
></ag-charts>
</div>
`,
})
export class AppComponent {
public options;
constructor() {
this.options = {
data: getData(),
series: [
{
type: "bar",
xKey: "year",
yKey: "value",
},
],
axes: {
x: {
type: "category",
label: {},
},
y: {
type: "number",
label: {},
},
},
};
}
reset = () => {
const options = clone(this.options);
const categoryAxis = options.axes!.x! as AgCategoryAxisOptions;
const numberAxis = options.axes!.y! as AgNumberAxisOptions;
delete categoryAxis.label!.rotation;
delete categoryAxis.label!.autoRotate;
delete categoryAxis.label!.avoidCollisions;
delete categoryAxis.label!.truncate;
delete numberAxis.label!.rotation;
delete numberAxis.label!.autoRotate;
delete numberAxis.label!.avoidCollisions;
delete numberAxis.label!.truncate;
(options.series![0] as AgBarSeriesOptions).xKey = "year";
this.options = options;
};
rotationChange = (e: Event) => {
const options = clone(this.options);
const categoryAxis = options.axes!.x! as AgCategoryAxisOptions;
const numberAxis = options.axes!.y! as AgNumberAxisOptions;
delete categoryAxis.label!.rotation;
delete numberAxis.label!.rotation;
const value = (e.target as HTMLInputElement).value;
switch (value) {
case "auto":
categoryAxis.label!.autoRotate = true;
numberAxis.label!.autoRotate = true;
break;
case "fixed":
categoryAxis.label!.rotation = 45;
numberAxis.label!.rotation = 45;
categoryAxis.label!.autoRotate = false;
numberAxis.label!.autoRotate = false;
break;
case "disabled":
categoryAxis.label!.autoRotate = false;
numberAxis.label!.autoRotate = false;
break;
}
this.options = options;
};
labelChange = (e: Event) => {
const options = clone(this.options);
const value = (e.target as HTMLInputElement).value;
(options.series![0] as AgBarSeriesOptions).xKey = value;
this.options = options;
};
truncationChange = (e: Event) => {
const options = clone(this.options);
const categoryAxis = options.axes!.x! as AgCategoryAxisOptions;
const numberAxis = options.axes!.y! as AgNumberAxisOptions;
delete categoryAxis.label!.rotation;
delete numberAxis.label!.rotation;
const value = (e.target as HTMLInputElement).value;
const enabled = value === "enabled";
categoryAxis.label!.truncate = enabled;
numberAxis.label!.truncate = enabled;
this.options = options;
};
avoidanceChange = (e: Event) => {
const options = clone(this.options);
const categoryAxis = options.axes!.x! as AgCategoryAxisOptions;
const numberAxis = options.axes!.y! as AgNumberAxisOptions;
delete categoryAxis.label!.rotation;
delete numberAxis.label!.rotation;
const value = (e.target as HTMLInputElement).value;
const enabled = value === "enabled";
categoryAxis.label!.avoidCollisions = enabled;
numberAxis.label!.avoidCollisions = enabled;
this.options = options;
};
wrapChange = (e: Event) => {
const options = clone(this.options);
const categoryAxis = options.axes!.x! as AgCategoryAxisOptions;
const numberAxis = options.axes!.y! as AgNumberAxisOptions;
const value = (e.target as HTMLInputElement).value as TextWrap;
categoryAxis.label!.wrapping = value;
numberAxis.label!.wrapping = value;
this.options = options;
};
}
.resizable-container {
height: 100%;
padding: 4px;
height: 100%;
border-radius: 8px;
background-color: color-mix(in srgb, var(--chart-bg), var(--chart-border) 10%);
border: 1px solid var(--chart-border);
overflow: hidden;
}
.resizable {
width: 100%;
max-width: 100%;
max-height: 100%;
overflow: hidden;
resize: both;
}
function sfc32(a: number, b: number, c: number, d: number) {
return function () {
a >>>= 0;
b >>>= 0;
c >>>= 0;
d >>>= 0;
let t = (a + b) | 0;
a = b ^ (b >>> 9);
b = (c + (c << 3)) | 0;
c = (c << 21) | (c >>> 11);
d = (d + 1) | 0;
t = (t + d) | 0;
c = (c + t) | 0;
return (t >>> 0) / 4294967296;
};
}
function seedRandom(seed = 1337): () => number {
const realSeed = seed ^ 0xdeadbeef;
return sfc32(0x9e3779b9, 0x243f6a88, 0xb7e15162, realSeed);
}
const years: number[] = [];
for (let year = 2012; year <= 2022; year++) {
years.push(year);
}
const sportStars = [
"Usain Bolt",
"Serena Williams",
"Michael Phelps",
"Simone Biles",
"Lionel Messi",
"Roger Federer",
"Katie Ledecky",
"Cristiano Ronaldo",
"Naomi Osaka",
"LeBron James",
"Max Verstappen",
];
export function getData(): any[] {
const random = seedRandom(67890);
return years.map((year, idx) => ({
year,
sportStar: sportStars[idx],
value: Math.round(random() * 1000),
}));
}
// Angular entry point file
import '@angular/compiler';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
bootstrapApplication(AppComponent);
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 buttons 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, 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:
'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.
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.
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
rotationproperty. - 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.
import { Component } from "@angular/core";
import { AgCharts } from "ag-charts-angular";
import {
AgChartOptions,
BarSeriesModule,
CategoryAxisModule,
LegendModule,
ModuleRegistry,
NumberAxisModule,
} from "ag-charts-community";
import { DataType, getData } from "./data";
ModuleRegistry.registerModules([
BarSeriesModule,
CategoryAxisModule,
LegendModule,
NumberAxisModule,
]);
@Component({
selector: "my-app",
standalone: true,
imports: [AgCharts],
template: `<ag-charts
[options]="options"
></ag-charts>
`,
})
export class AppComponent {
public options;
constructor() {
this.options = {
data: getData(),
title: {
text: "Desktop Operating Systems",
},
series: [
{
type: "bar",
xKey: "os",
yKey: "share",
},
],
axes: {
x: {
type: "category",
label: {
formatter: ({ value }) =>
value === "Windows" ? "== Windows ==" : value,
},
},
y: {
type: "number",
label: {
formatter: ({ value }) => `${value * 100}%`,
},
},
},
};
}
}
export interface DataType {
os: string;
share: number;
}
export function getData(): DataType[] {
return [
{ os: "Windows", share: 0.88 },
{ os: "macOS", share: 0.094 },
{ os: "Linux", share: 0.187 },
];
}
// Angular entry point file
import '@angular/compiler';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
bootstrapApplication(AppComponent);
{
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
valueof the label (without any default formatting applied). - The
indexof 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.
import { Component } from "@angular/core";
import { AgCharts } from "ag-charts-angular";
import {
AgChartOptions,
LegendModule,
LineSeriesModule,
ModuleRegistry,
NumberAxisModule,
UnitTimeAxisModule,
} from "ag-charts-community";
ModuleRegistry.registerModules([
LegendModule,
LineSeriesModule,
NumberAxisModule,
UnitTimeAxisModule,
]);
@Component({
selector: "my-app",
standalone: true,
imports: [AgCharts],
template: `<ag-charts
[options]="options"
></ag-charts>
`,
})
export class AppComponent {
public options;
constructor() {
this.options = {
series: [
{
type: "line",
xKey: "date",
yKey: "temp",
},
],
axes: {
x: {
type: "unit-time",
interval: { step: "month" },
label: {
format: "%b %Y",
},
},
y: {
type: "number",
label: {
format: "$#{0>6.2f}",
},
},
},
data: [
{ date: new Date("2019-01-01"), temp: 82.0 },
{ date: new Date("2019-02-01"), temp: 75.0 },
{ date: new Date("2019-03-01"), temp: 62.0 },
{ date: new Date("2019-04-01"), temp: 99.0 },
{ date: new Date("2019-05-01"), temp: 82.0 },
],
};
}
}
// Angular entry point file
import '@angular/compiler';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
bootstrapApplication(AppComponent);
{
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.
Customisation Copy Link
See Fills & Borders for details on customising axis labels.