This short tutorial will show how you can make your XY chart more readable by introducing a fill between each second set of grid lines.
We’ll let you in on a secret: each axis is already filling the space between each second set of its grid lines.
You just can’t see it because those fills are disabled by default.
They are there, though. In the axis renderer’s axisFills list.
Naturally, if we want to enable and configure those fills, we need to modify renderer.axisFills.template.
categoryAxis.renderer.axisFills.template.disabled = false;
categoryAxis.renderer.axisFills.template.fillOpacity = 0.1;
categoryAxis.renderer.axisFills.template.fill = am4core.color("red");
categoryAxis.renderer.axisFills.template.disabled = false;
categoryAxis.renderer.axisFills.template.fillOpacity = 0.1;
categoryAxis.renderer.axisFills.template.fill = am4core.color("red");
* ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4charts.XYChart);
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.axisFills.template.disabled = false;
categoryAxis.renderer.axisFills.template.fillOpacity = 0.1;
categoryAxis.renderer.axisFills.template.fill = am4core.color("red");
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var series = chart.series.push(new am4charts.StepLineSeries());
series.dataFields.valueY = "value";
series.dataFields.categoryX = "category";
<div id="chartdiv"></div>
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
Alternating axis fills (1)alternating-axis-fills-1
Any axis type can use axis fills. You can even enable them on several axis, to create crazy checkered setups:
* ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4charts.XYChart);
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.axisFills.template.disabled = false;
categoryAxis.renderer.axisFills.template.fillOpacity = 0.1;
categoryAxis.renderer.axisFills.template.fill = am4core.color("red");
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.axisFills.template.disabled = false;
valueAxis.renderer.axisFills.template.fillOpacity = 0.1;
valueAxis.renderer.axisFills.template.fill = am4core.color("green");
var series = chart.series.push(new am4charts.StepLineSeries());
series.dataFields.valueY = "value";
series.dataFields.categoryX = "category";
<div id="chartdiv"></div>
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
Alternating axis fills (2)alternating-axis-fills-2
Axis fills work within plot area only. That said, there’s a trick to extend the fill to go beyond, so it covers axis labels as well.
We can use an adapter to modify plot area’s height via pixelHeight (or pixelWidth if we’re modifying horizontal fills):
chart.plotContainer.adapter.add("pixelHeight", function(value, target) {
chart.plotContainer.adapter.add("pixelHeight", function(value, target) {
"pixelHeight": function(value, target) {
The above will make plot area higher by 40 pixels, which will fool axis fills, striving to cover the whole height, to grow as well:
* ---------------------------------------
* This demo was created using amCharts 4.
* For more information visit:
* https://www.amcharts.com/
* Documentation is available at:
* https://www.amcharts.com/docs/v4/
* ---------------------------------------
am4core.useTheme(am4themes_animated);
var chart = am4core.create("chartdiv", am4charts.XYChart);
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.minGridDistance = 30;
categoryAxis.renderer.axisFills.template.disabled = false;
categoryAxis.renderer.axisFills.template.fillOpacity = 0.1;
categoryAxis.renderer.axisFills.template.fill = am4core.color("red");
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var series = chart.series.push(new am4charts.StepLineSeries());
series.dataFields.valueY = "value";
series.dataFields.categoryX = "category";
// Make the axis fill a bit higher so it extends to axis label
chart.plotContainer.adapter.add("pixelHeight", (value, target) => {
<div id="chartdiv"></div>
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
Alternating axis fills (3)alternating-axis-fills-3