Data Labels in ASP.NET MVC Chart

19 Dec 202224 minutes to read

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.

@Html.EJS().Chart("container").Series(series =>
{
series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
Marker(mr=> mr.Visible(true).DataLabel(dl=>dl.Visible(true))).
XName("x").
YName("y").
DataSource(ViewBag.dataSource). Name("Gold").Width(2).Add();
}).PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).Title("Olympic Medal Counts - RIO").Render()
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).

@Html.EJS().Chart("container").Series(series =>
                     {
                     series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
                     Marker(mr=> mr.Visible(false).DataLabel(dl=>dl.Visible(true).Position(Syncfusion.EJ2.Charts.LabelPosition.Middle))).
                     XName("x").
                     YName("y").
                     DataSource(ViewBag.dataSource).
                     Name("Gold").
                     Width(2).Add();
                     }).PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).Title("Olympic Medal Counts - RIO").Render()
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;
        }

NOTE

The position Outer is 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.

<div id="ControlRegion">
                  @Html.EJS().Chart("container").Series(series =>
                     {
                     series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
                     Marker(mr=> mr.Visible(false).DataLabel(dl=>dl.Visible(true).Position(Syncfusion.EJ2.Charts.LabelPosition.Middle).Template("#template"))).
                     XName("x").
                     YName("y").
                     DataSource(ViewBag.dataSource).
                     Name("Gold").
                     Width(2).Add();
                     }).PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).Title("Olympic Medal Counts - RIO").Render()
<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.

@Html.EJS().Chart("container").Series(series =>
   {
    @Html.EJS().Chart("container").Series(series =>
                     {
                     series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
                     Marker(mr=> mr.Visible(false).DataLabel(dl=>dl.Visible(true).Name("text"))).
                     XName("x").
                     YName("y").
                     DataSource(ViewBag.dataSource).
                     Name("Gold").
                     Width(2).Add();
                     }).PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).Title("Olympic Medal Counts - RIO").Render()
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’.

@Html.EJS().Chart("container").Series(series =>
   {
    @Html.EJS().Chart("container").Series(series =>
                     {
                     series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
                     Marker(mr=> mr.Visible(false).DataLabel(dl=>dl.Visible(true).Format("n2"))).
                     XName("x").
                     YName("y").
                     DataSource(ViewBag.dataSource).
                     Name("Gold").
                     Width(2).Add();
                     }).PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).Title("Olympic Medal Counts - RIO").Render()
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

Margin for data label can be applied to using Left, Right, Bottom and Top properties.

@{
    var border = new { width = 2, color = "red" };
    var margin = new { left = 15, right = 15, bottom = 15, top = 15 };
 }

@Html.EJS().Chart("container").Series(series =>
                     {
                     series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
                     Marker(mr=> mr.Visible(false).DataLabel(dl=>dl.Visible(true).Border(border).Margin(margin))).
                     XName("x").
                     YName("y").
                     DataSource(ViewBag.dataSource).
                     Name("Gold").
                     Width(2).Add();
                     }).PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).Title("Olympic Medal Counts - RIO").Render()
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

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" };
    var margin = new { left = 15, right = 15, bottom = 15, top = 15 };
 }
 @Html.EJS().Chart("container").Series(series =>
                     {
                     series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
                     Marker(mr=> mr.Visible(false).DataLabel(dl=>dl.Visible(true).Border(border).Name("text").Rx(10).Ry(10).Fill("skyblue"))).
                     XName("x").
                     YName("y").
                     DataSource(ViewBag.dataSource).
                     Name("Gold").
                     Width(2).Add();
                     }).PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).Title("Olympic Medal Counts - RIO").Render()
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;
        }

NOTE

Rx and Ry properties requires Border values not to be null.

Customizing Specific Point

You can also customize the specific marker and label using PointRender and TextRender event. PointRender event allows you to change the shape, color and border for a point, whereas the TextRender event allows you to change the text for the point.

@Html.EJS().Chart("container").TextRender("textRender").PointRender("pointRender").Series(series =>
                     {
                     series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column).
                     Marker(mr=> mr.Visible(false).DataLabel(dl=>dl.Visible(true))).
                     XName("x").
                     YName("y").
                     DataSource(ViewBag.dataSource).
                     Name("Gold").
                     Width(2).Add();
                     }).PrimaryXAxis(px => px.Interval(1).ValueType(Syncfusion.EJ2.Charts.ValueType.Category)).Title("Olympic Medal Counts - RIO").Render()
<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 style="text-align:center">
        @Html.EJS().Chart("container").TextRender("onTextRender").SeriesRender("onSeriesRender").Series(series =>
   {

       series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column)
       .Marker(mr => mr.DataLabel(dl => dl.Visible(true).Font(ff => ff.FontWeight("600").Color("#ffffff")).Position(Syncfusion.EJ2.Charts.LabelPosition.Top))).XName("x")
       .YName("yValue").DataSource(ViewBag.dataSource1).Name("Gold").Width(2).Add();
       series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column)
        .Marker(mr => mr.DataLabel(dl => dl.Visible(true).Font(ff => ff.FontWeight("600").Color("#ffffff")).Position(Syncfusion.EJ2.Charts.LabelPosition.Top))).XName("x").YName("yValue1").DataSource(ViewBag.dataSource2)
       .Name("Silver").Width(2).Add();
       series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Column)
        .Marker(mr => mr.DataLabel(dl => dl.Visible(true).Font(ff => ff.FontWeight("600").Color("#ffffff")).Position(Syncfusion.EJ2.Charts.LabelPosition.Top))).XName("x")
        .YName("yValue2").DataSource(ViewBag.dataSource3).Name("Brozen").Width(2).Add();

   }).PrimaryYAxis(px => px.LabelStyle(ls => ls.Color("transparent")).LineStyle(ls => ls.Width(0)).MajorTickLines(mg => mg.Width(0))
   .MajorGridLines(mg => mg.Width(0))
   ).PrimaryXAxis(px => px.Interval(1)
   .ValueType(Syncfusion.EJ2.Charts.ValueType.Category).MajorGridLines(mg => mg.Width(0))
   ).Tooltip(tt => tt.Enable(true)).ChartArea(area => area.Border(br => br.Color("transparent"))
   ).LegendSettings(lg => lg.Visible(false)).Title("Olympic Medal Counts - RIO").Render()
    </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;
        }