Stack bar Chart in EJ2 JavaScript control

28 Sep 202321 minutes to read

Stacked bar

To render a stacked bar series, use series type as StackingBar and inject StackingBarSeries module using Chart.Inject(StackingBarSeries) method.

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'
            }, {
                type: 'StackingBar', name: 'Orange',
                dataSource: chartData, xName: 'x', yName: 'y1'
            }, {
               type: 'StackingBar', name: 'Wastage',
                dataSource: chartData, 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/24.2.3/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 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

You can use the stackingGroup property to group the stacked bar and 100% stacked bar. Columns with same group name are stacked on top of each other.

var chartData = [
    { 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: chartData, xName: 'x', yName: 'y'
            }, {
                type: 'StackingBar', name: 'Andrew', stackingGroup: 'JohnAndAndrew',
                dataSource: chartData, xName: 'x', yName: 'y1'
            }, {
               type: 'StackingBar', name: 'Thomas', stackingGroup: 'ThomasAndMichael',
                dataSource: chartData, xName: 'x', yName: 'y2'
            }, {
               type: 'StackingBar', name: 'Michael', stackingGroup: 'ThomasAndMichael',
                dataSource: chartData, 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/24.2.3/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 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.

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/24.2.3/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 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 stacked bar 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',
        border: { width: 2, color: 'red' },
      },
      {
        type: 'StackingBar',
        name: 'Orange',
        dataSource: chartData,
        xName: 'x',
        yName: 'y1',
        border: { width: 2, color: 'grey' },
      },
      {
        type: 'StackingBar',
        name: 'Wastage',
        dataSource: chartData,
        xName: 'x',
        yName: 'y2',
        border: { width: 2, color: 'lime' },
      },
    ],
    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/24.2.3/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 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>

See also