This tutorial will explain how you can easily make your chart grow or contract based on an actual number of data.
Let’s take a simple bar chart:

It looks OK.
However, it height is fixed, so if we had more bars, it might start looking a bit awkward:

Or, if there are too few, we end up with huge bars wasting up space:

This is happening because the category axis will always try to adjust size of the bars for a better fit.
However, we can’t always anticipate number of bars in our data, and therefore cannot hardcode the height of the chart container.
An obvious solution is to have chart container auto-adjust its height.
We will do that by utilizing series’ "datavalidated" event, which kicks in at the moment it has finished processing its data.
At that moment we already know precisely how many bars we have, so we can calculate our target height for the chart container.
series.events.on("datavalidated", function(ev) {
let chart = series.chart;
let xAxis = chart.xAxes.getIndex(0);
// Calculate how we need to adjust chart height
let chartHeight = series.data.length * cellSize + xAxis.height() + chart.get("paddingTop", 0) + chart.get("paddingBottom", 0);
// Set it on chart's container
chart.root.dom.style.height = chartHeight + "px";
series.events.on("datavalidated", function(ev) {
var chart = series.chart;
var xAxis = chart.xAxes.getIndex(0);
// Calculate how we need to adjust chart height
var chartHeight = series.data.length * cellSize + xAxis.height() + chart.get("paddingTop", 0) + chart.get("paddingBottom", 0);
// Set it on chart's container
chart.root.dom.style.height = chartHeight + "px";
* ---------------------------------------
* This demo was created using amCharts 5.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://docs.amcharts.com/v5/
* ---------------------------------------
// https://docs.amcharts.com/v5/getting-started/#root-element
var root = am5.Root.new("chartdiv");
// https://docs.amcharts.com/v5/concepts/themes/
root.setThemes([am5themes_Animated.new(root)]);
var data = generateData();
// https://docs.amcharts.com/v5/charts/xy-chart/
var chart = root.container.children.push(
am5xy.XYChart.new(root, {
layout: root.verticalLayout
// https://docs.amcharts.com/v5/charts/xy-chart/axes/
var yAxis = chart.yAxes.push(
am5xy.CategoryAxis.new(root, {
categoryField: "category",
renderer: am5xy.AxisRendererY.new(root, {
var xAxis = chart.xAxes.push(
am5xy.ValueAxis.new(root, {
renderer: am5xy.AxisRendererX.new(root, {})
// https://docs.amcharts.com/v5/charts/xy-chart/series/
var series = chart.series.push(
am5xy.ColumnSeries.new(root, {
categoryYField: "category"
series.events.on("datavalidated", function (ev) {
var chart = series.chart;
var xAxis = chart.xAxes.getIndex(0);
// Calculate how we need to adjust chart height
series.data.length * cellSize +
chart.get("paddingTop", 0) +
chart.get("paddingBottom", 0);
// Set it on chart's container
chart.root.dom.style.height = chartHeight + "px";
series.data.setAll(data);
// Make stuff animate on load
// https://docs.amcharts.com/v5/concepts/animations/
function generateData() {
var count = Math.random() * 10;
for (var i = 0; i < count; i++) {
category: "Category #" + i,
value: Math.round(Math.random() * 100)
series.data.setAll(data);
<input type="button" value="Update chart data" onclick="updateData()" />
<div id="chartdiv"></div>
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
"category": "Category #0",
"category": "Category #1",
"category": "Category #2",
"category": "Category #3",
"categoryField": "category",
"categoryYField": "category"
Auto-adjusting chart container height based on a number of data