Stack bar Chart in EJ2 TypeScript control
20 Sep 202424 minutes to read
Stacked bar
To render a stacked bar series in your chart, you need to follow a few steps to configure it correctly. Here’s a concise guide on how to do this:
-
Set the series type: Define the series
type
asStackingBar
in 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. -
Inject the StackingBarSeries module: Use the
Chart.Inject(StackingBarSeries)
method to inject theStackingBarSeries
module into your chart. This step is essential, as it ensures that the necessary functionalities for rendering stacked bar series are available in your chart.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
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.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Series customization
The following properties can be used to customize the stacked bar
series.
Fill
The fill property determines the color applied to the series.
<!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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
The fill property can be used to apply a gradient color to the stacked bar series. By configuring this property with gradient values, you can create a visually appealing effect in which the color transitions smoothly from one shade to another.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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',
fill: 'url(#gradient1)'
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: stackedData,
xName: 'x',
yName: 'y1',
fill: 'url(#gradient2)'
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: stackedData,
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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<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>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Opacity
The opacity property specifies the transparency level of the fill. Adjusting this property allows you to control how opaque or transparent the fill color of the series appears.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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',
opacity: 0.7
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: stackedData,
xName: 'x',
yName: 'y1',
opacity: 0.7
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: stackedData,
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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Dash array
The dashArray property determines the pattern of dashes and gaps in the series.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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',
dashArray: '5,5',
border: { width: 2, color: '#ff4251' }
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: stackedData,
xName: 'x',
yName: 'y1',
dashArray: '5,5',
border: { width: 2, color: '#4C4C4C' }
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: stackedData,
xName: 'x',
yName: 'y2',
dashArray: '5,5',
border: { width: 2, color: '#794F1B' }
}
],
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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Border
Use the border property to customize the width and color of the series border.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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' }
},
{
type: 'StackingBar',
name: 'Orange',
dataSource: stackedData,
xName: 'x',
yName: 'y1',
border: { width: 2, color: '#66BDB7' }
},
{
type: 'StackingBar',
name: 'Wastage',
dataSource: stackedData,
xName: 'x',
yName: 'y2',
border: { width: 2, color: '#794F1B' }
}
],
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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Stacking group
Use the stackingGroup
property to group stacked bars and 100% stacked bars. Bars with the same group name are stacked on top of each other.
import { Chart, StackingBarSeries } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries);
let chart: Chart = new 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
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.
import { Chart, StackingBarSeries } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries);
let chart: Chart = new Chart({
series: [
{
//Series type as stacked bar with cylinder shape
type: 'StackingBar', columnFacet: 'Cylinder',
dataSource: stackedData, xName: 'x', yName: 'y'
},
{
type: 'StackingBar', columnFacet: 'Cylinder',
dataSource: stackedData, xName: 'x', yName: 'y1'
},
{
type: 'StackingBar', columnFacet: 'Cylinder',
dataSource: stackedData, 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Empty points
Data points with null
or undefined
values are considered empty. Empty data points are ignored and not plotted on the chart.
Mode
Use the mode
property to define how empty or missing data points are handled in the series. The default mode for empty points is Gap
.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Fill
Use the fill
property to customize the fill color of empty points in the series.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Border
Use the border
property to customize the width and color of the border for empty points.
import { Chart, StackingBarSeries, Category, Legend } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Events
Series render
The seriesRender
event allows you to customize series properties, such as data, fill, and name, before they are rendered on the chart.
import { Chart, StackingBarSeries, Category, Legend, ISeriesRenderEventArgs } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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: (args: ISeriesRenderEventArgs) => {
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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];
Point render
The pointRender
event allows you to customize each data point before it is rendered on the chart.
import { Chart, StackingBarSeries, Category, Legend, IPointRenderEventArgs } from '@syncfusion/ej2-charts';
import { stackedData } from './datasource.ts';
Chart.Inject(StackingBarSeries, Category, Legend);
let chart: Chart = new 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: (args: IPointRenderEventArgs) => {
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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>
export let stackedData: Object[] = [
{ 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 }
];