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;
}
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.
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 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;
}
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;
}
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.
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;
}