Chart can visualise data bound from local or remote data.
You can bind a simple JSON data to the chart using
dataSource
property in series. Now map the fields in
JSON to xName
and yName
properties.
var chartData: any[] = [
{ month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
{ month: 'Mar', sales: 34 }, { month: 'Apr', sales: 32 },
{ month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
{ month: 'Jul', sales: 35 }, { month: 'Aug', sales: 55 },
{ month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
{ month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category'
},
series:[{
dataSource: chartData,
xName: 'month',
yName: 'sales',
type: 'Column'
}]
}, '#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/21.2.3/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>
You can also bind remote data to the chart using DataManager. The DataManager
requires minimal information
like webservice URL, adaptor and crossDomain to interact with service endpoint properly. Assign the instance
of DataManager to the dataSource
property in series
and map the fields of data to xName
and
yName
properties. You can also use the
query
property of the series to filter the data.
var dataManager = new ej.data.DataManager({
url: 'http://mvc.syncfusion.com/Services/Northwnd.svc/Tasks/'
});
var query = new ej.data.Query().take(5).where('Estimate', 'lessThan', 3, false);
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category',
title: 'Assignee'
},
primaryYAxis:
{
title: 'Estimate',
minimum: 0, maximum: 3, interval: 1
},
series: [
{
type: 'Column',
dataSource: dataManager,
xName: 'Assignee', yName: 'Estimate', query: query,
name: 'In progress'
}
],
title: 'Sprint Task Analysis'
}, '#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/21.2.3/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>
OData
is a standardized protocol for creating and consuming data. You can retrieve data from OData service using the DataManager. Refer to the following code example for remote Data binding using OData service.
var query = new ej.data.Query()
var data = new ej.data.DataManager({
url: 'https://js.syncfusion.com/ejServices/Wcf/Northwind.svc/Orders/?$top=7',
adaptor: new ej.data.ODataAdaptor()
});
var chart = new ej.charts.Chart({
//Initializing Primary X Axis
primaryXAxis: {
valueType: 'Category',
},
//Initializing Chart Sample
series: [
{
type: 'Column',
dataSource: data,
xName: 'CustomerID', yName: 'Freight', query: query,
}
],
});
<!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/21.2.3/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>
Lazy loading allows you to load data for chart on demand. Chart will fire the scrollEnd event, in that we can get the minimum and maximum range of the axis, based on this, we can upload the data to chart.
var intl = new Internationalization();
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Day',
valueType: 'DateTime',
edgeLabelPlacement: 'Shift',
skeleton: 'yMMM',
skeletonType: 'Date',
scrollbarSettings: {
range: {
minimum: new Date(2009, 0, 1),
maximum: new Date(2014, 0, 1)
},
enable: true,
}
},
primaryYAxis: {
title: 'Server Load',
labelFormat: '{value}MB'
},
series: [{
dataSource: GetDateTimeData(new Date(2009, 0, 1), new Date(2009, 8, 1)),
xName: 'x', yName: 'y',
type: 'Line', animation: { enable: false },
}],
height: '450',
title: 'Network Load',
crosshair: { enable: true, lineType: 'Vertical' },
tooltip: { enable: true, shared: true },
legendSettings: { visible: true },
scrollEnd: (args: IScrollEventArgs) => {
if (lazymode.value === 'Range') {
chart.series[0].dataSource = GetDateTimeData(args.currentRange.minimum as Date, args.currentRange.maximum as Date);
}
chart.dataBind();
},
}, '#element');
function GetDateTimeData(start, end) {
var series1 = [];
var date;
var value = 30;
var option = {
skeleton: 'full',
type: 'dateTime'
};
var dateParser = intl.getDateParser(option);
var dateFormatter = intl.getDateFormat(option);
for (var i = 0; start <= end; i++) {
date = Date.parse(dateParser(dateFormatter(start)));
if (Math.random() > .5) {
value += (Math.random() * 10 - 5);
}
else {
value -= (Math.random() * 10 - 5);
}
if (value < 0) {
value = getRandomInt(20, 40);
}
var point1 = { x: new Date(date), y: Math.round(value) };
new Date(start.setDate(start.getDate() + 1));
series1.push(point1);
}
return series1;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
<!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/21.2.3/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>
The Data points that uses the null
or undefined
as value are considered as empty points. Empty data points are ignored and not plotted in the Chart. When the data is provided by using the points property,
By using emptyPointSettings
property in series, you can customize the empty point. Default mode
of the empty point is Gap
.
var chartData = [
{ month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
{ month: 'Mar', sales: null }, { month: 'Apr', sales: 32 },
{ month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
{ month: 'Jul', sales: 35 }, { month: 'Aug', sales: undefined },
{ month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
{ month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category'
},
series:[{
dataSource: chartData,
xName: 'month',
yName: 'sales',
type: 'Column',
emptyPointSettings: {
mode: 'Gap'
}
},
{
dataSource: chartData,
xName: 'month',
yName: 'sales',
type: 'Line',
marker: { visible: true},
emptyPointSettings: {
mode: 'Average'
}
}]
}, '#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/21.2.3/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>
Customizing empty point
Specific color for empty point can be set by fill
property in emptyPointSettings
. Border for a empty point can be set by
border
property.
var chartData = [
{ month: 'Jan', sales: 35 }, { month: 'Feb', sales: 28 },
{ month: 'Mar', sales: null }, { month: 'Apr', sales: 32 },
{ month: 'May', sales: 40 }, { month: 'Jun', sales: 32 },
{ month: 'Jul', sales: 35 }, { month: 'Aug', sales: undefined },
{ month: 'Sep', sales: 38 }, { month: 'Oct', sales: 30 },
{ month: 'Nov', sales: 25 }, { month: 'Dec', sales: 32 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'Category'
},
series:[{
dataSource: chartData,
xName: 'month',
yName: 'sales',
type: 'Column',
emptyPointSettings: {
mode: 'Average',
fill: 'green',
border: { color: 'black', width: 2}
}
},
{
dataSource: chartData,
xName: 'month',
yName: 'sales',
type: 'Line',
marker: { visible: true},
emptyPointSettings: {
mode: 'Zero',
fill: 'pink'
}
}]
}, '#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/21.2.3/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>