How to display stacking series totals in JavaScript Charts
Use one point-based annotation for each X-axis category and set its content to the total of all stacked values in that category. Position each annotation slightly above the corresponding stack.
Calculate the stack totals
Calculate totals from the source data instead of relying on internal series state. This keeps the displayed totals synchronized when the data changes.
function getTotal(point) {
return point.apple + point.orange + point.grapes;
}Create one annotation per category
Map the source data to annotation objects. Set coordinateUnits to Point, use the category as x, and place y slightly above the calculated total.
var annotations = fruitData.map(function (point) {
var total = getTotal(point);
return {
content: '<span class="stack-total">' + total + '</span>',
x: point.person,
y: total + 0.5,
coordinateUnits: 'Point',
region: 'Series',
horizontalAlignment: 'Center',
verticalAlignment: 'Bottom'
};
});coordinateUnits: 'Point' positions the annotation with axis values, and region: 'Series' positions it relative to the series area.
Complete example
var fruitData = [{
person: 'James',
apple: 5,
orange: 4,
grapes: 1
}, {
person: 'Michael',
apple: 4,
orange: 3,
grapes: 2
}, {
person: 'John',
apple: 5,
orange: 4,
grapes: 2
}];
function getTotal(point) {
return point.apple + point.orange + point.grapes;
}
function createAnnotations(data) {
return data.map(function (point) {
var total = getTotal(point);
return {
content: '<span class="stack-total">' + total + '</span>',
x: point.person,
y: total + 0.5,
coordinateUnits: 'Point',
region: 'Series',
horizontalAlignment: 'Center',
verticalAlignment: 'Bottom',
description: 'Total for ' + point.person + ': ' + total
};
});
}
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
interval: 1,
title: 'Person',
majorGridLines: {
width: 0
}
},
primaryYAxis: {
minimum: 0,
maximum: 13,
interval: 2,
title: 'Fruit count'
},
series: [{
type: 'StackingColumn',
dataSource: fruitData,
xName: 'person',
yName: 'apple',
name: 'Apple',
animation: {
enable: false
},
marker: {
dataLabel: {
visible: true,
position: 'Middle',
font: {
color: '#ffffff',
fontWeight: '600'
}
}
}
}, {
type: 'StackingColumn',
dataSource: fruitData,
xName: 'person',
yName: 'orange',
name: 'Orange',
animation: {
enable: false
},
marker: {
dataLabel: {
visible: true,
position: 'Middle',
font: {
color: '#ffffff',
fontWeight: '600'
}
}
}
}, {
type: 'StackingColumn',
dataSource: fruitData,
xName: 'person',
yName: 'grapes',
name: 'Grapes',
animation: {
enable: false
},
marker: {
dataLabel: {
visible: true,
position: 'Middle',
font: {
color: '#ffffff',
fontWeight: '600'
}
}
}
}],
annotations: createAnnotations(fruitData),
tooltip: {
enable: true,
shared: true
},
title: 'Fruit Consumption',
width: '100%',
height: '400px'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div class="col-sm-8">
<div class="row">
<div class="col-sm-4">
<div id="container">
<div id="element" style="width:350px; height:350px;float:left">
</div>
<label id="lbl"></label>
</div>
</div>
<div class="col-sm-4" style="width:200px; height:350px;float: right">
<div id="Grid">
</div>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Dynamic data updates
When the source data changes, recalculate the annotations before refreshing the Chart.
chart.annotations = createAnnotations(fruitData);
chart.refresh();If the number or order of categories changes, recreating the annotation collection prevents totals from being assigned to the wrong category.
Troubleshooting
- If totals do not appear, verify that Chart Annotation support is available in the loaded EJ2 ES5 bundle.
- If totals appear above the wrong category, verify that annotation
xvalues exactly match the X-axis category values. - If totals are clipped, increase the Y-axis maximum or Chart top margin.
- If totals are stale after a data change, rebuild
chart.annotationsbefore callingrefresh(). - If a total is incorrect, verify that every stacked field is numeric and included in
getTotal().