How to prevent data labels for zero values in JavaScript Charts
Use the Chart textRender event to cancel a data label before it is rendered. Check the numeric point value and set args.cancel to true when the value is zero. The event runs for each data label.
Enable data labels
Set marker.dataLabel.visible to true. Data labels are not displayed unless they are enabled for the series.
marker: {
visible: true,
dataLabel: {
visible: true
}
}Cancel labels for zero values
Handle textRender at the Chart level and compare the mapped Y value with numeric zero.
textRender: function(args) {
args.cancel = args.point.y === 0;
},Complete example
var chart = new ej.charts.Chart({
//Initializing Primary X Axis
primaryXAxis: {
valueType: 'DateTime',
},
//Initializing Chart Series
series: [
{
type: 'Line',
dataSource: [
{ x: new Date(2005, 0, 1), y: 21 }, { x: new Date(2006, 0, 1), y: 24 },
{ x: new Date(2007, 0, 1), y: 0 }, { x: new Date(2008, 0, 1), y: 38 },
{ x: new Date(2009, 0, 1), y: 54 }, { x: new Date(2010, 0, 1), y: 57 },
],
xName: 'x', width: 2, marker: {
dataLabel : { visible: true },
visible: true,
width: 10,
height: 10
},
yName: 'y', name: 'Germany',
}
],
//Initializing Chart title
title: 'Inflation - Consumer Price',
textRender: function(args) {
args.cancel = args.point.y === 0;
},
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://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.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>Troubleshooting
- If zero labels remain visible, verify that
textRenderis configured on the Chart and that the Y field contains numeric zero. - If no labels appear, verify that
marker.dataLabel.visibleistrueand Data Label support is available in the loaded EJ2 ES5 bundle. - If the data value is a formatted string, convert it to a number or compare the correct source field.
- If near-zero values remain visible, use an application-appropriate tolerance instead of strict equality.
- If the entire point disappears, verify that only
args.cancelintextRenderis changed and that the series data is not filtered.