The Telerik UI Chart component for ASP.NET Core allows to display a message when there is no data to show. This feature is particularly useful when loading data asynchronously, as it reassures users that data may appear after a delay. Customizing the No Data Template is simple, enabling to add styling or interactive elements like buttons to improve usability. The No Data Template can be used for all chart types. Here’s how to set up a custom message for scenarios where the chart data is unavailable.
@(Html.Kendo().Chart<Kendo.Mvc.Examples.Models.ElectricityProduction>().Name("chart").Title("Spain electricity production (GWh)").NoData(n => n.Template("<div class=\"empty-template\">\r\n<p>There is no data to display.</p>\r\n<button id=\"button\" type=\"button\">Load Data</button>\r\n</div>")).Legend(legend => legend
.Position(ChartLegendPosition.Top)).Series(series =>{
series.Column(model => model.Nuclear).Name("Nuclear").CategoryField("Year");
series.Column(model => model.Hydro).Name("Hydro").CategoryField("Year");
series.Column(model => model.Wind).Name("Wind").CategoryField("Year");}).CategoryAxis(axis => axis
.Labels(labels => labels.Rotation(-90)).MajorGridLines(lines => lines.Visible(false))).ValueAxis(axis => axis.Numeric().Labels(labels => labels.Format("{0:N0}")).MajorUnit(10000).Line(line => line.Visible(false))))
Razor
@addTagHelper*,Kendo.Mvc<divclass="demo-section wide"><kendo-chartname="chart"><no-datatemplate="<div class='empty-template'>\r\n<p>There is no data to display.</p>\r\n<button id='button' type='button'>Load Data</ button>\r\n</div>"></no-data><category-axis><category-axis-item><labels><chart-category-axis-labels-rotationangle="-90"/></labels><major-grid-linesvisible="false"/></category-axis-item></category-axis><series><series-itemtype="ChartSeriesType.Column"category-field="Year"field="Nuclear"name="Nuclear"></series-item><series-itemtype="ChartSeriesType.Column"category-field="Year"field="Hydro"name="Hydro"></series-item><series-itemtype="ChartSeriesType.Column"category-field="Year"field="Wind"name="Wind"></series-item></series><value-axis><value-axis-itemmajor-unit="10000"type="numeric"><labelsformat="{0:N0}"></labels><linevisible="false"/></value-axis-item></value-axis><chart-legendposition="ChartLegendPosition.Top"></chart-legend><chart-titletext="Spain electricity production (GWh)"></chart-title><tooltipformat="{0:N0}"visible="true"></tooltip></kendo-chart></div>
@(Html.Kendo().Chart(Model).Name("chart").Title("Break-up of Spain Electricity Production for 2008").NoData(n => n.Template("<div class=\"empty-template\">\r\n<p>There is no data to display.</p>\r\n<button id=\"button\" type=\"button\">Load Data</button>\r\n</div>")).Legend(legend => legend
.Position(ChartLegendPosition.Bottom)).SeriesColors(newstring[]{"#03a9f4","#ff9800","#fad84a","#4caf50"}).Series(series =>{
series.Pie(model => model.Percentage, model => model.Source).ExplodeField("Explode");}).Tooltip(tooltip => tooltip.Template("${ category } - ${ value }%").Visible(true)))
Razor
@addTagHelper*,Kendo.Mvc@modelIEnumerable<Kendo.Mvc.Examples.Models.ElectricitySource>@{var seriesColors =newstring[]{"#03a9f4","#ff9800","#fad84a","#4caf50"};}<divclass="demo-section wide"><kendo-chartname="chart"series-colors="seriesColors"><chart-titletext="Break-up of Spain Electricity Production for 2008"></chart-title><no-datatemplate="<div class='empty-template'>\r\n<p>There is no data to display.</p>\r\n<button id='button' type='button'>Load Data</button>\r\n</div>"></no-data><chart-legendposition="ChartLegendPosition.Bottom"></chart-legend><series-defaultstype="ChartSeriesType.Pie"></series-defaults><series><series-itemcategory-field="Source"field="Percentage"explode-field="Explode"data="@Model.ToArray()"></series-item></series><tooltipvisible="true"template="${ category } - ${ value }%"></tooltip></kendo-chart></div>
The Telerik Template component can be integrated to further enhance the No Data Template. For example, the Html.Kendo().Template() approach can be used to add more complex UI elements, such as buttons or forms, directly within the template. Here is an example:
@addTagHelper*,Kendo.Mvc<kendo-chartname="chart"><no-data><no-data-template><divclass='empty-template'><p>There is no data to display.</p><buttonid='button'type='button'>Load Data</button></div></no-data-template></no-data><category-axis><category-axis-item><labels><chart-category-axis-labels-rotationangle="-90"/></labels><major-grid-linesvisible="false"/></category-axis-item></category-axis><series><series-itemtype="ChartSeriesType.Column"category-field="Year"field="Nuclear"name="Nuclear"></series-item><series-itemtype="ChartSeriesType.Column"category-field="Year"field="Hydro"name="Hydro"></series-item><series-itemtype="ChartSeriesType.Column"category-field="Year"field="Wind"name="Wind"></series-item></series><value-axis><value-axis-itemmajor-unit="10000"type="numeric"><labelsformat="{0:N0}"></labels><linevisible="false"/></value-axis-item></value-axis><chart-legendposition="ChartLegendPosition.Top"></chart-legend><chart-titletext="Spain electricity production (GWh)"></chart-title><tooltipformat="{0:N0}"visible="true"></tooltip></kendo-chart>