Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new kb article chart-display-empty #2699

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/chart/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ Starting in **version 7.0.0**, when all Chart series have no data to show, a def
## See Also

* [Live Demo: Chart - No Data Template](https://demos.telerik.com/blazor-ui/chart/no-data-template)
* [How to Show Empty Chart Instead the Default No Data Template](slug:// chart-kb-display-empty-chart)
Tsvetomir-Hr marked this conversation as resolved.
Show resolved Hide resolved

107 changes: 107 additions & 0 deletions knowledge-base/chart-display-empty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: How to Show Empty Chart Instead of the Default No Data Template
description: Learn how to show an empty Chart component when there is no data, instead of displaying the default No Data template.
type: how-to
page_title: How to Show Empty Chart Instead of the Default No Data Template
slug: chart-kb-display-empty-chart
tags: charts, blazor, no data template, empty chart
res_type: kb
ticketid: 1675313
---

## Environment
<table>
<tbody>
<tr>
<td>Product</td>
<td>Chart for Blazor</td>
</tr>
</tbody>
</table>

## Description

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. This knowledge base article also answers the following questions:

- How can I hide the No Data Template in Blazor Charts?
- Is it possible to display an empty Chart in Blazor when there's no data?
- Can I override the No Data Template in Blazor Charts?
Comment on lines +26 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unnecessary to have "This knowledge base article also answers the following questions:" section, given the description.


## Solution

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.



````RAZOR
<TelerikButton OnClick="@UpdateData">@ButtonContent</TelerikButton>
<br />
<TelerikChart @ref="ChartRef" Width="800px" Height="400px">
<ChartSeriesItems>
<ChartSeries Type="ChartSeriesType.Column"
Data="@ChartData"
Name="Product Sales"
Field="@nameof(ChartSeriesData.ProductSales)"
CategoryField="@nameof(ChartSeriesData.Year)">
</ChartSeries>
</ChartSeriesItems>
</TelerikChart>

<style>
.k-chart-overlay {
display: none;
}
</style>

@code {
private const string Add = "Add Data";
private const string Remove = "Remove Data";

private TelerikChart ChartRef { get; set; }
private List<ChartSeriesData> ChartData { get; set; } = new ();
private string ButtonContent { get; set; } = Add;

private void UpdateData()
{
if (ChartData == null || ChartData?.Count() == 0)
{
ChartData = ChartSeriesData.GenerateData();
ButtonContent = Remove;
}
else
{
// Clear the data
ChartData = new List<ChartSeriesData>();
ButtonContent = Add;
}
ChartRef.Refresh(); // Refresh the Chart
}

public class ChartSeriesData
{
public int ProductSales { get; set; }
public int Year { get; set; }

public static List<ChartSeriesData> GenerateData()
{
List<ChartSeriesData> data = new List<ChartSeriesData>
{
new ChartSeriesData { ProductSales = 120, Year = 2020 },
new ChartSeriesData { ProductSales = 180, Year = 2021 },
new ChartSeriesData { ProductSales = 150, Year = 2022 },
new ChartSeriesData { ProductSales = 210, Year = 2023 },
new ChartSeriesData { ProductSales = 90, Year = 2024 }
};

return data;
}
}
}
````

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem needed.


## See Also

- [Blazor Charts Overview](slug://components/chart/overview)
- [Override Theme Styles](slug://themes-override)