|
| 1 | +--- |
| 2 | +title: No Data Template |
| 3 | +page_title: Chart - No Data Template |
| 4 | +description: The NoDataTemplate in the Chart for Blazor lets you customize the content displayed when no data is available for any series. |
| 5 | +slug: chart-no-data-template |
| 6 | +tags: telerik,blazor,chart,templates |
| 7 | +published: True |
| 8 | +position: 101 |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | +# No Data Template |
| 13 | + |
| 14 | +The `NoDataTemplate` allows you to define custom content when any of the Chart series has no data to show. To change the default **No data** localizable text, declare a `<NoDataTemplate>` tag inside a `<ChartSettings>` tag: |
| 15 | + |
| 16 | +````CSHTML |
| 17 | +<TelerikButton OnClick="@UpdateData">@ButtonContent</TelerikButton> |
| 18 | +<br /> |
| 19 | +<TelerikChart @ref="ChartRef" Width="800px" Height="400px"> |
| 20 | + <ChartTitle Text="Product Sales Over the Years" Position="@ChartTitlePosition.Bottom"></ChartTitle> |
| 21 | +
|
| 22 | + <ChartSettings> |
| 23 | + @* Define what should be shown when there's no data in the chart *@ |
| 24 | + <NoDataTemplate> |
| 25 | + <p>No data available to display. Please add product data or check back later.</p> |
| 26 | + </NoDataTemplate> |
| 27 | + </ChartSettings> |
| 28 | +
|
| 29 | + <ChartSeriesItems> |
| 30 | + <ChartSeries Type="ChartSeriesType.Column" |
| 31 | + Data="@ChartData" |
| 32 | + Name="Product Sales" |
| 33 | + Field="@nameof(ChartSeriesData.ProductSales)" |
| 34 | + CategoryField="@nameof(ChartSeriesData.Year)"> |
| 35 | + </ChartSeries> |
| 36 | + </ChartSeriesItems> |
| 37 | +</TelerikChart> |
| 38 | +
|
| 39 | +@code { |
| 40 | + private const string Add = "Add Data"; |
| 41 | + private const string Remove = "Remove Data"; |
| 42 | +
|
| 43 | + private TelerikChart ChartRef { get; set; } |
| 44 | + private List<ChartSeriesData> ChartData { get; set; } = new List<ChartSeriesData>(); |
| 45 | + private string ButtonContent { get; set; } = Add; |
| 46 | +
|
| 47 | + private void UpdateData() |
| 48 | + { |
| 49 | + if (ChartData == null || ChartData?.Count() == 0) |
| 50 | + { |
| 51 | + ChartData = ChartSeriesData.GenerateData(); |
| 52 | + ButtonContent = Remove; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + // Clear the data |
| 57 | + ChartData = new List<ChartSeriesData>(); |
| 58 | + ButtonContent = Add; |
| 59 | + } |
| 60 | + ChartRef.Refresh(); // Refresh the Chart |
| 61 | + } |
| 62 | +
|
| 63 | + public class ChartSeriesData |
| 64 | + { |
| 65 | + public int ProductSales { get; set; } |
| 66 | + public int Year { get; set; } |
| 67 | +
|
| 68 | + public static List<ChartSeriesData> GenerateData() |
| 69 | + { |
| 70 | + List<ChartSeriesData> data = new List<ChartSeriesData> |
| 71 | + { |
| 72 | + new ChartSeriesData { ProductSales = 120, Year = 2020 }, |
| 73 | + new ChartSeriesData { ProductSales = 180, Year = 2021 }, |
| 74 | + new ChartSeriesData { ProductSales = 150, Year = 2022 }, |
| 75 | + new ChartSeriesData { ProductSales = 210, Year = 2023 }, |
| 76 | + new ChartSeriesData { ProductSales = 90, Year = 2024 } |
| 77 | + }; |
| 78 | +
|
| 79 | + return data; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +```` |
| 84 | + |
| 85 | +## See Also |
| 86 | + |
| 87 | + * [Live Demo: Chart - No Data Template](https://demos.telerik.com/blazor-ui/chart/no-data-template) |
| 88 | + |
0 commit comments