Skip to content

Category axis

Category axis type allows plotting data attached to string-based names (categories).

Category axis is the only axis type that requires its own data as well as data field set up.

It’s needed because the axis needs to know what categories to plot.

It can be the same data we use for the actual series, e.g.:

let xAxis = chart.xAxes.push(
am5xy.CategoryAxis.new(root, {
categoryField: "category",
renderer: am5xy.AxisRendererX.new(root, {})
})
);
xAxis.data.setAll(data);
let series = chart.series.push(
am5xy.ColumnSeries.new(root, {
name: "Series",
xAxis: xAxis,
yAxis: yAxis,
valueYField: "value",
categoryXField: "category"
})
);
series.data.setAll(data);

Or it can be a subset of data.

Having different data for category axis makes sense in the situations where we want to have only specific categories displayed:

let xAxis = chart.xAxes.push(
am5xy.CategoryAxis.new(root, {
categoryField: "category",
renderer: am5xy.AxisRendererX.new(root, {})
})
);
xAxis.data.setAll([{
category: "Research"
}, {
category: "Marketing"
}, {
category: "Sales"
}]);

We can use value axis’ method zoomToCategories() to zoom the axis to specific range between two categories:

xAxis.zoomToCategories("Mike", "Dorothy");

We can also make category axis zoom to specific indexes via zoomToIndexes():

xAxis.zoomToIndexes(10, 20);

Category axis uses own data, so we can use its own datavalidated event to invoke zooming functions:

xAxis.events.once("datavalidated", function(ev) {
ev.target.zoomToIndexes(10, 20);
});

Read more about pre-zooming using series events in “Zoom and pan: Pre-zooming axes”.