Stacking total in EJ2 JavaScript Chart control
8 May 20239 minutes to read
By using the annotation
, you can show any element in desired view.
To show the total value in data points, follow the given steps:
Step 1:
Define annotation for each x point in chart, now change the annotation value in chart by using the annotationRender
event. In this event, assign the stacked value of the last series to the annotation to show the total value of the
stacking series.
var i = 0;
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category', interval: 1
},
// Initialize the chart series
series: [
{
type: 'StackingColumn', xName: 'x', width: 2, yName: 'y', name: 'Apple', animation: {enable: false},
dataSource: [{ x: 'Jamesh', y: 5 }, { x: 'Michael', y: 4 }, { x: 'John', y: 5 }],
marker: { dataLabel: { visible: true, position: 'Top', font: { fontWeight: '600', color: '#ffffff' } } }
}, {
type: 'StackingColumn', xName: 'x', width: 2, yName: 'y', name: 'Orange', animation: {enable: false},
dataSource: [{ x: 'Jamesh', y: 4 }, { x: 'Michael', y: 3 }, { x: 'John', y: 4 }],
marker: { dataLabel: { visible: true, position: 'Top', font: { fontWeight: '600', color: '#ffffff' } } }
},
{
type: 'StackingColumn', xName: 'x', width: 2, yName: 'y', name: 'Grapes', animation: {enable: false},
dataSource: [{ x: 'Jamesh', y: 1 }, { x: 'Michael', y: 2 }, { x: 'John', y: 2 }],
marker: { dataLabel: { visible: true, position: 'Top', font: { fontWeight: '600', color: '#ffffff' } } }
}
],
annotations:[
{
content: '<div id="point1" style="font-size:11px;font-weight:bold;color:gray;fill:gray;"><span>12</span></div>',
x: 'Jamesh', y: '11', coordinateUnits: 'Point', region: 'Series'
},
{
content: '<div id="point1" style="font-size:11px;font-weight:bold;color:gray;fill:gray;"><span>12</span></div>',
x: 'Michael', y: '10', coordinateUnits: 'Point', region: 'Series'
},
{
content: '<div id="point1" style="font-size:11px;font-weight:bold;color:gray;fill:gray;"><span>12</span></div>',
x: 'John', y: '12', coordinateUnits: 'Point', region: 'Series'
}
],
// Initialize the chart title
title: 'Fruit Consumption', tooltip: { enable: true, shared: true },
annotationRender: function(args) {
var length = args.chart.series.length - 1;
var value = args.chart.series[length].stackedValues.endValues[i];
i += (i == length) ? -length : 1;
args.content.children[0].children[0].innerHTML = value;
},
width:'650px',
height: '350px'
}, '#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://cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.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>