Data Labels in ASP.NET Core Charts
Data label can be added to a chart series by enabling the visible option in the dataLabel. By default, the labels will arrange smartly without overlapping.
<ejs-chart id="container" width="60%">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" xName="x" yName="y"
type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true"></e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>public ActionResult Index()
{
List<Data> chartData = new List<Data>
{
new Data{ x= "Jan", y= 3, fill= "#498fff", text= "January" },
new Data{ x= "Feb", y= 3.5, fill= "#ffa060", text= "February" },
new Data{ x= "Mar", y= 7, fill= "#ff68b6", text= "March" },
new Data{ x= "Apr", y= 13.5,fill= "#81e2a1", text= "April" }
};
ViewBag.dataSource = chartData;
return View();
}
public class Data
{
public string x;
public double y;
public string fill;
public string text;
}Position
Using position property, you can place the label either on Top, Middle, Bottom or Outer (outer is applicable for column and bar type series).
<ejs-chart id="container" width="60%">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" xName="x" yName="y"
type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" position="Middle"></e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>public ActionResult Index()
{
List<Data> chartData = new List<Data>
{
new Data{ x= "Jan", y= 3, fill= "#498fff", text= "January" },
new Data{ x= "Feb", y= 3.5, fill= "#ffa060", text= "February" },
new Data{ x= "Mar", y= 7, fill= "#ff68b6", text= "March" },
new Data{ x= "Apr", y= 13.5,fill= "#81e2a1", text= "April" }
};
ViewBag.dataSource = chartData;
return View();
}
public class Data
{
public string x;
public double y;
public string fill;
public string text;
}The position
Outeris applicable for column and bar type series.
Data Label Template
Label content can be formatted by using the template option. Inside the template, you can add the placeholder text ${point.x} and ${point.y} to display corresponding data points x & y value. Using template property, you can set data label template in chart.
<ejs-chart id="container" width="60%">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" xName="x" yName="y"
type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" position="Middle"
template="#template"></e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>
<script id="template">
<div style="background:#f5f5f5; border: 1px solid black; padding: 3px 3px 3px 3px">
<div>${point.x}</div>
<div>${point.y}</div>
</div>
</script>public ActionResult Index()
{
List<Data> chartData = new List<Data>
{
new Data{ x= "Jan", y= 3, fill= "#498fff", text= "January" },
new Data{ x= "Feb", y= 3.5, fill= "#ffa060", text= "February" },
new Data{ x= "Mar", y= 7, fill= "#ff68b6", text= "March" },
new Data{ x= "Apr", y= 13.5,fill= "#81e2a1", text= "April" }
};
ViewBag.dataSource = chartData;
return View();
}
public class Data
{
public string x;
public double y;
public string fill;
public string text;
}Text Mapping
Text from the data source can be mapped using name property.
<ejs-chart id="container" width="60%">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" xName="x" yName="y"
type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" name="text"></e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>public ActionResult Index()
{
List<Data> chartData = new List<Data>
{
new Data{ x= "Jan", y= 3, fill= "#498fff", text= "January" },
new Data{ x= "Feb", y= 3.5, fill= "#ffa060", text= "February" },
new Data{ x= "Mar", y= 7, fill= "#ff68b6", text= "March" },
new Data{ x= "Apr", y= 13.5,fill= "#81e2a1", text= "April" }
};
ViewBag.dataSource = chartData;
return View();
}
public class Data
{
public string x;
public double y;
public string fill;
public string text;
}Format
Data label for the chart can be formatted using format property. You can use the global formatting options, such as ‘n’, ‘p’, and ‘c’.
<ejs-chart id="container" width="60%">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" xName="x" yName="y"
type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" name="text" format= "n2"></e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>public ActionResult Index()
{
List<Data> chartData = new List<Data>
{
new Data{ x= "Jan", y= 3, fill= "#498fff", text= "January" },
new Data{ x= "Feb", y= 3.5, fill= "#ffa060", text= "February" },
new Data{ x= "Mar", y= 7, fill= "#ff68b6", text= "March" },
new Data{ x= "Apr", y= 13.5,fill= "#81e2a1", text= "April" }
};
ViewBag.dataSource = chartData;
return View();
}
public class Data
{
public string x;
public double y;
public string fill;
public string text;
}| Value | Format | Resultant Value | Description |
|---|---|---|---|
| 1000 | n1 | 1000.0 | The number is rounded to 1 decimal place. |
| 1000 | n2 | 1000.00 | The number is rounded to 2 decimal places. |
| 1000 | n3 | 1000.000 | The number is rounded to 3 decimal place. |
| 0.01 | p1 | 1.0% | The number is converted to percentage with 1 decimal place. |
| 0.01 | p2 | 1.00% | The number is converted to percentage with 2 decimal place. |
| 0.01 | p3 | 1.000% | The number is converted to percentage with 3 decimal place. |
| 1000 | c1 | $1000.0 | The currency symbol is appended to number and number is rounded to 1 decimal place. |
| 1000 | c2 | $1000.00 | The currency symbol is appended to number and number is rounded to 2 decimal place. |
Margin
The margin for data label can be applied to use left, right, bottom and top properties.
@{
var border = new { width = 2, color = "red" };
var margin = new { left = 15, right = 15, bottom = 15, top = 15 };
}
<ejs-chart id="container" width="60%">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" xName="x" yName="y"
type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" name="text" border="border" margin="margin"></e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>public ActionResult Index()
{
List<Data> chartData = new List<Data>
{
new Data{ x= "Jan", y= 3, fill= "#498fff", text= "January" },
new Data{ x= "Feb", y= 3.5, fill= "#ffa060", text= "February" },
new Data{ x= "Mar", y= 7, fill= "#ff68b6", text= "March" },
new Data{ x= "Apr", y= 13.5,fill= "#81e2a1", text= "April" }
};
ViewBag.dataSource = chartData;
return View();
}
public class Data
{
public string x;
public double y;
public string fill;
public string text;
}Customization
The stroke and border of data label can be customized using fill and border properties. Rounded corners can be customized using rx and ry properties.
@{
var border = new { width = 2, color = "red" };
}
<ejs-chart id="container" width="60%">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" xName="x" yName="y"
type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" name="text" border="border" rx="10" ry="10"></e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>public ActionResult Index()
{
List<Data> chartData = new List<Data>
{
new Data{ x= "Jan", y= 3, fill= "#498fff", text= "January" },
new Data{ x= "Feb", y= 3.5, fill= "#ffa060", text= "February" },
new Data{ x= "Mar", y= 7, fill= "#ff68b6", text= "March" },
new Data{ x= "Apr", y= 13.5,fill= "#81e2a1", text= "April" }
};
ViewBag.dataSource = chartData;
return View();
}
public class Data
{
public string x;
public double y;
public string fill;
public string text;
}
rxandryproperties requiresbordervalues not to be null.
Handling Overlapping Data Labels
Use the labelIntersectAction property to specify how overlapping data labels are handled.
The available options are:
-
None- Displays all data labels even when they overlap. -
Hide- Hides overlapping data labels. -
Rotate90- Rotates overlapping data labels by 90 degrees. -
RelocateVertically- Relocates overlapping data labels vertically. -
RelocateHorizontally- Relocates overlapping data labels horizontally.
The RelocateVertically and RelocateHorizontally options are supported only for the following series types:
StackingColumnStackingColumn100StackingBarStackingBar100
These relocation modes enable the Smart Data Label feature, which improves readability by relocating overlapping data labels and connecting them to their corresponding data points using connector lines. Use the smartLabelSettings property to customize relocated labels through the background, pointerShape, border, connectorLineStyle, and offset settings. The offset property is applicable only when labelIntersectAction is set to RelocateVertically.
@{
var border = new
{
color = "black",
dashArray = "2",
width = 2
};
var connectorLineStyle = new
{
color = "black",
width = 2
};
var smartLabelSettings = new
{
background = "#4f4f4f",
border = border,
connectorLineStyle = connectorLineStyle,
pointerShape = "Arrow",
offset = 0.5
};
}
<ejs-chart id="container" title="Chart Title">
<e-chart-tooltipsettings enable="true">
</e-chart-tooltipsettings>
<e-chart-legendsettings visible="true">
</e-chart-legendsettings>
<e-chart-primaryxaxis valueType="@Syncfusion.EJ2.Charts.ValueType.Category">
</e-chart-primaryxaxis>
<e-chart-primaryyaxis visible="false">
</e-chart-primaryyaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataA" xName="x" yName="y" name="Category A" type="@Syncfusion.EJ2.Charts.ChartSeriesType.StackingColumn" fill="#254061" columnWidth="0.6">
<e-series-marker>
<e-series-datalabel visible="true" name="text" fill="Transparent" labelIntersectAction="@Syncfusion.EJ2.Charts.DataLabelIntersectAction.RelocateVertically" smartLabelSettings="smartLabelSettings">
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataB" xName="x" yName="y" name="Category B" type="@Syncfusion.EJ2.Charts.ChartSeriesType.StackingColumn" fill="#376092" columnWidth="0.6">
<e-series-marker>
<e-series-datalabel visible="true" name="text" fill="Transparent" labelIntersectAction="@Syncfusion.EJ2.Charts.DataLabelIntersectAction.RelocateVertically" smartLabelSettings="smartLabelSettings">
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataC" xName="x" yName="y" name="Category C" type="@Syncfusion.EJ2.Charts.ChartSeriesType.StackingColumn" fill="#4f81bd" columnWidth="0.6">
<e-series-marker>
<e-series-datalabel visible="true" name="text" fill="Transparent" labelIntersectAction="@Syncfusion.EJ2.Charts.DataLabelIntersectAction.RelocateVertically" smartLabelSettings="smartLabelSettings">
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataD" xName="x" yName="y" name="Category D" type="@Syncfusion.EJ2.Charts.ChartSeriesType.StackingColumn" fill="CornflowerBlue" columnWidth="0.6">
<e-series-marker>
<e-series-datalabel visible="true" name="text" fill="Transparent" labelIntersectAction="@Syncfusion.EJ2.Charts.DataLabelIntersectAction.RelocateVertically" smartLabelSettings="smartLabelSettings">
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataE" xName="x" yName="y" name="Category E" type="@Syncfusion.EJ2.Charts.ChartSeriesType.StackingColumn" fill="LightBlue" columnWidth="0.6">
<e-series-marker>
<e-series-datalabel visible="true" name="text" fill="Transparent" labelIntersectAction="@Syncfusion.EJ2.Charts.DataLabelIntersectAction.RelocateVertically" smartLabelSettings="smartLabelSettings">
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataF" xName="x" yName="y" name="Category F" type="@Syncfusion.EJ2.Charts.ChartSeriesType.StackingColumn" fill="LightSteelBlue" columnWidth="0.6">
<e-series-marker>
<e-series-datalabel visible="true" name="text" fill="Transparent" labelIntersectAction="@Syncfusion.EJ2.Charts.DataLabelIntersectAction.RelocateVertically" smartLabelSettings="smartLabelSettings">
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataG" xName="x" yName="y" name="Category G" type="@Syncfusion.EJ2.Charts.ChartSeriesType.StackingColumn" fill="Moccasin" columnWidth="0.6">
<e-series-marker>
<e-series-datalabel visible="true" name="text" fill="Transparent" labelIntersectAction="@Syncfusion.EJ2.Charts.DataLabelIntersectAction.RelocateVertically" smartLabelSettings="smartLabelSettings">
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataH" xName="x" yName="y" name="Category H" type="@Syncfusion.EJ2.Charts.ChartSeriesType.StackingColumn" fill="LightGrey" columnWidth="0.6">
<e-series-marker>
<e-series-datalabel visible="true" name="text" fill="Transparent" labelIntersectAction="@Syncfusion.EJ2.Charts.DataLabelIntersectAction.RelocateVertically" smartLabelSettings="smartLabelSettings">
</e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
public class HomeController : Controller
{
public ActionResult Index()
{
List<Data> dataA = new List<Data>
{
new Data { x = "Period 1", y = 35, text = "35%" },
new Data { x = "Period 2", y = 30, text = "30%" },
new Data { x = "Period 3", y = 32, text = "32%" }
};
List<Data> dataB = new List<Data>
{
new Data { x = "Period 1", y = 22, text = "22%" },
new Data { x = "Period 2", y = 25, text = "25%" },
new Data { x = "Period 3", y = 24, text = "24%" }
};
List<Data> dataC = new List<Data>
{
new Data { x = "Period 1", y = 14, text = "14%" },
new Data { x = "Period 2", y = 16, text = "16%" },
new Data { x = "Period 3", y = 15, text = "15%" }
};
List<Data> dataD = new List<Data>
{
new Data { x = "Period 1", y = 10, text = "10%" },
new Data { x = "Period 2", y = 11, text = "11%" },
new Data { x = "Period 3", y = 12, text = "12%" }
};
List<Data> dataE = new List<Data>
{
new Data { x = "Period 1", y = 7, text = "7%" },
new Data { x = "Period 2", y = 8, text = "8%" },
new Data { x = "Period 3", y = 6, text = "6%" }
};
List<Data> dataF = new List<Data>
{
new Data { x = "Period 1", y = 5, text = "5%" },
new Data { x = "Period 2", y = 5, text = "5%" },
new Data { x = "Period 3", y = 5, text = "5%" }
};
List<Data> dataG = new List<Data>
{
new Data { x = "Period 1", y = 4, text = "4%" },
new Data { x = "Period 2", y = 3, text = "3%" },
new Data { x = "Period 3", y = 4, text = "4%" }
};
List<Data> dataH = new List<Data>
{
new Data { x = "Period 1", y = 3, text = "3%" },
new Data { x = "Period 2", y = 2, text = "2%" },
new Data { x = "Period 3", y = 2, text = "2%" }
};
ViewBag.dataA = dataA;
ViewBag.dataB = dataB;
ViewBag.dataC = dataC;
ViewBag.dataD = dataD;
ViewBag.dataE = dataE;
ViewBag.dataF = dataF;
ViewBag.dataG = dataG;
ViewBag.dataH = dataH;
return View();
}
public class Data
{
public string x;
public double y;
public string text;
}
}Customizing Specific Point
You can also customize the specific marker and label using pointRender and textRender event. The pointRender event allows to change the shape, color and border for a point, whereas the textRender event allows to change the text for the point.
<ejs-chart id="container" width="60%" pointRender="pointRender" textRender="textRender">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" xName="x" yName="y"
type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true"></e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>
<script>
pointRender=function(args) {
if (args.point.index === 2) {
args.fill = 'red'
}
},
textRender = function (args) {
if (args.point.index === 2) {
args.text = 'Maximum Temperature';
args.color = 'red';
}
else {
args.cancel = true;
}
}
</script>public ActionResult Index()
{
List<Data> chartData = new List<Data>
{
new Data{ x= "Jan", y= 3, fill= "#498fff", text= "January" },
new Data{ x= "Feb", y= 3.5, fill= "#ffa060", text= "February" },
new Data{ x= "Mar", y= 7, fill= "#ff68b6", text= "March" },
new Data{ x= "Apr", y= 13.5,fill= "#81e2a1", text= "April" }
};
ViewBag.dataSource = chartData;
return View();
}
public class Data
{
public string x;
public double y;
public string fill;
public string text;
}Show percentage based on each series points
You can calculate the percentage value based on the sum for each series using the seriesRender and textRender events in the chart. In seriesRender calculate the sum of each series y values and In textRender calculate percentage value based on the sum value and modify the text.
<div class="control-section">
<div id="container" align='center'>
<ejs-chart id="lineContainer" textRender="onTextRender" seriesRender="onSeriesRender" title="Olympic Medal Counts - RIO">
<e-chart-tooltipsettings enable="true">
</e-chart-tooltipsettings>
<e-chart-primaryxaxis valueType="Category" interval=1>
<e-majorgridlines width="0"></e-majorgridlines>
</e-chart-primaryxaxis>
<e-chart-primaryyaxis>
<e-majorgridlines width="0"></e-majorgridlines>
<e-majorticklines width="0"></e-majorticklines>
<e-linestyle width="0"></e-linestyle>
<e-labelstyle color="transparent"></e-labelstyle>
</e-chart-primaryyaxis>
<e-chart-chartarea>
<e-chartarea-border width="0"></e-chartarea-border>
</e-chart-chartarea>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource1" xName='x' yName='y' name='Gold' width=2 type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" position="Top">
<e-font color="#ffffff" fontWeight="600"></e-font>
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataSource2" xName='x' yName='y' name='Silver' width=2 type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" position="Top">
<e-font fontWeight="600" color="#ffffff"></e-font>
</e-series-datalabel>
</e-series-marker>
</e-series>
<e-series dataSource="ViewBag.dataSource3" xName='x' yName='y' name='Bronze' width=2 type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column">
<e-series-marker>
<e-series-datalabel visible="true" position="Top">
<e-font fontWeight="600" color="#ffffff"></e-font>
</e-series-datalabel>
</e-series-marker>
</e-series>
</e-series-collection>
</ejs-chart>
</div>
</div>
<script>
var total = [];
function onSeriesRender(args) {
for (var i = 0; i < args.data.length; i++) {
if (!total[args.data[i].x]) total[args.data[i].x] = 0;
total[args.data[i].x] = total[args.data[i].x] + parseInt(args.data[i].y);
}
}
function onTextRender(args) {
var percentage = (parseInt(args.text) / total[args.point.x]) * 100;
percentage = percentage % 1 === 0 ? percentage : percentage.toFixed(2);
args.text = percentage + "%";
}
</script>public ActionResult Index()
{
List<ColumnChartData> chartData1 = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", y = 46 },
new ColumnChartData { x= "GBR", y = 27 },
new ColumnChartData { x= "CHN", y = 26 }
};
ViewBag.dataSource1 = chartData1;
List<ColumnChartData> chartData2 = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", y = 37 },
new ColumnChartData { x= "GBR", y = 23 },
new ColumnChartData { x= "CHN", y = 18 }
};
ViewBag.dataSource2 = chartData2;
List<ColumnChartData> chartData3 = new List<ColumnChartData>
{
new ColumnChartData { x= "USA", y = 38 },
new ColumnChartData { x= "GBR", y = 17 },
new ColumnChartData { x= "CHN", y = 26 }
};
ViewBag.dataSource3 = chartData3;
return View();
}
public class ColumnChartData
{
public string x;
public double y;
}