|
| 1 | +--- |
| 2 | +title: How to Show Empty Chart Instead of the Default No Data Template |
| 3 | +description: Learn how to show an empty Chart component when there is no data, instead of displaying the default No Data template. |
| 4 | +type: how-to |
| 5 | +page_title: How to Show Empty Chart Instead of the Default No Data Template |
| 6 | +slug: chart-kb-display-empty-chart |
| 7 | +tags: charts, blazor, no data template, empty chart |
| 8 | +res_type: kb |
| 9 | +ticketid: 1675313 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Chart for Blazor</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +As of version 7.1.0 of Telerik UI for Blazor, the [No Data Template](slug://chart-no-data-template) was introduced for Charts in Blazor. In some scenarios, displaying an empty Chart, rather than the No Data template, is preferred when there is no data. |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +To display an empty Chart when there is no data, [override the default theme styles](slug://themes-override) with custom CSS. The following example demonstrates how to achieve an empty Chart display by hiding the No Data Template overlay through CSS. |
| 29 | + |
| 30 | +````RAZOR |
| 31 | +<TelerikButton OnClick="@UpdateData">@ButtonContent</TelerikButton> |
| 32 | +<br /> |
| 33 | +<TelerikChart @ref="ChartRef" Width="800px" Height="400px"> |
| 34 | + <ChartSeriesItems> |
| 35 | + <ChartSeries Type="ChartSeriesType.Column" |
| 36 | + Data="@ChartData" |
| 37 | + Name="Product Sales" |
| 38 | + Field="@nameof(ChartSeriesData.ProductSales)" |
| 39 | + CategoryField="@nameof(ChartSeriesData.Year)"> |
| 40 | + </ChartSeries> |
| 41 | + </ChartSeriesItems> |
| 42 | +</TelerikChart> |
| 43 | +
|
| 44 | +<style> |
| 45 | + .k-chart-overlay { |
| 46 | + display: none; |
| 47 | + } |
| 48 | +</style> |
| 49 | +
|
| 50 | +@code { |
| 51 | + private const string Add = "Add Data"; |
| 52 | + private const string Remove = "Remove Data"; |
| 53 | +
|
| 54 | + private TelerikChart ChartRef { get; set; } |
| 55 | + private List<ChartSeriesData> ChartData { get; set; } = new (); |
| 56 | + private string ButtonContent { get; set; } = Add; |
| 57 | +
|
| 58 | + private void UpdateData() |
| 59 | + { |
| 60 | + if (ChartData == null || ChartData?.Count() == 0) |
| 61 | + { |
| 62 | + ChartData = ChartSeriesData.GenerateData(); |
| 63 | + ButtonContent = Remove; |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + // Clear the data |
| 68 | + ChartData = new List<ChartSeriesData>(); |
| 69 | + ButtonContent = Add; |
| 70 | + } |
| 71 | + ChartRef.Refresh(); // Refresh the Chart |
| 72 | + } |
| 73 | +
|
| 74 | + public class ChartSeriesData |
| 75 | + { |
| 76 | + public int ProductSales { get; set; } |
| 77 | + public int Year { get; set; } |
| 78 | +
|
| 79 | + public static List<ChartSeriesData> GenerateData() |
| 80 | + { |
| 81 | + List<ChartSeriesData> data = new List<ChartSeriesData> |
| 82 | + { |
| 83 | + new ChartSeriesData { ProductSales = 120, Year = 2020 }, |
| 84 | + new ChartSeriesData { ProductSales = 180, Year = 2021 }, |
| 85 | + new ChartSeriesData { ProductSales = 150, Year = 2022 }, |
| 86 | + new ChartSeriesData { ProductSales = 210, Year = 2023 }, |
| 87 | + new ChartSeriesData { ProductSales = 90, Year = 2024 } |
| 88 | + }; |
| 89 | +
|
| 90 | + return data; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +```` |
| 95 | + |
| 96 | +## See Also |
| 97 | + |
| 98 | +- [Blazor Charts Overview](slug://components/chart/overview) |
| 99 | +- [Override Theme Styles](slug://themes-override) |
0 commit comments