This tutorial will show how you can leverage a number of settings on Series and Axes to create offset/overlaid columns effect.
In the course of this tutorial we’re going to turn this:
Into this:
Here’s a live version of the source chart:
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) Run * ---------------------------------------
* 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 );
category: " Distribution " ,
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " category " ;
categoryAxis . renderer . grid . template . location = 0 ;
categoryAxis . renderer . minGridDistance = 30 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var series = chart . series . push ( new am4charts . ColumnSeries ());
series . dataFields . valueY = " value " ;
series . dataFields . categoryX = " category " ;
series . name = " Series #1 " ;
var series2 = chart . series . push ( new am4charts . ColumnSeries ());
series2 . dataFields . valueY = " value2 " ;
series2 . dataFields . categoryX = " category " ;
series2 . name = " Series #1 " ;
var series3 = chart . series . push ( new am4charts . ColumnSeries ());
series3 . dataFields . valueY = " value3 " ;
series3 . dataFields . categoryX = " category " ;
series3 . name = " Series #3 " ;
chart . legend = new am4charts . Legend ();
chart . legend . useDefaultMarker = true ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Overlaid column clusters overlaid-column-clusters
Patient’s ready. Let’s get started.
By default, all Column series on the chart get clustered, meaning that each category/date will be divvied up between each series in equal parts.
Since we want our columns to be overlaid, we need to disable that.
ColumnSeries’s clustered propeprty will do the trick:
series1 . clustered = false ; // ...series2.clustered = false;// ...series3.clustered = false;
series1 . clustered = false ; // ...series2.clustered = false;// ...series3.clustered = false;
{ // … "series": [{ // … "clustered": false }, { // … "clustered": false }, { // … "clustered": false }]}
Here’s where that gets us:
Now our columns are not clustered anymore, but they take up the whole width and overlap each other to the point of being unusable.
Let’s see what we can do about it.
To make our chart more usable, let’s shift our first column a bit to the left, and the third column a bit to the right. This will give us a bit of a non-overlapping space, so all columns are visible to some extent.
To shift any object in amCharts horizontally, we can use its dx property. It basically tells “move the object to the right (or left if value is negative) by X pixels”.
series1 . columns . template . dx = - 20 ; // ...// We don't want middle column to shift, so we don't set dx on it// ...series3.columns.template.dx = -20;
series1 . columns . template . dx = - 20 ; // ...// We don't want middle column to shift, so we don't set dx on it// ...series3.columns.template.dx = -20;
{ // … "series": [{ // … "columns": { "dx": -20 } }, { // … // Middle column stays put }, { // … "columns": { "dx": 20 } }]}
Here’s the result:
OK, now we can easily make out each individual column, bringing our chart back to the realm of useful data viz.
The columns look a bit fat, though. Let’s slim them down.
By default, each column takes 80% of the width allotted to it. If we need to change that, we can use column template’s width property.
More info
The whole logic of column width and its relation to clustering, axes, cells, and related stuff is thoroughly explained here .
Let’s set the columns to use just 50% of the available space.
series1 . columns . template . width = am4core . percent ( 50 ); // ...series2.columns.template.width = am4core.percent(50);// ...series3.columns.template.width = am4core.percent(50);
series1 . columns . template . width = am4core . percent ( 50 ); // ...series2.columns.template.width = am4core.percent(50);// ...series3.columns.template.width = am4core.percent(50);
{ // … "series": [{ // … "columns": { // … "width": "50%" } }, { // … "columns": { // … "width": "50%" } }, { // … "columns": { // … "width": "50%" } }]}
The result:
Looks quite nice now. Let’s add the final cherry on the top.
Result JS HTML CSS
Open in: New tab CodePen (opens in a new tab) jsFiddle (opens in a new tab) Run * ---------------------------------------
* 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 );
category: " Distribution " ,
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " category " ;
categoryAxis . renderer . grid . template . location = 0 ;
categoryAxis . renderer . minGridDistance = 30 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
var series1 = chart . series . push ( new am4charts . ColumnSeries ());
series1 . dataFields . valueY = " value " ;
series1 . dataFields . categoryX = " category " ;
series1 . columns . template . width = am4core . percent ( 50 );
series1 . clustered = false ;
series1 . columns . template . dx = - 20 ;
//series1.columns.template.align = "left"
series1 . name = " Series #1 " ;
var series2 = chart . series . push ( new am4charts . ColumnSeries ());
series2 . dataFields . valueY = " value2 " ;
series2 . dataFields . categoryX = " category " ;
series2 . columns . template . width = am4core . percent ( 50 );
series2 . clustered = false ;
series2 . name = " Series #1 " ;
var series3 = chart . series . push ( new am4charts . ColumnSeries ());
series3 . dataFields . valueY = " value3 " ;
series3 . dataFields . categoryX = " category " ;
series3 . clustered = false ;
series3 . name = " Series #3 " ;
series3 . columns . template . width = am4core . percent ( 50 );
series3 . columns . template . dx = 20 ;
series3 . columns . template . align = " left " ;
var label = series3 . columns . template . createChild ( am4core . Label );
label . fill = am4core . color ( " #fff " );
chart . legend = new am4charts . Legend ();
chart . legend . useDefaultMarker = true ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Overlaid column clusters overlaid-column-clusters-2