Data Label in ASP.NET MVC Accumulation Chart Component

9 May 202224 minutes to read

Data label can be added to a chart series by enabling the Visible option in the dataLabel property.

Html.EJS().AccumulationChart("container").Series(series =>
            {
                series.DataSource(ViewBag.dataSource)
                      .XName("xValue")
                      .YName("yValue")
                      .Name("Browser")
                      .Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
                      .DataLabel(ViewBag.datalabel).Add();
            })
            .EnableSmartLabels(true)
            .Title("Mobile Browser Statistics")
            .LegendSettings(ls => ls.Visible(false)).Render()
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
            return View();
        }
         public class PieChartData
        {
            public string xValue;
            public double yValue;
            public string text;
            public string fill;
        }

Positioning

Accumulation chart provides support for placing the data label either Inside or Outside the chart.

@Html.EJS().AccumulationChart("container").Series(series =>
            {
                series.DataSource(ViewBag.dataSource)
                      .XName("xValue")
                      .YName("yValue")
                      .Name("Browser")
                      .Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
                      .DataLabel(ViewBag.datalabel).Add();
            })
            .EnableSmartLabels(true)
            .Title("Mobile Browser Statistics")
            .LegendSettings(ls => ls.Visible(false)).Render()
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class PieChartData
        {
            public string xValue;
            public double yValue;
            public string text;
            public string fill;
        }

Smart labels

Datalabels will be arranged smartly without overlapping with each other. You can enable or disable this feature using the EnableSmartLabels property.

@Html.EJS().AccumulationChart("container").Series(series =>
            {
                series.DataSource(ViewBag.dataSource)
                      .XName("xValue")
                      .YName("yValue")
                      .Name("Browser")
                      .Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
                      .DataLabel(ViewBag.datalabel).Add();
            })
            .EnableSmartLabels(true)
            .Title("Mobile Browser Statistics")
            .LegendSettings(ls => ls.Visible(false)).Render()
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
             return View();
        }
        public class PieChartData
        {
            public string xValue;
            public double yValue;
            public string text;
            public string fill;
        }

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.

@Html.EJS().AccumulationChart("container").Series(series =>
            {
                series.DataSource(ViewBag.dataSource)
                      .XName("xValue")
                      .YName("yValue")
                      .Name("Browser")
                      .Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
                      .DataLabel(ViewBag.datalabel).Add();
            })
            .EnableSmartLabels(true)
            .Title("Mobile Browser Statistics")
            .LegendSettings(ls => ls.Visible(false)).Render()
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class PieChartData
        {
            public string xValue;
            public double yValue;
             public string text;
            public string fill;
        }

Connector Line

Connector line will be visible when the data label is placed Outside the chart. The connector line can be customized using the Type, Color, Width, Length and DashArray properties.

@Html.EJS().AccumulationChart("container").Series(series =>
            {
                series.DataSource(ViewBag.dataSource)
                      .XName("xValue")
                      .YName("yValue")
                      .Name("Browser")
                      .Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
                      .DataLabel(ViewBag.datalabel).Add();
            })
            .EnableSmartLabels(true)
            .Title("Mobile Browser Statistics")
            .LegendSettings(ls => ls.Visible(false)).Render()
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
             return View();
        }
         public class PieChartData
        {
            public string xValue;
            public double yValue;
            public string text;
            public string fill;
        }

Text Mapping

Text from the data source can be mapped to data label using Name property.

@Html.EJS().AccumulationChart("container").Series(series =>
            {
                series.DataSource(ViewBag.dataSource)
                      .XName("xValue")
                      .YName("yValue")
                      .Name("Browser")
                      .Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
                      .DataLabel(ViewBag.datalabel).Add();
            })
            .EnableSmartLabels(true)
            .Title("Mobile Browser Statistics")
            .LegendSettings(ls => ls.Visible(false)).Render()
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class PieChartData
        {
            public string xValue;
            public double yValue;
            public string text;
            public string fill;
        }

Customization

Individual text can be customized using the TextRender event.

@Html.EJS().AccumulationChart("container").Series(series =>
            {
                series.DataSource(ViewBag.dataSource)
                      .XName("xValue")
                      .YName("yValue")
                      .Name("Browser")
                      .Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
                      .DataLabel(ViewBag.datalabel).Add();
            })
            .EnableSmartLabels(true)
            .Title("Mobile Browser Statistics")
            .TextRender("textRender")
            .LegendSettings(ls => ls.Visible(false)).Render()

<script>
            var textRender = function (args) {
               if (args.text.indexOf('iPhone') > -1) {
            args.color = 'red';
            args.border.width = 1;
        }
            };
</script>
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
            return View();
        }
        public class PieChartData
        {
            public string xValue;
            public double yValue;
            public string text;
            public string fill;
        }

Show percentages in data labels of pie chart

You can show the percentages in data labels of pie chart using textRender event and template option.

Using textRender event

You can customize the data label of pie chart using TextRender event as follows to show percentage.

Html.EJS().AccumulationChart("container").Series(series =>
            {
                series.DataSource(ViewBag.dataSource)
                      .XName("xValue")
                      .YName("yValue")
                      .Name("Browser")
                      .Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
                      .DataLabel(ViewBag.dataSource).Add();
            })
            .EnableSmartLabels(true)
            .TextRender("textRender")
            .LegendSettings(ls => ls.Visible(false)).Render()

<script>
        var textRender = function (args) {
            args.text = args.point.percentage + "%";
        };
</script>
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
            return View();
        }
         public class PieChartData
        {
            public string xValue;
            public double yValue;
            public string text;
            public string fill;
        }

Using template

You can display the percentage values in data label of pie chart using template option.

@(Html.EJS().AccumulationChart("container")
    .LegendSettings(ls => ls.Visible(false)).EnableSmartLabels(true).
	Series(series =>
	{
		series.DataLabel(dl => dl.Visible(true).Name("text").Template("<div id='dataLabelTemplate'>${point.percentage}%</div>"))
			.XName("xValue")
			.YName("yValue")
			.Name("Browser")
			.Type(Syncfusion.EJ2.Charts.AccumulationType.Pie)
			.DataSource(ViewBag.dataSource).Add();
	}).Render())
public ActionResult Index()
        {
            List<PieChartData> chartData = new List<PieChartData>
            {

                new PieChartData { xValue = "Chrome", yValue = 37, text = "37%", fill="#498fff"},
                new PieChartData { xValue = "UC Browser", yValue = 17, text = "17%", fill="#ffa060"},
                new PieChartData { xValue = "iPhone", yValue = 19, text = "19%", fill="#ff68b6"},
                new PieChartData { xValue = "Others", yValue = 4 , text = "4%", fill="#81e2a1"},
            };
            ViewBag.dataSource = chartData;
            return View();
        }
         public class PieChartData
        {
            public string xValue;
            public double yValue;
            public string text;
            public string fill;
        }

Show percentage in ASP.NET Core Accumulationchart control