|
| 1 | +--- |
| 2 | +title: How to Display an Empty Chart Without Data |
| 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 Display an Empty Chart Without Data |
| 6 | +slug: chark-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, 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. This knowledge base article also answers the following questions: |
| 25 | + |
| 26 | +- How can I hide the No Data Template in Blazor Charts? |
| 27 | +- Is it possible to display an empty Chart in Blazor when there's no data? |
| 28 | +- Can I override the No Data Template in Blazor Charts? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +To display an empty Chart when there is no data, [override the default theme styles](slug://themes-override) by applying custom CSS. The following example demonstrates how to achieve an empty Chart display by hiding the No Data Template overlay through CSS. |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +````RAZOR |
| 37 | +<TelerikButton OnClick="@UpdateData">@ButtonContent</TelerikButton> |
| 38 | +<br /> |
| 39 | +<TelerikChart @ref="ChartRef" Width="800px" Height="400px"> |
| 40 | + <ChartTitle Text="Product Sales Over the Years" Position="@ChartTitlePosition.Bottom"></ChartTitle> |
| 41 | + <ChartSeriesItems> |
| 42 | + <ChartSeries Type="ChartSeriesType.Column" |
| 43 | + Data="@ChartData" |
| 44 | + Name="Product Sales" |
| 45 | + Field="@nameof(ChartSeriesData.ProductSales)" |
| 46 | + CategoryField="@nameof(ChartSeriesData.Year)"> |
| 47 | + </ChartSeries> |
| 48 | + </ChartSeriesItems> |
| 49 | +</TelerikChart> |
| 50 | +
|
| 51 | +<style> |
| 52 | + .k-chart-overlay { |
| 53 | + display: none; |
| 54 | + } |
| 55 | +</style> |
| 56 | +
|
| 57 | +@code { |
| 58 | + private const string Add = "Add Data"; |
| 59 | + private const string Remove = "Remove Data"; |
| 60 | +
|
| 61 | + private TelerikChart ChartRef { get; set; } |
| 62 | + private List<ChartSeriesData> ChartData { get; set; } = new List<ChartSeriesData>(); |
| 63 | + private string ButtonContent { get; set; } = Add; |
| 64 | +
|
| 65 | + private void UpdateData() |
| 66 | + { |
| 67 | + if (ChartData == null || ChartData?.Count() == 0) |
| 68 | + { |
| 69 | + ChartData = ChartSeriesData.GenerateData(); |
| 70 | + ButtonContent = Remove; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + // Clear the data |
| 75 | + ChartData = new List<ChartSeriesData>(); |
| 76 | + ButtonContent = Add; |
| 77 | + } |
| 78 | + ChartRef.Refresh(); // Refresh the Chart |
| 79 | + } |
| 80 | +
|
| 81 | + public class ChartSeriesData |
| 82 | + { |
| 83 | + public int ProductSales { get; set; } |
| 84 | + public int Year { get; set; } |
| 85 | +
|
| 86 | + public static List<ChartSeriesData> GenerateData() |
| 87 | + { |
| 88 | + List<ChartSeriesData> data = new List<ChartSeriesData> |
| 89 | + { |
| 90 | + new ChartSeriesData { ProductSales = 120, Year = 2020 }, |
| 91 | + new ChartSeriesData { ProductSales = 180, Year = 2021 }, |
| 92 | + new ChartSeriesData { ProductSales = 150, Year = 2022 }, |
| 93 | + new ChartSeriesData { ProductSales = 210, Year = 2023 }, |
| 94 | + new ChartSeriesData { ProductSales = 90, Year = 2024 } |
| 95 | + }; |
| 96 | +
|
| 97 | + return data; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +```` |
| 102 | + |
| 103 | +By applying the `.k-chart-overlay { display: none; }` style, the No Data template overlay is hidden, allowing the Chart to appear empty when there is no data. |
| 104 | + |
| 105 | +## See Also |
| 106 | + |
| 107 | +- [Blazor Charts Overview](slug://components/chart/overview) |
| 108 | +- [Customizing the Appearance of Blazor Charts](slug://themes-override) |
0 commit comments