Stacked Bar Chart in JavaScript Charts
Stacked Bar
To render a stacked bar series in your chart, follow the steps below:
-
Set the series type: Define the series
typeasStackingBarin your chart configuration. This indicates that the data should be represented as a stacked bar chart, where each bar consists of multiple segments stacked horizontally on top of each other.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 }, { x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 }, { x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 }, { x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 }, { x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 }, { x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 }, { x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis:
{
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar', name: 'Apple',
dataSource: stackedData, xName: 'x', yName: 'y'
}, {
type: 'StackingBar', name: 'Orange',
dataSource: stackedData, xName: 'x', yName: 'y1'
}, {
type: 'StackingBar', name: 'Wastage',
dataSource: stackedData, xName: 'x', yName: 'y2'
}
],
title: 'Sales Comparison'
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Binding data with series
You can bind data to the chart using the dataSource property within the series configuration. This allows you to connect a JSON dataset or remote data to your chart. To display the data correctly, map the fields from the data to the chart series xName and yName properties.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 }, { x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 }, { x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 }, { x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 }, { x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 }, { x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 }, { x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis:
{
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar', name: 'Apple',
dataSource: stackedData, xName: 'x', yName: 'y'
}, {
type: 'StackingBar', name: 'Orange',
dataSource: stackedData, xName: 'x', yName: 'y1'
}, {
type: 'StackingBar', name: 'Wastage',
dataSource: stackedData, xName: 'x', yName: 'y2'
}
],
title: 'Sales Comparison'
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Series customization
The following properties can be used to customize the StackingBar series.
Solid fill
The fill property determines the color applied to the series.
var chartData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 },
{ x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 },
{ x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 },
{ x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 },
{ x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 },
{ x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 },
{ x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart(
{
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis: {
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar',
name: 'Apple',
dataSource: chartData,
xName: 'x',
yName: 'y',
fill: '#483D8B'
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: chartData,
xName: 'x',
yName: 'y1',
fill: '#556B2F'
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: chartData,
xName: 'x',
yName: 'y2',
fill: '#8B0000'
}
],
title: 'Sales Comparison'
},
'#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Gradient fill
The fill property also applies an SVG gradient to a stacked bar series. Define the gradient within an SVG <defs> element using a unique ID, and assign it to the series in the url(#gradientId) format. This produces a smooth color transition across the stacked segments, useful for distinguishing consecutive stacks or emphasizing totals.
var chartData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 },
{ x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 },
{ x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 },
{ x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 },
{ x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 },
{ x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 },
{ x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart(
{
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis: {
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar',
name: 'Apple',
dataSource: chartData,
xName: 'x',
yName: 'y',
fill: 'url(#gradient1)'
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: chartData,
xName: 'x',
yName: 'y1',
fill: 'url(#gradient2)'
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: chartData,
xName: 'x',
yName: 'y2',
fill: 'url(#gradient3)'
}
],
title: 'Sales Comparison'
},
'#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<svg style="width:1px; height:1px">
<defs>
<linearGradient id="gradient1">
<stop offset="0%" style="stop-color:coral;stop-opacity:5" />
<stop offset="50%" style="stop-color:mediumseagreen;stop-opacity:5" />
</linearGradient>
</defs>
</svg>
<svg style="width:1px; height:1px">
<defs>
<linearGradient id="gradient2">
<stop offset="0%" style="stop-color:darkred;stop-opacity:5" />
<stop offset="50%" style="stop-color:darkorange;stop-opacity:5" />
</linearGradient>
</defs>
</svg>
<svg style="width:1px; height:1px">
<defs>
<linearGradient id="gradient3">
<stop offset="0%" style="stop-color:darkmagenta;stop-opacity:5" />
<stop offset="50%" style="stop-color:darkcyan;stop-opacity:5" />
</linearGradient>
</defs>
</svg>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body>
</html>Opacity
The opacity property controls the transparency of the fill and affects how the series blends with background or overlapping series.
var chartData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 },
{ x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 },
{ x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 },
{ x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 },
{ x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 },
{ x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 },
{ x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart(
{
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis: {
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar',
name: 'Apple',
dataSource: chartData,
xName: 'x',
yName: 'y',
opacity: 0.7
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: chartData,
xName: 'x',
yName: 'y1',
opacity: 0.7
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: chartData,
xName: 'x',
yName: 'y2',
opacity: 0.7
}
],
title: 'Sales Comparison'
},
'#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Border
Use the border property to configure the border width, color, and dash array of the stacked bar series.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 },
{ x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 },
{ x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 },
{ x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 },
{ x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 },
{ x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 },
{ x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis: {
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar',
name: 'Apple',
dataSource: stackedData,
xName: 'x',
yName: 'y',
border: { width: 2, color: '#ff4251', dashArray: '5,5' }
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: stackedData,
xName: 'x',
yName: 'y1',
border: { width: 2, color: '#66BDB7', dashArray: '5,5' }
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: stackedData,
xName: 'x',
yName: 'y2',
border: { width: 2, color: '#794F1B', dashArray: '5,5' }
}
],
title: 'Sales Comparison'
},
'#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Stacking group
Use the stackingGroup property to group stacked bars with 100% stacked bars. Series that share the same group name are stacked together, while different group names are rendered side by side.
var stackedData = [
{ x: 2007, y: 453, y1: 876, y2: 356, y3: 122 }, { x: 2008, y: 354, y1: 564, y2: 876, y3: 444 },
{ x: 2009, y: 282, y1: 242, y2: 898, y3: 222 }, { x: 2010, y: 321, y1: 121, y2: 567, y3: 231 },
{ x: 2011, y: 333, y1: 343, y2: 456, y3: 122 }, { x: 2012, y: 351, y1: 451, y2: 345, y3: 333 },
{ x: 2013, y: 403, y1: 203, y2: 543, y3: 354 }, { x: 2014, y: 421, y1: 431, y2: 654, y3: 100 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Year',
minimum:2006,
maximum:2015,
interval:1
},
primaryYAxis:
{
title: 'Sales Percentage(%)'
},
series: [
{
type: 'StackingBar', name: 'John',
//Stacking group for stacked bar
stackingGroup: 'JohnAndAndrew',
dataSource: stackedData, xName: 'x', yName: 'y'
},
{
type: 'StackingBar', name: 'Andrew',
stackingGroup: 'JohnAndAndrew',
dataSource: stackedData, xName: 'x', yName: 'y1'
},
{
type: 'StackingBar', name: 'Thomas',
stackingGroup: 'ThomasAndMichael',
dataSource: stackedData, xName: 'x', yName: 'y2'
},
{
type: 'StackingBar', name: 'Michael',
stackingGroup: 'ThomasAndMichael',
dataSource: stackedData, xName: 'x', yName: 'y3'
}
],
title: 'Sales by year'
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Cylindrical stacked bar chart
To render a cylindrical stacked bar chart, set the columnFacet property to Cylinder in the chart series. This property transforms the regular stacked bars into cylindrical shapes, enhancing the visual representation of the data.
var cylindricalData = [
{ x: 2000, y: 0.61, y1: 0.03, y2: 0.48 }, { x: 2001, y: 0.81, y1: 0.05, y2: 0.53 },
{ x: 2002, y: 0.91, y1: 0.06, y2: 0.57 }, { x: 2003, y: 1, y1: 0.09, y2: 0.61 },
{ x: 2004, y: 1.19, y1: 0.14, y2: 0.63 }, { x: 2005, y: 1.47, y1: 0.20, y2: 0.64 },
{ x: 2006, y: 1.74, y1: 0.29, y2: 0.66 }, { x: 2007, y: 1.98, y1: 0.46, y2: 0.76 },
{ x: 2008, y: 1.99, y1: 0.64, y2: 0.77 }, { x: 2009, y: 1.70, y1: 0.75, y2: 0.55 }
];
var chart = new ej.charts.Chart({
series: [
{
//Series type as stacked bar with cylinder shape
type: 'StackingBar', columnFacet: 'Cylinder',
dataSource: cylindricalData, xName: 'x', yName: 'y'
},
{
type: 'StackingBar', columnFacet: 'Cylinder',
dataSource: cylindricalData, xName: 'x', yName: 'y1'
},
{
type: 'StackingBar', columnFacet: 'Cylinder',
dataSource: cylindricalData, xName: 'x', yName: 'y2'
}
]
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Empty points
Data points with null or undefined values are considered empty. Configure the emptyPointSettings on the series to control how empty data points are rendered.
Mode
Use the mode property to control handling of empty points. Available modes are Gap, Drop, Zero, and Average. The default mode is Gap.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 },
{ x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: null },
{ x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 },
{ x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: undefined, y2: -4 },
{ x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 },
{ x: 'Oct', y: null, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 },
{ x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis: {
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar',
name: 'Apple',
dataSource: stackedData,
xName: 'x',
yName: 'y',
emptyPointSettings: {
mode: 'Zero'
}
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: stackedData,
xName: 'x',
yName: 'y1',
emptyPointSettings: {
mode: 'Gap'
}
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: stackedData,
xName: 'x',
yName: 'y2',
emptyPointSettings: {
mode: 'Average'
}
}
],
title: 'Sales Comparison'
},
'#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Fill
Use the fill property to set the fill color for empty points.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 },
{ x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: null },
{ x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 },
{ x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: undefined, y2: -4 },
{ x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 },
{ x: 'Oct', y: null, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 },
{ x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis: {
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar',
name: 'Apple',
dataSource: stackedData,
xName: 'x',
yName: 'y',
emptyPointSettings: {
mode: 'Zero'
}
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: stackedData,
xName: 'x',
yName: 'y1',
emptyPointSettings: {
mode: 'Gap'
}
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: stackedData,
xName: 'x',
yName: 'y2',
emptyPointSettings: {
mode: 'Average',
fill: 'red'
}
}
],
title: 'Sales Comparison'
},
'#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Border
Use the border property to customize the border width and color for empty points.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 },
{ x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: null },
{ x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 },
{ x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: undefined, y2: -4 },
{ x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 },
{ x: 'Oct', y: null, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 },
{ x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis: {
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar',
name: 'Apple',
dataSource: stackedData,
xName: 'x',
yName: 'y',
emptyPointSettings: {
mode: 'Zero'
}
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: stackedData,
xName: 'x',
yName: 'y1',
emptyPointSettings: {
mode: 'Gap'
}
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: stackedData,
xName: 'x',
yName: 'y2',
emptyPointSettings: {
mode: 'Average',
fill: 'red',
border: { width: 2, color: 'green' }
}
}
],
title: 'Sales Comparison'
},
'#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Stack labels
The stack labels in stacked charts display cumulative total values for stack segments directly using data labels. Configure the stackLabels on the chart to enable and customize these labels. If a stacked point has negative values, the stack labels are displayed below the point.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 }, { x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 }, { x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 }, { x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 }, { x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 }, { x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 }, { x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis:
{
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar', name: 'Apple',
dataSource: stackedData, xName: 'x', yName: 'y', marker:{dataLabel : {visible : true}},
}, {
type: 'StackingBar', name: 'Orange',
dataSource: stackedData, xName: 'x', yName: 'y1', marker:{dataLabel : {visible : true}},
}, {
type: 'StackingBar', name: 'Wastage',
dataSource: stackedData, xName: 'x', yName: 'y2', marker:{dataLabel : {visible : true}},
}
],
stackLabels: {
visible: true
},
title: 'Sales Comparison'
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Stack labels customization
Stack labels expose the following properties for customization:
-
visible- Specifies whether stack labels are visible. Setting to true will display the labels. Default is false. -
fill- Defines the background color of the stack labels. Accepts valid CSS color strings (hex, RGBA, etc.). Default is transparent. -
format- Formats the text displayed in the stack labels. Supports placeholders like {value}. Default is null. -
angle- Specifies the rotation angle for stack labels in degrees. Default is 0. -
rx- Defines the rounded corner radius along the X-axis (horizontal direction) for the stack label background. Default is 5. -
ry- Defines the rounded corner radius along the Y-axis (vertical direction) for the stack label background. Default is 5. -
margin- Configures the margin around the stack label (left, right, top, and bottom). -
border- Configures the appearance of the stack label’s border. -
font- Customizes the stack label text, including font size, color, style, weight, and family.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 }, { x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 }, { x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 }, { x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 }, { x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 }, { x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 }, { x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis:
{
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar', name: 'Apple',
dataSource: stackedData, xName: 'x', yName: 'y', marker:{dataLabel : {visible : true}},
}, {
type: 'StackingBar', name: 'Orange',
dataSource: stackedData, xName: 'x', yName: 'y1', marker:{dataLabel : {visible : true}},
}, {
type: 'StackingBar', name: 'Wastage',
dataSource: stackedData, xName: 'x', yName: 'y2', marker:{dataLabel : {visible : true}},
}
],
stackLabels: { visible: true, fill: 'rgba(0, 123, 255, 0.5)', format: '{value}', angle: 45, rx: 10, ry: 10, margin: { left: 10, right: 10, top: 10, bottom: 10 }, border: { width: 2, color: '#000' }, font: { size: '14px', color: '#fff', weight: 'bold', family: 'Arial', textAlignment: 'Left' } },
title: 'Sales Comparison'
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Corner radius
The cornerRadius property in the chart series is used to customize the corner radius for stacked bar series. This allows you to create stacked bars with rounded corners. You can customize each corner of the stacked bars using the topLeft, topRight, bottomLeft, and bottomRight properties.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 }, { x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 }, { x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 }, { x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 }, { x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 }, { x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 }, { x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis:
{
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar', name: 'Apple',
dataSource: stackedData, xName: 'x', yName: 'y'
}, {
cornerRadius: { topRight: 10, bottomRight: 10 },
type: 'StackingBar', name: 'Orange',
dataSource: stackedData, xName: 'x', yName: 'y1'
}, {
cornerRadius: { topRight: 10, bottomRight: 10 },
type: 'StackingBar', name: 'Wastage',
dataSource: stackedData, xName: 'x', yName: 'y2'
}
],
title: 'Sales Comparison',
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Point corner radius
You can customize the corner radius for individual points in the chart series using the pointRender event by setting the cornerRadius property in its event argument.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 }, { x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 }, { x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 }, { x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 }, { x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 }, { x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 }, { x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis:
{
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar', name: 'Apple',
dataSource: stackedData, xName: 'x', yName: 'y'
}, {
type: 'StackingBar', name: 'Orange',
dataSource: stackedData, xName: 'x', yName: 'y1'
}, {
type: 'StackingBar', name: 'Wastage',
dataSource: stackedData, xName: 'x', yName: 'y2'
}
],
title: 'Sales Comparison',
pointRender: function (args) {
if (args.point.index === 1 && args.point.series.index === 1) {
args.cornerRadius = { topLeft: 0, bottomLeft: 0, topRight: 10, bottomRight: 10 };
}
if (args.point.index === 4 && args.point.series.index === 1) {
args.cornerRadius = { topLeft: 0, bottomLeft: 0, topRight: 10, bottomRight: 10 };
}
if (args.point.index === 6 && args.point.series.index === 1) {
args.cornerRadius = { topLeft: 0, bottomLeft: 0, topRight: 10, bottomRight: 10 };
}
if (args.point.index === 8 && args.point.series.index === 1) {
args.cornerRadius = { topLeft: 0, bottomLeft: 0, topRight: 10, bottomRight: 10 };
}
}
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Events
Series render
The seriesRender event enables modification of series properties (for example, data, fill, or name) immediately before rendering. Use this event to adjust series appearance or to dynamically swap data sources.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 }, { x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 }, { x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 }, { x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 }, { x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 }, { x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 }, { x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis:
{
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar', name: 'Apple',
dataSource: stackedData, xName: 'x', yName: 'y'
}, {
type: 'StackingBar', name: 'Orange',
dataSource: stackedData, xName: 'x', yName: 'y1'
}, {
type: 'StackingBar', name: 'Wastage',
dataSource: stackedData, xName: 'x', yName: 'y2'
}
],
title: 'Sales Comparison',
seriesRender: function (args) {
if (args.series.index === 0) {
args.fill = '#ff4251';
}
else if (args.series.index === 1) {
args.fill = '#4C4C4C';
}
else if (args.series.index === 2) {
args.fill = '#794F1B';
}
}
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Point render
The pointRender event provides a hook to customize each data point (for example, marker shape, border, or fill) before it is drawn. Use this to apply per-point styling rules or conditional formatting.
var stackedData = [
{ x: 'Jan', y: 6, y1: 6, y2: -1 }, { x: 'Feb', y: 8, y1: 8, y2: -1.5 },
{ x: 'Mar', y: 12, y1: 11, y2: -2 }, { x: 'Apr', y: 15, y1: 16, y2: -2.5 },
{ x: 'May', y: 20, y1: 21, y2: -3 }, { x: 'Jun', y: 24, y1: 25, y2: -3.5 },
{ x: 'Jul', y: 28, y1: 27, y2: -4 }, { x: 'Aug', y: 32, y1: 31, y2: -4.5 },
{ x: 'Sep', y: 33, y1: 34, y2: -5 }, { x: 'Oct', y: 35, y1: 34, y2: -5.5 },
{ x: 'Nov', y: 40, y1: 41, y2: -6 }, { x: 'Dec', y: 42, y1: 42, y2: -6.5 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Months'
},
primaryYAxis:
{
title: 'Percentage (%)',
minimum: -20,
maximum: 100,
labelFormat: '{value}%',
edgeLabelPlacement: 'Shift'
},
series: [
{
//Series type as stacked bar
type: 'StackingBar', name: 'Apple',
dataSource: stackedData, xName: 'x', yName: 'y'
}, {
type: 'StackingBar', name: 'Orange',
dataSource: stackedData, xName: 'x', yName: 'y1'
}, {
type: 'StackingBar', name: 'Wastage',
dataSource: stackedData, xName: 'x', yName: 'y2'
}
],
title: 'Sales Comparison',
pointRender: function (args) {
if (args.point.series.index % 2 !== 0) {
args.fill = '#ff6347';
}
else {
args.fill = '#009cb8';
}
}
}, '#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="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Troubleshooting
The following list describes common issues you may encounter when working with the Stacked Bar series and how to resolve them.
Chart does not render a stacked bar: Confirm the type property of each series is set to StackingBar. If type is omitted or set to a different value (for example, Bar), the series is drawn as overlapping (non-stacked) bars. In JavaScript, the StackingBarSeries module is loaded through the script bundle; no explicit Chart.Inject(...) call is required.
Bars are not stacked or stack incorrectly: In stacked series, each successive series contributes to the cumulative total by being rendered above the previous series. Confirm that each series uses a unique yName (or series-specific data field) and that all series share the same xName; otherwise, points fall out of order and segments misalign. To stack only a subset, set stackingGroup to a shared identifier across the desired series.
Cylindrical stacked bar does not render: Confirm columnFacet is set to Cylinder (case-sensitive). The only other valid value is Rectangle (the default), which renders rectangular bars.
Corner radius has no effect: The cornerRadius property requires non-zero widths (typically border.width > 0 or a non-zero series.width). Set both cornerRadius and border.width to a positive value, otherwise rounded corners are not visible.
Stack labels do not appear: Stack labels are off by default. Set visible to true on stackLabels to render the cumulative totals at each segment end. Negative values are automatically rendered below the point.
Empty-point mode produces no visible point: The default mode is Gap, which leaves a gap between stacked bars. To render empty points, switch the mode to Zero or Average and (optionally) set a fill on emptyPointSettings, otherwise the stack total may collapse locally.