In our everyday charting we mainly use absolute values. However, sometimes we might need to show how specific value relates to other elements. In this case relative percent value is much more representative than absolute one. This tutorial will explain ways we can use such relative values.
Let’s start with a fairly simple stacked column chart:
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_dataviz );
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
chart . numberFormatter . numberFormat = " #.# " ;
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " year " ;
categoryAxis . renderer . grid . template . location = 0 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
//valueAxis.renderer.inside = true;
//valueAxis.renderer.labels.template.disabled = true;
function createSeries ( field , name ) {
var series = chart . series . push ( new am4charts . ColumnSeries ());
series . dataFields . valueY = field ;
series . dataFields . categoryX = " year " ;
series . sequencedInterpolation = true ;
//series.cursorTooltipEnabled = false;
series . columns . template . width = am4core . percent ( 60 );
series . columns . template . tooltipText = " [bold]{name}[/] \n [font-size:14px]{categoryX}: {valueY} " ;
series . legendSettings . valueText = " {valueY.close}M " ;
series . legendSettings . itemValueText = " {valueY}M " ;
var labelBullet = series . bullets . push ( new am4charts . LabelBullet ());
labelBullet . label . text = " {valueY} " ;
labelBullet . locationY = 0.5 ;
labelBullet . label . fill = am4core . color ( " #fff " );
createSeries ( " europe " , " Europe " );
createSeries ( " namerica " , " North America " );
createSeries ( " asia " , " Asia-Pacific " );
createSeries ( " lamerica " , " Latin America " );
createSeries ( " meast " , " Middle-East " );
createSeries ( " africa " , " Africa " );
chart . legend = new am4charts . Legend ();
chart . cursor = new am4charts . XYCursor ();
chart . cursor . behavior = " none " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Percent in series percent-in-series
Right now, it operates using absolute values:
Value labels over columns.
Rollover tooltips.
Legend values: both when chart is static and when hovered over specific category.
Let’s see how we can use percent values instead.
Percent calculation is a costly operation in terms of CPU resources. Since in most cases it is not needed, it is disabled by default.
We’ll need to enable it for each series individually. For that we have calculatePercent - a property of Series.
Simply set it to true.
series . calculatePercent = true ;
series . calculatePercent = true ;
Now that we’ve enabled percent calculation, let’s see what we can do with it.
As you probably are aware, we are using “data placeholders ” (references in curly brackets) to insert dynamic data values in labels.
When we are referring to absolute values, we can just reference directly to a “data field” by its name, e.g.: {valueY}.
To show its percent representation, we need to use calculated value, which is referenced like this: {valueY.percent}.
Data labels on columns are implemented by utilizing “label bullets”.
To make them show percent instead of absolute value, we will need to update text value of their label accordingly:
labelBullet . label . text = " {valueY.percent}% " ;
labelBullet . label . text = " {valueY.percent}% " ;
{ // ... "series": [{ // ... "bullets": [{ // ... "label": { // ... "text": "{valueY.percent}%" } }] }, { // ... }, { // ... }]}
It works for tooltips identically:
series . columns . template . tooltipText = " [bold]{name}[/] \n [font-size:14px]{categoryX}: [bold]{valueY.percent}%[/] ({valueY}M) " ;
series . columns . template . tooltipText = " [bold]{name}[/] \n [font-size:14px]{categoryX}: [bold]{valueY.percent}%[/] ({valueY}M) " ;
{ // ... "series": [{ // ... "columns": { // ... "tooltipText": "[bold]{name}[/]\n[font-size:14px]{categoryX}: [bold]{valueY.percent}% ({valueY}M)" } }, { // ... }, { // ... }]}
Same placeholder syntax can be used for legend as well:
series . legendSettings . itemValueText = " {valueY.percent}% " ;
series . legendSettings . itemValueText = " {valueY.percent}% " ;
{ // ... "series": [{ // ... "legendSettings": { "itemValueText": "{valueY.percent}%" } }, { // ... }, { // ... }]}
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_dataviz );
am4core . useTheme ( am4themes_animated );
var chart = am4core . create ( " chartdiv " , am4charts . XYChart );
chart . numberFormatter . numberFormat = " #.0 " ;
var categoryAxis = chart . xAxes . push ( new am4charts . CategoryAxis ());
categoryAxis . dataFields . category = " year " ;
categoryAxis . renderer . grid . template . location = 0 ;
var valueAxis = chart . yAxes . push ( new am4charts . ValueAxis ());
//valueAxis.renderer.inside = true;
//valueAxis.renderer.labels.template.disabled = true;
valueAxis . numberFormatter = new am4core . NumberFormatter ();
valueAxis . numberFormatter . numberFormat = " #.# " ;
function createSeries ( field , name ) {
var series = chart . series . push ( new am4charts . ColumnSeries ());
series . dataFields . valueY = field ;
series . dataFields . categoryX = " year " ;
series . sequencedInterpolation = true ;
series . calculatePercent = true ;
//series.cursorTooltipEnabled = false;
series . columns . template . width = am4core . percent ( 60 );
series . columns . template . tooltipText =
" [bold]{name}[/] \n [font-size:14px]{categoryX}: [bold]{valueY.percent}%[/] ({valueY}M) " ;
series . legendSettings . itemValueText = " {valueY.percent}% " ;
var labelBullet = series . bullets . push ( new am4charts . LabelBullet ());
labelBullet . label . text = " {valueY.percent}% " ;
labelBullet . locationY = 0.5 ;
labelBullet . label . fill = am4core . color ( " #fff " );
createSeries ( " europe " , " Europe " );
createSeries ( " namerica " , " North America " );
createSeries ( " asia " , " Asia-Pacific " );
createSeries ( " lamerica " , " Latin America " );
createSeries ( " meast " , " Middle-East " );
createSeries ( " africa " , " Africa " );
chart . legend = new am4charts . Legend ();
chart . cursor = new am4charts . XYCursor ();
chart . cursor . behavior = " none " ;
< div id = " chartdiv " ></ div >
-apple-system , BlinkMacSystemFont, " Segoe UI " , Roboto, Helvetica , Arial , sans-serif ,
" Apple Color Emoji " , " Segoe UI Emoji " , " Segoe UI Symbol " ;
Percent in series percent-in-series-2